Java class inheritance

Inheritance is one of the most crucial features of any object-oriented programming language. It gives an opportunity to design software in a hierarchical manner. In simple terms, inheritance is deriving or inheriting a class from another class. The derived class is called subclass and parent class is called superclass or base class. When we perform inheritance, subclass inherits all the properties of its superclass, making the sub class more powerful than its parent, the superclass. Subclass can access data members and methods of a superclass. This enables us to create a generic parent (super) class and then create specialized subclasses.

In Java, we can derive one class from another class by using extends keyword. Let us write a class called Shape which is a generic base class for all shapes such as triangle and rectangle. This Shape class contains width and height as data members and a method showDimensions as shown below,

class Shape {
	
	double width;
	double height;	
	void showDimensions() {
		System.out.println("Width: " + width + ", Height: " + height);
	}	
}

Now create a Rectangle class which is derived from Shape class. This Rectangle has one extra data member called style, to indicate whether it is filled or outlined. Also a method called showStyle() is created to display its style as shown below:

class Rectangle extends Shape {
	String style;
	
	public void showStyle() {
		System.out.println("Style: " + style);
	}
}

In our main method let us create an object of Rectangle class and call showStyle and getDimensions methods. This is how we can access superclass members from a subclass.

/*
This is a simple Java program about Inheritance.
Call this file KH_Inheritance.java.
*/
class Shape {
	
	double width;
	double height;
	
	void showDimensions()
	{
		System.out.println("Width: " + width + ", Height: " + height);
	}	
}

class Rectangle extends Shape {
	String style;
	
	public void showStyle() {
		System.out.println("Style: " + style);
	}
}

class KH_Inheritance {
	public static void main(String args[]) {
		Rectangle ob = new Rectangle();
		ob.width = 10;
		ob.height = 6;
		ob.style = "outlined";
		
		ob.showDimensions();
		ob.showStyle();		
	}
}

Download the code Run the code

Output:

Width: 10.0, Height: 6.0
Style: outlined

One important point to note: Unlike some other object oriented languages such as C++, we can not derive a subclass from multiple superclasses. We can derive a subclass from only one superclass.

Member access in inheritance

In the previous program, all data members and methods are public, hence, we were able to access them from the subclass. If we change the members width and height to private then we can not access them from the subclass. This is a very important feature which allows us to hide superclass’s data and make is unavailable to subclasses..

So then how to access these data members? If your design needs you to give access to these private data members then you need to create support functions such as getWidth and setWidth to the superclass. In the below modified program, we have created two methods for width and height to set and get values.

/*
This is a simple Java program about Inheritance.
Call this file KH_InheritanceAccess.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 void showStyle() {
		System.out.println("Style: " + style);
	}
}

class KH_InheritanceAccess {
	public static void main(String args[]) {
		Rectangle ob = new Rectangle();
		ob.setWidth(10);
		ob.setHeight(6);
		ob.style = "outlined";
		
		ob.showDimensions();
		ob.showStyle();
		
	}
}

Download the code Run the code

Output:

Width: 10.0, Height: 6.0
Style: outlined
<<< Java class methodsJava abstract classes and abstract methods >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .