1.   Multilevel inheritance
(JAVA supports) 
  2.   Multiple
inheritance(C,C++  supports,JAVA does
not)
   3.   Cyclic inheritance(no
programming language).
Program for
Multilevel inheritance:
| 
   
class GrandFather {                  // parent class
  (or)  super class   
int
  age ; 
void  cultivator( )   { 
System.out.println("my
  grand fa was a cultivator");   
  }    } 
class Father 
  extends  GrandFather   {              //
  Father sub class 
int
  age ; 
void  cultivator( )   { 
System.out.println("Father
  was cultivator");     } 
void  business( )   { 
System.out.println("Father
  was a businessMan");  }} //Son is
  Sub Class here and Father is a superclass 
class Son extends Father   { 
int
  age ; 
void  teacher( )   { 
System.out.println("Teacher");   } 
void cultivator( )  
  { 
System.out.println("Son
  is cultivator"); 
super.cultivator();    } 
void  business( )   { 
System.out.println("Son
  is a businessMan");   } 
public static
  void main(String[] args)  
  { 
Son son = new Son(); 
son.cultivator();     }  
  }                         //class
  Son extends GrandFather, Father // not possible in java. 
 | 
 
Explanation:-
Ø Son
son = new Son();
Ø Object
will be created for Son class variable son.
Ø Son();will
be executed constructor of Son class will be loaded.
Ø Compiler
will do following for us
| 
   
class Son extends Father   { 
Son( )    { 
Super(
  );     .}    }                        //this is a call to super class constructor 
class Father extends GrandFather   { 
Father ( )    { 
Super ( );               .}   }                          //jvm will
  checks extends any other class 
class GrandFather   {                               // super class  
 GrandFather( )   { 
//since this
  class is not extending any other class compiler will not create any super(); 
}      } 
 | 
 
Multiple inheritance:- 
Explanation:-
Ø Inside
the Son class if we say super.cultivator();
Ø JVM
will not understands which method has  to
be executed.it leads ambiguity.which will become a complexity in JAVA.Son class
have two super classes.
Ø So,JAVA
will not supports multiple inheritance.
Note:-        1.multiple inheritance and pointers
leads confusion.so,they are removed in JAVA.
1.   In
C++  multiple inheritance is supported by
using friend functions.
Cyclic inheritance:-
So,cyclic inheritance will
not supported any programming language.
Java.lang.Object  :-      “Object” is a class which is a super class
to all the classes in JAVA.
| 
   
Eg:-    class A 
  { 
                            //stmts 
                   } 
 | 
 
Compiler:    It
will checks if A is extending any other class.
               
    If yes it will checks
that, otherwise it will extends Object class.
Fig:-multilevel inheritance
object creation
Method overriding:-
Process of defining a method
with same name,same signature in sub class for a method already exist in its
super class.
We can override methods of
supermost classes also.
We can have 5 classes we can
inheritance hierarchy as follows
According java coding
standards the      hierarchy is limited to
5 classes.    Eg:-
| 
   
class Addition   { 
int sum(int i, int j)  
  { 
int  total = i+j ; 
return total ;               }    } 
class ChildAddition extends Addition  { 
int sum(int a, int b)  
  { 
int total = a+b+100 ; 
return total ;                  } 
public void
  static main(String args[])  
  { 
ChildAddition       childAdd
  = new ChildAddition (); 
int tot = childAdd.sum(7,8); 
System.out.println(tot);               }    } 
 | 
 
Super class referring sub
class object rule:-
We can assign a sub class
object into a super class reference.
Here for super class
reference we can assign only super class non static contents.
Program same as above
public void static
main(String args[])    {
Addition add=new
ChildAddition();
Addition add=new
Addition();     }








No comments:
Post a Comment