Java abstract classes and abstract methods

Sometimes we need to create a generalized superclass that defines few general methods and basic data members to be used and implemented by its subclasses. These generalized methods (also called abstract methods) will not have any function body.

To understand clearly remember our Shape class. This class is the general class for all shapes like triangle and rectangle etc. Every shape will have its area and we need to calculate the area of each shape. As you know calculation of area differs from shape to shape that is, formula to calculate the area of a rectangle is different from the triangle.

We can not defined the body of the area() method in Shape class, but we want to force each subclass of Shape class to implement this area() method. How do we force? Can we make a rule? If we did not make a rule then it is up to subclass programmer to create an area() method or not. This is not our intention.

This rule can be achieved by abstract keyword. And the method which does not have a body is called the abstract method. To define abstract methods below is syntax,

abstract returntype method_name(parameterlist);
  • If a class has an abstract method then that class also becomes abstract and we need to add abstract keyword before class keyword at the definition.
  • We can not create an instance (object) of an abstract class. i.e. the abstract classes are not instantiable.

below is updated Shape class:

/*
This is a simple Java program about Inheritance.
Call this file KH_abstract.java.
*/
abstract 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);
	}	
	abstract double area();	// abstract method 
}

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_abstract {
	public static void main(String args[]) {
		Rectangle ob = new Rectangle("outlined", 10, 5);
		
		ob.showDimensions();
		ob.showStyle();		
	}
}

Download the code Run the code

Now we have changed our Shape class and made it abstract, now it is time to modify our Rectangle class. If you try to compile without modifying Rectangle class compiler throws an error. If you look at below error it is clear that we need to implement area method in Rectangle class.

KH_abstract.java:45: error: Rectangle is not abstract and does not override abstract method area() in Shape
class Rectangle extends Shape {
^
1 error

Now implement area method in Rectangle class and run it and it runs successfully. This how we can use the abstract keyword to create generalized classes.

/*
This is a simple Java program about Inheritance.
Call this file KH_abstract1.java.
*/
abstract 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);
	}	
	abstract double area();	
}

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);
	}	
	double area(){
		return getHeight() * getWidth();
	}
}

class KH_abstract1 {
	public static void main(String args[]) {
		Rectangle ob = new Rectangle("outlined", 10, 5);		
		ob.showDimensions();
		ob.showStyle();		
		System.out.println("Area is: " + ob.area());		
	}
}

Download the code Run the code

Output:

Width: 10.0, Height: 5.0
Style: outlined
Area is: 50.0
<<< Java class inheritanceJava final classes, final methods and final data members >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .