Object-Oriented Programming — Java
OOPs Concept in Java
Object refers to a real-world entity like a Table, Fan, Keyboard, Pen, Bike, etc. Object-Oriented Programming is a methodology to design a program with the help of classes and objects. This greatly simplifies the software development and maintenance by providing some concepts:
- Class
- Object
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
Class
A class is defined as a user-defined blueprint using which we can create objects. A class does not consume any space.
Object
An object is any entity that has a state and behavior. An Object is an instance of a class. It contains an address and takes up some space in memory. Different objects can interact with each other’s data without getting to know the details.
Inheritance
When an object acquires all properties and behaviors of the parent object, this relation is known as Inheritance. Code reusability is provided by Inheritance. While inheriting from a class, you can use the methods and fields defined in the parent class already. In addition to this, you can also add new methods and fields in the newly created class.
Types of Inheritance:
- Single Inheritance: Single Inheritance is defined when a child class inherits the properties and behavior of the parent class.
- Hierarchical Inheritance: In Hierarchical Inheritance, more than one subclass inherits the properties and behaviors of the superclass(base class). For example, a class named A is a superclass (base class) for B, C, and D subclasses.
- Multilevel Inheritance: In Multilevel Inheritance, a subclass inherits a base class and that subclass also acts as a base class for another subclass inheriting it.
- Hybrid Inheritance: It is a mix of two or more of the above type of inheritance, Hierarchical Inheritance and Multilevel Inheritance.
- Multiple Inheritance: A subclass inherited by multiple superclasses is known as Multiple Inheritance.
Polymorphism
It is a concept of performing a single task in different ways. The word Polymorphism is derived from two Greek words: poly and morphs. “Poly” means many and “Morphs” means forms.
Polymorphism in Java can be achieved by method overloading and method overriding.
Method Overloading Example:
class OverloadingExample{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
Method Overriding Example:
class Animal{
void eat(){System.out.println(“eating…”);}
}
class Dog extends Animal{
void eat(){System.out.println(“eating bread…”);}
}
Abstraction
Abstraction is used when you only want to display essential details and functionality.
A real-life example of Abstraction is an ATM Machine. You only know how to withdrawal, deposit, check balance, etc., and don't know about the mechanism behind these operations.
Encapsulation
When data is wrapped under a single unit, it is called encapsulation. It binds the code and data is manipulated. Another way to define encapsulation is, it provides a protective shield that does not let anyone access data by the code outside this shield.