OOPs Concept’s in Java

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.

  1. Single inheritance
  2. Multi-level inheritance
  3. Hierarchical inheritance
  4. Multiple inheritances
  5. 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.

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();
}}

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.

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();
}}

Hierarchical inheritance

In hierarchical inheritance, more than one class inherit the same parent class.

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();
}}

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.

  1. Compile-time polymorphism(method overloading)
  2. 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.

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);
}}

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.

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();
}}

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.

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());
}}

Abstraction

Abstraction is a process by which be hide the unnecessary data and show only those data which we want to show.

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();
}}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store