» » Inheritance Definition and Programs

  • Getting the properties from one class object to other class is known as inheritance.
  • In this inheritance will have parent class and child class so getting the properties of parent class object to child class object known as inheritance.
  • Inheritance means to take something that already made.
  • Inheritance can be implemented in java by using "extends" and "implements' keywords
  • In this we have two types.
  • Multilevel inheritance
  • Multiple inheritance

Multilevel inheritance:

  • Getting the properties from super class object to sub class object level wise with different priorities known as multiple inheritance.

Super class:

  1. package instanceofjava; 

  2. public class Super {
  3.  
  4.     int a,b;
  5.  
  6. void add(){
  7.  
  8.     System.out.println((a+b));
  9.  
  10. }
  11.  
  12. }

Sub class:

  1. package instanceofjava;
  2.  
  3. public class Sub extends Super {
  4.  
  5. void show(){
  6.  
  7.       System.out.println("this is sub class show() method");
  8.  
  9. }
  10.  
  11. public static void main(String [] args){
  12.  
  13.        Sub obj= new Sub();
  14.  
  15.         obj.a=1;
  16.         obj.b=2;
  17.  
  18.         obj.add();
  19.         obj.show();
  20.  
  21.     }
  22. }



Output:

  1. 3
  2. this is sub class show() method

Example program #2:

  1. package instanceofjava;
  2.  
  3. public class A {
  4.  
  5.     int a,b;
  6.  
  7. public void show(){
  8.  
  9.   System.out.println("A class show method");
  10.  
  11. }
  12.  
  13. }



  1. package instanceofjava;
  2.  
  3. public class B extends A{
  4.  
  5.  
  6. public void print(){
  7.  
  8.   System.out.println("B class print method");
  9.  
  10. }
  11.  
  12. }

  1. package instanceofjava;
  2.  
  3. public class C extends B{
  4.  
  5.  
  6. public void display(){
  7.  
  8.   System.out.println("C class display method");
  9.  
  10. }
  11. public static void main(String [] args){
  12.  
  13.  C obj= new C();
  14.  
  15.    obj.print();
  16.    obj.show();
  17.    obj.display();

  18. }
  19.  
  20. }

Output:

  1. B class print() method
  2. A class show method
  3. Class C display method

Multiple inheritance:

  • Multiple inheritance means getting the properties from one class object to multiple class object with different priority.
  • Multiple inheritance concept is not supported by java

  1. package instanceofjava;
  2.  
  3. public class A {

  4.  
  5. public void show(){
  6.  
  7.   System.out.println("A class show method");
  8.  
  9. }
  10.  
  11. }

  1. package instanceofjava;
  2.  
  3. public class b {
  4.  
  5. public void show(){
  6.  
  7.   System.out.println("B class show method");
  8.  
  9. }
  10.  
  11. }


  1. package instanceofjava;
  2.  
  3. public class C extends A,B {  // not supported by java
  4.  
  5.  
  6.  
  7. public void print(){
  8.  
  9.   System.out.println("C class print method");
  10.  
  11. }
  12.  
  13. }


You Might Like:

1. Java Interview Programs On Core Java

2. Java Interview Programs On Collections 

3. What is The Output Of Following Programs Java Interview:

4. Java Concept and Example Program

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu