Abstract Classes in Java — quick Overview

Hirunika Karunathilaka
2 min readApr 29, 2018

--

If you are a Java programmer, I’m pretty sure you have heard the term “abstract class”. In this article, I’m hoping to give you a quick overview of abstract classes in Java, just in case to use for an interview or exam.

What does abstraction mean?

Abstraction is a process of hiding the implementation details and showing only functionality to the user.
This abstraction can be achieved using two ways in Java. By using abstract classes and interfaces.Interfaces give a 100% abstraction while abstract classes provide 0%-100% abstraction.

When we should declare a class as abstract?

When the class has one or more abstract methods.

What is meant by an abstract method?

It is a method that has a method heading, but nobody. Simply, an abstract method has no implementation code inside its curly braces as other normal methods do.

How to implement and use abstract classes?

Let’s implement a class named “Animal” and for it to be an abstract class, we need to add at least one abstract method. Let the abstract method named as “getNoOfLegs”. The reason why we can’t implement this method is that we can’t specifically say the number of legs of different animals. So we keep the method as abstract.

abstract class Animal{
abstract int getNoOfLegs (); //no implementation
}

Then, how can we make use of this class? We’ll create two classes and extend this Animal class. Actually abstract classes are meant to be inherited from because it cannot be instantiated.

 abstract class Animal{
abstract int getNoOfLegs (); //no implementation
}
class Dog extends Animal{
int getNoOfLegs (){
return 4;
}
}
class Bird extends Animal{
int getNoOfLegs (){
return 2;
}
}

Create a “Test” class with the main method and run this code to see the output.

class Test{  public static void main(String args[]){    Animal a1 = new Dog();
System.out.println(“No of legs of a dog is ”+a1. getNoOfLegs());
Animal a2 = new Bird();
System.out.println(“No of legs of a bird is ”+a2. getNoOfLegs());
}
}

The output will be,
No of legs of a dog is 4
No of legs of a bird is 2

Some more facts you should know

Above we saw how to make use of an abstract class. Let’s look at some other important facts about these abstract classes.

• An abstract class can have abstract and non-abstract methods, constructors, instance variables as well.
• A non-abstract class is also called as a “concrete class”.
• An abstract class is good if you think you have some other subclasses that this abstract class provides a common base class implementation. Like, you have rectangle, circle and square classes, you can create a class as Shape and make it as abstract class.
• If you think you will need to add methods in the future, then an abstract class is a better choice.
• If you are extending an abstract class that has an abstract method, you must either provide the implementation of the method or make this class abstract.

I hope you enjoy the article. Happy coding!!! ☺

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Hirunika Karunathilaka
Hirunika Karunathilaka

No responses yet

Write a response