Gadgets

Thursday, 21 February 2013

Java Inheritance


Inheritance:-
Inheritance is mechanism to create a new class by taking properties of existing(super) class. existing class is called super class & produced class is called sub class.
Here we create object for sub class
1.      Singe inheritance.
2.      Multilevel inheritance.
3.      Multiple inheritance.
àMultiple inheritance is not available in java. Here one sub class has more than one super class. JVM confuse to execute super class.
Single Inheritance:-
We can create only one subclass and it’s consist only one super class.
 
Multilevel Inheritance:-
We can create two or more super classes and sub classes.
“A” is super  class of “B”. ”B” is super class of “C” and “b” Is sub class of “A”. 
 
Example program: -
public class Inheritance {
    public static void main(String args[]){
        Sub s=new Sub();
        s.adding();
        s.subt();
    }
   
}
class Add{
    int a=10,b=20,c;
    void adding()
    {
       
        c=a+b;
        System.out.println("super class :"+c);
    }
}
class Sub extends Add{
    void subt(){
   c=b-a;
    System.out.println("sub class :"+c);
    }
}
     

No comments:

Post a Comment