Inheritance in java

Inheritance in java

Inheritance in java

Inheritance

  • Access the resources of the .class file after establish the relation is the concept of inheritance in java.
  • Inheritance is the object-oriented concept which is used to create a relation between two .class files.
  • Relation mean one .class file parent class(base class,super class ,master class) and another .class file.
  • We can access the member of another .class like own resource in case of inheritance.
  • The private member of superclass can’t be accessed within the subclass.
  • The protected member of superclass can’t be accessed within the subclass.

Types of inheritance in java

  1. Single inheritance
  2. Multilevel inheritance
  3. Multiple inheritance
  4. Hierarchical inheritance
  5. Hybrid inheritance

Single Inheritance

Only two .class file design with the relation is known as single inheritance.

single

Example

class Test

{

   int a=10;

    Test()

    {

       System.out.println("abc");

     }

    public static void main(string [] args)

    {

         Test t1=new Test();

          System.out.println(t1.a);             

    }

}
output
abc 
10

Multilabel Inheritance

multilable

When a .class file behaves like a parent class as well as subclass then it is known as multilabel inheritance .object creation in java always in the top to down approach.

Example

 

class A

{

    int x=10;

     A()

     {

           System.out.println("i am in a");

      }

}

class B extends A

{

      int y=20;

       B()

        {

               System.out.println("I am in B");

        }

}

class Test extends B

{

     int z=30;

      Test()

      {

            System.out.println("i am in Test");

       }

    public static void main(String[] args)

     {

            Test t1=new Test();

             System.out.println(t1.x);        

              System.out.println(t1.y);        

               System.out.println(t1.z);        

       }

}




Output 
            A class constructor………………..

            Demo class constructor……………..

            Test class constructor…………………….

            10     20       30

Multiple Inheritance

If more than one .class file behaves like parent class that is the situation of multiple inheritance.

If Java does not support multiple inheritance because it may create method ambiguity.

where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call a method of A or B class.

multiple

Example
interface A

{

                void show();

}

interface B

{

                void show();

}

class C implements A,B

{

     public void show()

      {

             System.out.println("i am in show");

       }

     public static void main(String[] args)

     {

          C c1=new C();

    }

}



Hierarchical Inheritance

If more than one .class file inherited in a single base class that is known as hierarchical inheritance.

hierarchical

Example

class Test

{




}

class Demo

{




}

class MyClass

{

     public static void main(String[] args)

      {

            Test t1=new Test();

            Demo d1=new Demo();

            MyClass mc=new MyClass();     

        }

}

Hybrid Inheritance

The combination of more than one of the inheritance is known as hybrid inheritance.

hybrid

Example

class A

{

    public void show()

     {

           System.out.println("i am in a");

      }

}

class B extends A

{

    void display()

     {

        System.out.println("i am in b");

      }          

}

class C extends A

{

    public static void main(String[] args)

     {

          C c=new C();

           c.show();

      }

}
basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community