Example : A java program to show simple inheritance concept.
class Parent
{
    int x=50;
}
 class Child extends Parent 
{
    int y=30;    
    public static void main(String[] args)
    {
        Child obj = new Child();
        System.out.println(obj.x +" "+obj.y);         
    }
}
Output :
50 30

---------  OR  ---------
class Parent
{
    int x;
}
 class Child extends Parent 
{
    void display()
    {
	x=50;
        int y=30;
	int z=x+y;
	System.out.println(z);
    }
    public static void main(String[] args)
    {
        Child obj = new Child();
        obj.display();          
    }
}
Output :
80
Example : A java program to show a final class can not be inherited.
final class Parent
{
    void display1()
    {
        System.out.println("Parent Class Method Executed");
    }
}
class Child extends Parent 
{
    void display2()
    {
        System.out.println("Child Class Method Executed");
    }
    public static void main(String[] args)
    {
        Child obj = new Child();
        obj.display1();  
        obj.display2();  
    }
}

Output :
F:\>javac abc.java
abc.java:8: error: cannot inherit from final Parent
class Child extends Parent
                    ^
1 error
Example : A java program to show SUPER keyword in inheritance.
class Parent
{
    int x;
}
class Child extends Parent 
{
    void display()
    {
	super.x=50;
        int x=30;
	int z=super.x+x;
	System.out.println(z);
    }
    public static void main(String[] args)
    {
        Child obj = new Child();
        obj.display();          
    }
}
Output :
80

---------  OR  ---------
class Parent
{
    int x;
}
class Child extends Parent 
{
    void display()
    {
	x=50;
        int x=30;

	int z=super.x+x;
	int p=x+x;
	System.out.println(z);
	System.out.println(p);
    }
    public static void main(String[] args)
    {
        Child obj = new Child();
        obj.display();          
    }
}
Output :
80
60

---------  OR  ---------
class Parent
{
     int x;
     void display()
      {
	 x=50;
	 System.out.println(x);
      }
}
 class Child extends Parent 
{
    int x;
    void display()
    {
	super.display();
	x=30;        
	System.out.println(x);
    }
    public static void main(String[] args)
    {
        Child obj = new Child();
        obj.display();          
    }
}
Output :
50
30

---------  OR  ---------
class Parent
{
    int x;
    Parent(int y)
     {
	x=y;
	System.out.println(x);
     }
}
class Child extends Parent 
{
    int x;
    Child(int m,int n)
      {
	 super(m);      //Passing Argument to Parent Class Constructor
	 this.x=n;		
      }
    void display()
     {
	 System.out.println(super.x +" and "+x);
     }
    public static void main(String[] args)
    {
        Child obj = new Child(50,30);
        obj.display();          
    }
}
Output :
50
50 and 30
Example : A java program to show SINGLE inheritance.
class Parent
{
    void display1()
    {
        System.out.println("Parent Class Method Executed");
    }
}
class Child extends Parent 
{
    void display2()
    {
        System.out.println("Child Class Method Executed");
    }
    public static void main(String[] args)
    {
        Child obj = new Child();
        obj.display1();  
        obj.display2();  
    }
}
Output :
Parent Class Method Executed
Child Class Method Executed
Example : A java program to show MULTILEVEL inheritance.
class Parent
{
    int x=10;
    void display1()
    {
        System.out.println(x);
    }
}

class Child extends Parent 
{
    int y=20;
    void display2()
    {
        System.out.println(y);
    }
}
class Subchild extends Child 
{
    int z=30;
    void display3()
    {
        System.out.println(z);
    }
    public static void main(String[] args)
    {
        Subchild obj = new Subchild();
        //obj.display1();       //Error is produced
        obj.display2();
	obj.display3();
		
	System.out.println();
		
        Child obj1 = new Child();
        obj1.display1();  
        obj1.display2();
	//obj1.display3();     //Error is produced
    }
}
Output :
F:\>javac abc1.java
F:\>java Subchild
20
30

10
20
Example : A java program to show HIERARCHICAL inheritance.
class Parent
{
    int x=10;
    void display1()
    {
        System.out.println(x);
    }
}
class Child extends Parent 
{
    int y=20;
    void display2()
    {
        System.out.println(y);
    }
}
class Subchild extends Parent 
{
    int z=30;
    void display3()
    {
        System.out.println(z);
    }
    public static void main(String[] args)
    {
        Subchild obj1 = new Subchild();
        obj1.display1();        
	obj1.display3();
		
	System.out.println();
		
	Child obj2 = new Child();
        obj2.display1();        
	obj2.display2();		
     }
}
Output :
10
30

10
20
Example : A java program to show Multiple inheritance, but fail.
class Parent
	{  
	   void display1()
	   {
	       System.out.println("Codershelpline 01");
	   }  
	}  
class Child
	{  
	   void display2()
	   {
	        System.out.println("Codershelpline 02");
	   }  
	}  
class SubChild extends Parent,Child
   {   
        public static void main(String args[])
	   {  
		 SubChild obj=new SubChild();  
		 obj.display1(); 
		 obj.display2();
	   }  
    }

Output :
F:\>javac abc1.java
abc1.java:15: error: '{' expected
class SubChild extends Parent,Child
                             ^
1 error
F:\> 

---------  OR  ---------
class Parent
  {  
      void display()
	{
	   System.out.println("Codershelpline 01");
	}  
  }  
class Child
  {  
       void display()
	 {
            System.out.println("Codershelpline 02");
	 }  
  }  
class SubChild extends Parent,Child
   {   
       public static void main(String args[])
	 {  
	     SubChild obj=new SubChild();  
	     obj.display();		 
	 }  
   }

Output :
F:\>javac abc1.java
abc1.java:15: error: '{' expected
class SubChild extends Parent,Child
                             ^
1 error
F:\>    

 482 total views,  1 views today

Categories: Java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.