Access superclass constructor and superclass members

As we already know that constructors are used for creating objects. Even if we did not specify a constructor then Java creates a default constructor. Hence, superclass and subclass both will have either programmer defined constructor or default constructor. Then which constructor is responsible for creating an object of the subclass?

The answer is, superclass constructor is responsible for creating superclass portion of object and subclass constructor is responsible for creating subclass portion of the object.

Our previous Shape program completely relies on default constructors. Now we have created a constructor in Rectangle class. This constructor initializes data members of Shape class.

/*
This is a simple Java program about Inheritance.
Call this file KH_InheritanceConstructor.java.
*/
class Shape {
	
	private double width;
	private double 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){
		this.style = style;
		setHeight(height);
		setWidth(width);
	}	
	public void showStyle() {
		System.out.println("Style: " + style);
	}
}

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

Download the code  Run the code

Output:

Width: 10.0, Height: 5.0
Style: outlined

What if both superclass and subclass both have constructors defined? How to call superclass constructor? The answer is the super keyword.

Use super( ) to call superclass Constructor

To call superclass constructor from subclass constructor we can use super keyword and its syntax is given below,

super(parameterlist);

Here parameter list denotes parameters are needed by superclass constructor. Most important point to remember is that the first statement in subclass constructor is the super keyword. The updated Shape class and Rectangle classes are given below.

/*
This is a simple Java program about Inheritance.
Call this file KH_InheritanceConstructorSuper.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 KH_InheritanceConstructorSuper {
	public static void main(String args[]) {
		Rectangle ob = new Rectangle("outlined", 10, 5);
		ob.showDimensions();
		ob.showStyle();
	}
}

Download the code  Run the code

Output:

Width: 10.0, Height: 5.0
Style: outlined

Use super. to access superclass members

In most of the cases, we need to access members of superclass from the subclass, in this case, we can use super keyword to access them in the same we use this keyword to access members of the same class. To get clear understanding on this, let’s create Parent class with one integer data member i. And derive a new Child class from class superclass Parent. Now Child class is also having an integer data member i. Now both Parent and Child classes have a data member i, i in Child class hides i in Parent class. So how to access Parent class’s i. This can be achieved by using super keyword. Below program illustrates this concept.

/*
This is a simple Java program about Inheritance.
Call this file KH_superkeyword.java.
*/
class Parent {
	int i;
}

class Child extends Parent {
	int i;
	public Child(){
		this.i = 10; //Child class i
		super.i = 20; //Parent class i
	}
	void display(){
		System.out.println("Child  class i: " + this.i);
		System.out.println("Parent class i: " + super.i);
	}
}

class KH_superkeyword {
	public static void main(String args[]) {
		Child ob = new Child();
		ob.display();
	}
}

Download the code  Run the code

Output:

Child  class i: 10
Parent class i: 20
<<< Java final classes, final methods and final data membersMethod Overriding and Run Time Polymorphism >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .