Object-Oriented Programming in Java
Java OOPs Concept
OOP stands for Object-Oriented Programming.
Java is an object-oriented Language. This chapter explains how to create a class and object in Java programming.
OOP stands for Object-Oriented Programming.
Object means a real-world entity such as a pen, chair, table, computer, watch, etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects.
List of OOPs Concepts in Java
Objects
Classes
Object
Class
Abstraction
Inheritance
Polymorphism
Encapsulation
Class in Java
Collection of objects is called class.
A class is a template for multiple objects with similar features.
Classes embody all the features of a particular set of objects. It is a logical entity.A class can also be defined as a blueprint from which you can create an individual object. Class doesn’t consume any space.
In Java, a class is defined using class keyword as follows :
class <ClassName >
{
<Variable>
<Methods>
}
Every class we write in Java is generally made up of two components: attributes and behaviour. Attributes are defined by variables ina class. Behaviour is defined by methods ina class. Methods are used to access or modify attributes.
Object in Java
Any entity that has state and behavior is known as an object.
For example, a chair, pen, table, keyboard, bike, etc. It can be physical or logical.An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory.
Example: A dog is an object because it has states like color, name, breed, etc. as well as behaviors like wagging the tail, barking, eating, etc.
Creating objects
Creating an object from a class requires the following steps :
• Declaration :A variable (reference variable) name of type class is declared with syntax <class
name> <variable name>
• Instantiation : Keyword new is used to create the object by allocating memory
• Initialization :Constructor (a special type of method) is called to initialize the newly created object.
Room r2 — new RoomO;
Inobject-oriented programming (OOP) languages, creating an object is also called object instantiation. An instance for an object is created by allocating memory to store the data for that object.
Inheritance in Java
it is possible to inherit attributes and methods from one class to another. We group the “inheritance concept” into two categories:
subclass (child) – the class that inherits from another class
superclass (parent) – the class being inherited from
To inherit from a class, use the extends keyword.
Object-orientedprogramming languages provide reusability feature usinginheritance Inheritance allows us to build new class with added capabilities by extending existing class.
Inheritance models ‘is-a’ relationship between two classes. For example, classroom is a room, student is a person. Here, room and person are called parent class; classroom and student are called child classes. Parent class is also referred to as superclass or base class. Inthe same way; child class is also referred to as subclass, derived class or extended class.
Whenever two classes have ‘is-a1 relationship, we use inheritance. Common features are kept in superclass. A subclass inherits all instance variables and methods from superclass and it may have its own added variables and methods.
Fun & Easy to follow
Works on all devices
Your own Pace
Super Affordable
Popular Videos
Play Video
UX for Teams
Learn the basics and a bit beyond to improve your backend dev skills.
Chris Matthews
Designer
Play Video
SEO & Instagram
Learn the basics and a bit beyond to improve your backend dev skills.
Chris Matthews
Designer