OOPs Concept’s in Java
OOPs Refers to Object-Oriented Programming Model that provides different types of concept’s such as inheritance, polymorphism, abstraction, encapsulations in java or any other language so let’s start to talk about each concept’s in detail.
Inheritance
If we do focus on the word inheritance, it gives the feeling of parent and child relationship in which child inherits his parent's property. Similarly, Inheritance works in Java that is child class inherit the methods and variables of his parent class. For inheriting child class uses extends keyword.
Five types of inheritance exist:-
- Single inheritance
- Multi-level inheritance
- Hierarchical inheritance
- Multiple inheritances
- Hybrid inheritance
Single inheritance
In single inheritance, only one level of inheritance has happened and in which derived class inherits the properties of a parent class and also added some other features.
Example:-
public class Animal{ public void voice(){
System.out.println("Hello I am animal"); }
}
public class Cow extends Animal{
public void sound(){
System.out.println("Hello I am Cow"); }
}
public class Main{
public static void main(String[] args){
Cow obj = new Cow();
obj.sound();
obj.voice(); }}
Output:-
Hello I am Cow
Hello I am animal
In this example, Cow class inherit the Animal class with the help of extends keyword and In the main method, we call the voice method which is in the Animal class with the help of the Cow object.
Multi-level inheritance
In multi-level inheritance, multiple levels of inheritance have happened and in which derived class inherits the properties of a parent class and also added some other features.
Example:-
class Animal{
public void voice(){
System.out.println("Hello I am animal"); }}
class Calf extends Cow{
public void soundCalf(){
System.out.println("Hello I am Calf"); }
}public class Main{
public static void main(String[] args){
Calf obj = new Calf();
obj.soundCalf();
obj.sound();
obj.voice(); }}
Output:-
Hello I am Calf
Hello I am Cow
Hello I am Animal
In this example, Cow class inherit the Animal class and the Calf class inherit the Cow class with the help of extends keyword and In the main method, we call the voice method and sound method which is in the Animal class and Cow class with the help of the Calf object.
Hierarchical inheritance
In hierarchical inheritance, more than one class inherit the same parent class.
Example:-
public class Animal{
public void voice(){
System.out.println("Hello I am animal"); }
}
public class Cow extends Animal{
public void soundCow(){
System.out.println("Hello I am Cow"); }
}
public class Dog extends Animal{
public void soundDog() {
System.out.println("Hello I am Dog"); }
}public class Main{
public static void main(String[] args){
Dog obj1 = new Dog();
Cow obj2 = new Cow();
obj1.voice();
obj2.voice(); }}
Output:-
Hello I am animal
Hello I am animal
In this example, class Cow and Dog inherit the same Animal class and give the same results when we called the Animal voice method with the individual objects.
Multiple inheritance
In Multiple inheritances, the child class inherit more than one parent class. Java does not support multiple inheritance (without interface) because what happens if parents class have same methods which method choose by the child class. So in this inheritance blockage is occurring and Therefore, Hybrid inheritance also not supported by Java.
Polymorphism
Polymorphism occurs when we have multiple classes related to each other with inheritance. Polymorphism has two types.
- Compile-time polymorphism(method overloading)
- Run-time polymorphism(method overriding)
Compile-time polymorphism
Compile-time polymorphism occurs when we have two methods with the same name but different signatures or parameters in the same class. It is also called method overloading.
Example:-
public class Calc{
public void add(int a, int b){
System.out.println(a + b); }
public void add(int a, int b, int c){
System.out.println(a + b + c); }}
public class Main{
public static void main(String[] args){
Calc obj1 = new Calc();
Calc obj2 = new Calc();
obj1.add(2, 3);
obj2.add(2, 3, 4); }}
Output:-
5
9
Run-time polymorphism
Run-time polymorphism occurs when we have the same name methods in a different classes which follow parent and child inheritance concepts with same signature or parameter. It is also called method overriding.
Example:-
public class Parent{
public void voice(){
System.out.println("Hello I am Parent"); }
}public class Child extends Parent{
@Override
public void voice(){
System.out.println("Hello I am Child"); }
}
public class Main{
public static void main(String[] args){
Child obj = new Child();
obj.voice(); }}
Output:-
Hello I am Child
Encapsulation
Encapsulation is a process that bind data and method together in single unit and keeps data safe from outside interference or other classes that these does not able to access the data. Encapsulation is also used for data hidding.
Encapsulation is achieved by making the data private and provide public getter and setter method.
Example:-
public class Person{
private String name;
private int age ;
public void setName(String name){
this.name = name; }
public void setAge(int age){
this.age = age; }
public String getName(){
return name; }
public int getAge(){
return age; }}
public class Main{
public static void main(String[] args){ Person obj = new Person();
obj.setName("Harish");
obj.setAge(27);
System.out.println(obj.getName());
System.out.println(obj.getAge()); }}
Output:-
Harish
27
Abstraction
Abstraction is a process by which be hide the unnecessary data and show only those data which we want to show.
We acquire a abstraction class by abstraction keyword which will be placed in front of class keyword. Abstraction class have one or more abstraction method and it also contain general method.
Example:-
abstract class Animal{
abstract void voice();}
class Cow extends Animal{
void voice(){
System.out.println("Hello I am Cow"); }}
public class Main{
public static void main(String[] args){
Animal obj = new Cow();
obj.voice(); }}
Output:-
Hello I am Cow