You Will Never Struggle With Java Abstract Class Again!

Abstract Class In Java: Make Sense!

Kossei Necira
JavaToDev
5 min readSep 3, 2024

--

Many Java developers, especially beginners, struggle to fully grasp what an abstract class is in Java and when it should be used. In this article, I’ll guide you through a fascinating journey to help you understand this important topic.

To understand abstract classes in Java we need to understand what is “Abstraction” in software engineering first.

What is Abstraction in software engineering:

The word “abstraction” is a widely used term and a key concept in the field of software engineering.

Abstraction is reducing sophistication while keeping the outlines of something.

I see that face!! I knew it! you would say: nah! , not again! , I am not a man of definitions!

Your face after hearing something theoretical again! :)

Reading this little story, in which you’re part of the narrative, definitely will make understanding much easier!

Let’s say you want to build a house. You’ve already imagined what it would look like, so you take a piece of paper and start sketching your dream house. But, since you’re not the best artist (just like me :3), you end up with something like this:

My artwork All copyright reserved :), you don’t need to compliment me, I know that I am the top artist haha

You take that piece of art to a builder.

You: Hello! Mr. builder.

The builder: Hi Mr. Ms. Reader :).

You: I want you to build me that home!

The builder’s expression was like :

builder expression after hearing what you have just said

and you were like:

You getting embarrassed :)

The builder said: This is an abstract design (a blueprint), I need a more detailed design from an expert home designer and luckily we have one on our team.

after a few days, he brings you the design to take a look at :

May this is your dream home for real :)

Now, if we compare the two pictures, we’ll notice that the first one has a lot of details removed compared to the second one. In conclusion, we can say that the first picture is an abstraction of the second one. If we start with the second picture and apply abstraction mechanisms, we would end up with the first one. Essentially, the more details you include, the less abstract the representation becomes.

by the end of this story, you should have a good idea about abstraction in software engineering.

What is an abstract class in Java:

To answer this question fully, it’s essential to understand what a class and object in Java are. I recommend reading my previous article on this topic, where I dive into the details from a unique perspective you may not have encountered before. This will help you grasp the concept more comprehensively, almost like appreciating a complete artwork!

The next step is making the two concepts together “abstract” + “class”.

basically, it means a class with fewer features or details :).

but wait a minute! what do we mean by that?

to understand that we need to know abstract class characteristics:

Abstract class properties in Java:

  1. Can not be instantiated
  2. May have abstract methods.

An abstract class can not be instantiated: meaning you can’t create an object directly from it. Instead, it can only be inherited by a subclass, and if you try to, a compile-time error will appear by your IDE. In this property, we already achieved the goal of abstraction by restricting the instantiation feature of the class.

An abstract class may have abstract methods: abstract methods are just methods with signature only (name and return type) and without a body, letting subclasses define it. We achieved abstraction here by omitting the method body, we can also say that we provide a blueprint for something more complicated.

Now, let’s look at a real-world example to bring together all the puzzle pieces we’ve discussed so far into a complete picture.

Abstract class example in Java:

The below code example takes Animals as an example to demonstrate the abstract class theory.

let’s take a look at this code:

public abstract class Animal {
// defining common properties
// all animals have name and species
private String name;
private String species;

// defining constructor
public Animal(String name, String species) {
this.name = name;
this.species = species;
}

// defining behavior
// all animals eat.
public void eat() {
System.out.println("eating...");
}

// defining abstract method:
// all animals have different sound
public abstract void makeSound();

// all animals move, but in different ways.
public abstract void move();

}

class Chicken extends Animal {


public Chicken(String name, String species) {
super(name, species);
// TODO Auto-generated constructor stub
}

@Override
public void makeSound() {
System.out.println("Cluck! Cluck!");
}

@Override
public void move() {
System.out.println("Walk! Walk!");
}
}

class Whale extends Animal {

public Whale(String name, String species) {
super(name, species);
}

@Override
public void makeSound() {
System.out.println("Click! Click!");
}

@Override
public void move() {
System.out.println("Swim! Swim! ....");
}

}

When to use abstract class in Java:

We use an abstract class to define a blueprint for other sub-classes, it helps us to define a hierarchy of one super-class on the top and different sub-classes on the bottom, the subclasses have common attributes, but in some cases may differ in behaviors.

in the previous example, we used the Animal abstract class to define a hierarchy, the subclasses have common attributes like name and species but differ in some behaviors, which is in our case the “makeSound” and “move” method, the implementation of that method differs from one class to another.

Conclusion:

I hope that you enjoyed the journey to the end of this article. Starting with understanding what is an abstraction in software engineering.

After that, we moved to grasp what is an abstract class in Java by understanding and combining the two terms to make sense, finally, we talked about a good practical example in Java that demonstrates the theoretical part.

If you enjoyed my article, don’t forget to clap as much as you can! 😊 And be sure to follow me to stay updated with my latest articles. Feel free to ask about any Java technology you’d like me to write about in the comments section!

--

--