Subscribe To MegiSoft RSS Feeds!     Your Browser OR  Google Feedburner

Java Abstract Classes Using Practical Exercises

Abstract classes don’t seem to be useful when you hear for the first time about them. The reason is that you can’t instantiate them and their methods are abstract. In this article, you find an explanation with exercises using an example from the real world.

 

What is an abstract class in Java?

An abstract class in Java is a class in which one or more methods have no body or not defined, but only declared. It is important to know that abstract classes can not be instantiated. The following rules apply to abstract classes.

Article: Java abstract classes
Quizzes: Beginner  Intermediate  Advanced 

abstract-classes-color

Abstract classes:

  • If a class is abstract, it cannot be used to create objects.
  • They are mainly used as super classes.
  • It has one or more abstract methods.
  • An abstract method is a method without a body, which are not defined.

 

Sub classes of abstract classes:

  • It is possible to instantiate a sub class that extends an abstract class.
  • A sub class of an abstract class must override all the abstract methods.
  • It is not mandatory to have any abstract methods inside an abstract class.
  • It is allowed in Java to declare a subclass of an abstract class also abstract.

 

Why do you need abstract classes?

An example of abstract classes is a Shape class. Instantiating a Shape class doesn’t make sense, because you can’t do anything with it. We know that every two-dimensional shape has an area and a perimeter, but there is no mathematical formulas to calculate them. That is why we leave all the methods of the Shape class without a body and declare them abstract as shown below:

 

protected abstract double getArea(double length);

 

Once we know that the shape is actually a circle or a square, then it is useful to create objects of those shapes. We can exactly calculate the areas or perimeters of circles and squares. That is also the reason that we write exact methods to do that for us, but doesn’t seem creating a class Shape redundant?  And why do we need it? If you create a class Shape and you declare inside it two abstract methods like getArea() and getPerimeter() you ensure that any classes extend the class Shape must have those two methods.

 

The method getArea of the Square class doesn’t have to be abstract, because we know how to calculate the area of a square. See the code below:

 

public double getArea(double side){
     return side * side;
}

 

If you don’t implement those two methods in your classes, you must declare your subclass also abstract. That means that your subclass cannot be instantiated either. If a building is not finished, you don’t allow people to live inside it. That principle applies to abstract classes too. When a class circle doesn’t have a method to calculate its area and perimeter, then it is better not to allow anyone to use it.

 

The UML diagram – inheritance of an abstract class

In the UML diagram below, the classes Circle and Square extends the Shape class and they inherit the methods getArea and getPerimeter from the abstract class Shape. This kind of relationship called the “is-a” relationship, because circles and squares are also special types of shapes.

 

uml_inheritance_abstract_is_a_relationship

Conclusion:

The shape example makes it clear that it doesn’t make sense to create the object shape. The reason is that you cannot do anything with it, but as soon as you know that the shape is a square, circle or a triangle then you can calculate exactly their areas and perimeters. This example clarify also why the methods of Shape are abstract and they do nothing. That is because there is no mathematical formulas to calculate the area of a shape in general without knowing which type of shape exactly you mean.

 

Example: abstract classes

In the following example, we declared the two methods getArea and getPerimeter abstract. We override each of the two methods in the classes Circle and Square. Remember that each one applies its own mathematical formula.

 

public abstract class Shape {

	protected abstract double getArea(double length);
	protected abstract double getPerimeter(double length);
}

public class Circle extends Shape {

	static final double PI = 3.14;

	public double getArea(double radius){
		return radius * radius * PI;
	}
	public double getPerimeter(double radius){
		return PI * 2 * radius;
	}
}

public class Square extends Shape {

	public double getArea(double side){
		return side * side;
	}
	public double getPerimeter(double side){
		return side * 4;
	}
}

 

Exercise 1

Which one of the following statements is true?

public abstract class Shape {

	protected abstract double getArea(double length);
	protected abstract double getPerimeter(double length);
}

public class Square extends Shape {

	public double getArea(double side){
		return side * side;
	}
	public double getPerimeter(double side){
		return side * 4;
	}
	public String getColor(){
		return "blue";
	}
}

public class TestProgram {

	public static void main(String[] args){
		Square s = new Square();
		System.out.print(s.getArea(3));
		System.out.print("," + s.getPerimeter(6));
		System.out.print("," + s.getColor());
	}
}

Select the correct answer.



Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

Exercise 2

Which of the following statements are true?

public abstract class Shape {

	protected abstract double getArea(double length);
	protected abstract double getPerimeter(double length);
}

public class Square extends Shape {

	public double getArea(double side){
		return side * side;
	}
	public abstract double getPerimeter(double side);
}

public class TestProgram {

	public static void main(String[] args){
		Square s = new Square();
		System.out.print(s.getArea(4));
	}
}

Select all the correct answers.



Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

Megisoft.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes. The author of Megisoft is Sar Maroof who has more than a decade of experience with web development mainly with Java, MySql, HTML and more..
Article: Java abstract classes
Quizzes: Beginner  Intermediate  Advanced 

Suggested Articles:


Click Here For Info about: Sar Maroof's New Java Book
Author: Sar Maroof
More than a decade of experience with developing web applications mainly with Java.
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster - Mind IT
Applied Science in Physics HBO Amsterdam
Bachellor Science in Physics at Basra university.
 Subscribe To Megisoft RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!
Share on FacebookTweet about this on TwitterShare on RedditShare on LinkedIn

1 Comment

Add a Comment
  1. I like to party, not look arlcties up online. You made it happen.

Leave a Reply

megisoft © 2016