Inheritance in java

Inheritance

Inheritance is a process of deriving a new class from existing class. The new class gets properties and methods inherited from existing class.
super class
It is a class from which a new class inherits properties and methods. It is also known as base class or parent class or super class.
Sub Class
It is a class, which is derived from an existing class. It is class known as child class or derived class.

Why use inheritance in java
  • For Method Overriding ( polymorphism).
  • For Code Reusability.
Types of Inheritance  
  1. Single inheritance
  2. Multiple inheritance
  3. Multi level inheritance
  4. Hierarchical inheritance
Syntax of Java Inheritance

Class subclass-name  extends superclass-name{

}

The extends keyword indicates that you are making a new class that derives from an existing class.

Single inheritance


It is a process of deriving a new class from a single base class.When a class inherits another class, it is known as a single inheritance.

Example:

class a{

    void j1(){

    System.out.println("java");

    }

}

class b extends a{

    void j2(){

    System.out.println("python");   

    }

}

 

public class Pairosoft{

    public static void main(String[] args){

      b obj=new b();

      obj.j1();

      obj.j2(); 

    }

}


Output:

java

python


Multiple inheritance


It is a process of deriving a new class from more than one base class.
Syntax:

class A

                {

                                statements ;

                }

                class B

                {

                                statements ;

                }

                class C extends A, B

                {

                                statements ;

                }

NOTE:
          Java does not support multiple inheritance with classes. But, it supports multiple inheritance using interfaces.

Multi level inheritance


It is a process of deriving a new class from another deriving class.When there is a chain of inheritance, it is known as multilevel inheritance.

Example:

class a{

    void j1(){

    System.out.println("java");

    }

}

class b extends a{

    void j2(){

    System.out.println("python");   

    }

}

class c extends b{

    void j3(){

    System.out.println("c++");

    }

}

public class Pairosoft{

    public static void main(String[] args){

      c obj=new c();

      obj.j1();

      obj.j2();

      obj.j3();

    }

}


Output:

java

python

c++


Hierarchical inheritance


It is a process of deriving more than one class from a single base class.

Example:

class a{

    void j1(){

    System.out.println("java");

    }

}

class b extends a{

    void j2(){

    System.out.println("python");   

    }

}

class c extends a{

    void j3(){

    System.out.println("c++");

    }

}

public class Pairosoft{

    public static void main(String[] args){

      c obj=new c();

      obj.j1();

      //obj.j2(); error

      obj.j3();

    }

}


Output:

Java

c++

 


More will come idea if you watch this video:-




 






Post a Comment

0 Comments