type of inheritance code example

Example 1: java program to demonstrate multilevel inheritance

Class X
{
   public void methodX()
   {
     System.out.println("Class X method");
   }
}
Class Y extends X
{
public void methodY()
{
System.out.println("class Y method");
}
}
Class Z extends Y
{
   public void methodZ()
   {
     System.out.println("class Z method");
   }
   public static void main(String args[])
   {
     Z obj = new Z();
     obj.methodX(); //calling grand parent class method
     obj.methodY(); //calling parent class method
     obj.methodZ(); //calling local method
  }
}

Example 2: types of inheritance

OOPs support the six different types of inheritance as given below :
Single inheritance.
Multi-level inheritance.
Multiple inheritance.
Multipath inheritance.
Hierarchical Inheritance.
Hybrid Inheritance.

Example 3: types of inheritance

//Base Class
class A 
{
 public void fooA()
 {
 //TO DO:
 }
}

//Base Class
class B
{
 public void fooB()
 {
 //TO DO:
 }
}

//Derived Class
class C : A, B
{
 public void fooC()
 {
 //TO DO:
 }
}

Tags:

Misc Example