Multilevel Inheritance and Referencing

Multilevel Inheritance

Till now we have seen creating a subclass from a superclass. Java also supports creating a new subclass from another subclass. This new class inherits properties of the immediate superclass and its grand superclass. For example, let’s derive a new colorReactangle class from Rectangle class. This new class holds properties of Rectangle class style and Shape class width and height. Also, this can have its own properties such as color.

Below program demonstrates this.

/*
This is a simple Java program about Inheritance.
Call this file KH_MultilevelInheritance.java.
*/
class Shape {
	
	private double width;
	private double height;
	
	public Shape(double width, double height){
		this.width = width;
		this.height = height;
	}
	double getWidth(){
		return width;
	}
	void setWidth(double w){
		width = w;
	}
	double getHeight(){
		return height;
	}
	void setHeight(double h){
		height = h;
	}
	void showDimensions(){
		System.out.println("Width: " + width + ", Height: " + height);
	}
}

class Rectangle extends Shape {
	String style;
	public Rectangle(String style, double width, double height){
		super(width, height);
		this.style = style;
	}
	public void showStyle() {
		System.out.println("Style: " + style);
	}
}

class ColorRectangle extends Rectangle {
	String color;
	public ColorRectangle(String color, String style, 
               double width, double height) {
		super(style, width, height);
		this.color = color;
	}
	public void showColor() {
		System.out.println("Color: " + color);
	}
}

class KH_MultilevelInheritance {
	public static void main(String args[]) {
		ColorRectangle ob = new ColorRectangle("Red", "outlined", 10, 5);
		ob.showDimensions();
		ob.showStyle();
		ob.showColor();
	}
}

Download the code  Run the code

Output:

Width: 10.0, Height: 5.0
Style: outlined
Color: Red

Referencing

As we already know Java is strongly typed language. This means that one type of object can not refer to another type. For example, create two classes X and Y. In the main method create new objects x and y for each X and Y class. And create a new object x2. Now try to refer x2 to x and x2 to y. The first case x2 to x is valid because these two are of the same type. But second case x2 to y is not valid because they both are of different types.

/*
This is a simple Java program about Inheritance.
Call this file KH_ReferencingInheritance.java.
*/
class X {
	void showX(){
		System.out.println("Class X");
	}	
}

class Y {
	void showY(){
		System.out.println("Class Y");
	}	
}

class KH_ReferencingInheritance {
	public static void main(String args[]) {
		X x = new X();
		Y y = new Y();
		
		X x2;
		
		x2 = x;
		x2.showX();
		
		x2 = y;
		//x2.showY();//Error as x2 is not of type class Y
	}
}

Download the code  Run the code

Output:

KH_ReferencingInheritance.java:31: error: incompatible types: Y cannot be conver
ted to X
                x2 = y;
                     ^
1 error

However, this is slightly exempted in case of inheritance. Where an object of type superclass reference can be assigned to a subclass object. In above program lets derive class Y from class X. Now x2 to Y is valid because x2 is of type superclass and y is of type subclass.

But there is one important point to note is that this reference is not full type reference, in this case, it is limited to what members can be accessed. This x2 can only access the members of y which are inherited from superclass X. That is x2 can not access members of subclass Y. Why is this behavior? Because superclass reference x2 does not know anything about the subclass. x2 only knows that the referred object has its own type and it maps to those own types and does not map to subclass members. For example below the program which illustrates this, try to uncomment the line where x2 accesses the member of class Y and see how our program behaves.

/*
This is a simple Java program about Inheritance.
Call this file KH_ReferencingInheritance1.java.
*/
class X {
	void showX(){
		System.out.println("Class X");
	}	
}

class Y extends X{
	void showY(){
		System.out.println("Class Y");
	}	
}

class KH_ReferencingInheritance1 {
	public static void main(String args[]) {
		X x = new X();
		Y y = new Y();
		
		X x2;
		
		x2 = x;
		x2.showX();
		
		x2 = y;
		x2.showX();
		//x2.showY();
           //Error as x2 is of type class X, so it can only 
           // access members of class X	
	}
}

Download the code  Run the code

Output:

Class X
Class X

Output when you uncomment line: x2.showY()

KH_ReferencingInheritance1.java:32: error: cannot find symbol
                x2.showY();//Error as x2 is not of type class Y
                  ^
  symbol:   method showY()
  location: variable x2 of type X
1 error
<<< Method Overriding and Run Time PolymorphismJava interfaces >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .