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 blog is for everyone who want to learn Java as well as want quick refresh of Java. This is really easy manner to learn something in easy way, whole blog is explained in very simple language so that even the person who don't know anything about Java can understand it.
Showing posts with label Example of Inheritance and types of Inheritance. Show all posts
Showing posts with label Example of Inheritance and types of Inheritance. Show all posts
Thursday, November 15, 2012
Subscribe to:
Posts (Atom)