Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Example of Inheritance and types of Inheritance !!!!

In Java there are four types of Inheritance

1) Single Inheritance (one super class)
2) Multilevel Inheritance (several super class)
3) Hierarchical Inheritance (one super class many sub-classes)
4) Multiple Inheritance   (derived from derived class)


                                   Single Inheritance                             Hierarchical Inheritance
                           Multilevel Inheritance                                 Multiple Inheritance
                                 


________________________________________________

public class Multi {
    int multi1;
    int multi2;
    
    multi(int a, int b){                             // constructor method
        multi1 = a;
        multi2 = b;
    }
    
    int result(){                                        //definition of method
        
      return(multi1 * multi2);
    }

    
    class multi1 extends multi{
        int multi3;
        
        multi1(int a, int b, int c){                       // constructor method

            super(a,b);         // super keyword only used in constructor method
            
multi3 = c;

        }
            int result1(){                                         //definition of method
            return(multi1 * multi2 * multi3);
        }
        }
    


class total{                                          // class with main method
    
    public static void main(String [] args){
        
        multi1 multii = new multi1(1,2,3);         //creating object
        
        System.out.println("Total will be: " +multii.result1());
        
        
    }
}
__________________________________________

Output:

Total will be: 6

________________________________________

Above example is just Single Inheritance example ! 


This post first appeared on LET's START CORE JAVA !, please read the originial post: here

Share the post

Example of Inheritance and types of Inheritance !!!!

×

Subscribe to Let's Start Core Java !

Get updates delivered right to your inbox!

Thank you for your subscription

×