Java2all.com  




Share This Topic -

Multilevel Inheritance in java:

Multilevel Hierarchy :

 class student
{
	int rollno;
	String name;

	student(int r, String n)
	{
		rollno = r;
		name = n;
	}
	void dispdatas()
	{
		System.out.println("Rollno = " + rollno);
		System.out.println("Name = " + name);
	}
}

class marks extends student
{
	int total;
	marks(int r, String n, int t)
	{
		super(r,n);   //call super class (student) constructor
		total = t;
	}
	void dispdatam()
	{
		dispdatas();	// call dispdatap of student class
		System.out.println("Total = " + total);
	}
}

class percentage extends marks
{
	int per;
	
	percentage(int r, String n, int t, int p)
	{
		super(r,n,t);  //call super class(marks) constructor
		per = p;
	}
	void dispdatap()
	{
		dispdatam();	// call dispdatap of marks class
		System.out.println("Percentage = " + per);
	}
}
class Multi_Inhe
{
	public static void main(String args[]) 
	{
		percentage stu = new percentage(1912, "SAM", 350, 50); //call constructor percentage
		stu.dispdatap();  // call dispdatap of percentage class
	}
}

 
Output :
 
Rollno = 1912
Name = SAM
Total = 350
Percentage = 50
 
It is common that a class is derived from another derived class.
 
The class student serves as a base class for the derived class marks, which in turn serves as a base class for the derived class percentage.
 
The class marks is known as intermediated base class since it provides a link for the inheritance between student and percentage.
 
The chain is known as inheritance path. 
 
When this type of situation occurs, each subclass inherits all of the features found in all of its super classes. In this case, percentage inherits all aspects of marks and student. 

To understand the flow of program read all comments of program.

When a class hierarchy is created, in what order are the constructors for the classes that
make up the hierarchy called?
 
EX :
 class X 
{
	X() 
	{
		System.out.println("Inside X's constructor.");
	}
}

class Y extends X 	// Create a subclass by extending class A.
{
	Y() 
	{
		System.out.println("Inside Y's constructor.");
	}
}

class Z extends Y 	// Create another subclass by extending B. 
{
	Z() 
	{
		System.out.println("Inside Z's constructor.");
	}
}

public class CallingCons 
{
	public static void main(String args[]) 
	{
		Z z = new Z();
	}
}

 
Output:
 
Inside X's constructor.
Inside Y's constructor.
Inside Z's constructor.
 
The answer is that in a class hierarchy, constructors are called in order of derivation, from superclass to subclass.
 
Further, since super( ) must be the first statement executed in a subclass’ constructor, this order is the same whether or not super( ) is used.
 
If super( ) is not used, then the default or parameterless constructor of each superclass will be executed.
 
As you can see from the output the constructors are called in order of derivation.
 
If you think about it, it makes sense that constructors are executed in order of derivation.
 
Because a superclass has no knowledge of any subclass, any initialization it needs to perform is separate from and possibly prerequisite to any initialization performed by the subclass. Therefore, it must be executed first.