Podcast Title

Author Name

0:00
0:00
Album Art

A Clear Guide to Abstract Classes in Java

By 10xdev team August 03, 2025

Abstract classes can be a really confusing concept when you're learning Java. When many of us were learning, they were pretty confusing. But in this article, we're going to go over exactly what an abstract class is, how you make one, how you can use one with an example, and why you would want to make one in the first place. We'll also talk about the differences between abstract classes and interfaces.

What is an Abstract Class?

First things first, what is an abstract class? All an abstract class is, is a class that you can't instantiate. You can't create objects from abstract classes. So what does that mean? Well, you know if you have a normal class, like a Cat class, you can create objects of that class.

For example, you could write: java Cat myCat = new Cat();

But with abstract classes, you can't do this. So if we go into the Cat class and make it a public abstract class, and then go back into the main method, we can see that we now get an error: cannot instantiate the type Cat. And by the way, this abstract keyword in the class definition is the only thing you need to make a class into an abstract class.

Why Use an Abstract Class?

Probably the first question that comes to mind is, why would I want to do that? Why would I want to make a class that I can't create objects from?

Here's a situation where it makes sense to use an abstract class. Let's change this class back into a regular class. Instead of just being a standalone class, let's say it extended a class called Animal. And let's say the Animal class had several fields, like int age and String name.

It totally makes sense that you could have an Animal class that would have subclasses of actual animals like Cat, Dog, Horse, whatever. And it makes sense that you could create objects of those subclasses, like you could create a Cat object. But what doesn't make a whole lot of sense is creating an Animal object. You know, it's just kind of weird. What kind of animal is it?

So you might want a parent class like Animal so that your subclasses can share some fields, like age and name, and maybe some methods that you write, but you might not want to be able to create objects of this Animal class. All you have to do is make this class abstract.

So an abstract class is a class you can't instantiate, but you can absolutely make subclasses of an abstract class that can be instantiated.

Understanding Abstract Methods

In any of your abstract classes, you can choose to have abstract methods. Let's say we had a method like public void makeNoise(). Of course, it makes sense for an animal to be able to make noise, but each individual animal is going to make noise in its own way. A cat's going to meow, a dog is gonna bark.

Because of that, it might not make a whole lot of sense to actually implement this makeNoise method here in your abstract Animal class. What you can do is make this method an abstract method. When you make a method abstract, you don't specify a body for the method. All you do is declare it and then end it with a semicolon.

public abstract void makeNoise();

But then, in all the child classes of your abstract class, you have to actually create an implementation of this makeNoise method. So if we go back over to our Cat class now, we can see that it's giving us an error that The type Cat must implement the inherited abstract method makeNoise.

Of course, we can write it all out, but we can also just use an IDE's fix tool to add the unimplemented method. Now, this Cat class can implement this makeNoise method however we want.

@Override
public void makeNoise() {
    System.out.println("Meow");
}

Because this Animal class declares an abstract method makeNoise, any child class of this Animal class has to provide its own makeNoise implementation. So now back in our main method, we could take myCat and call makeNoise(), and if we run our program, we can see that it says "Meow".

Now, of course, we could, in our Animal class, get rid of this abstract method and, in our Cat class, just declare a makeNoise method that says "Meow". And if we go back and run our program again, it still says "Meow". So why do we need this abstract method? What do we want that for?

What the abstract class does as a whole is enforce and organize exactly what every subclass of Animal has to have. So this Animal class is saying, "Hey, if you want to create a new type of animal, it's going to have an age, it's going to have a name, and it has to be able to make noise." Every type of animal might make noise in completely different ways, but this makes sure that every single animal type is able to make noise.

As a side note, though, in your abstract class, all of the methods don't necessarily have to be abstract. You can create actual concrete, implemented methods in your abstract classes. So you could have something like:

public void printName() {
    System.out.println("My name is " + name);
}

Now, every subclass of Animal will also have this printName method available to it. But since it's not abstract, they don't need to implement it themselves; they can just use the implementation that's here.

Abstract Classes vs. Interfaces: Key Differences

The question a lot of people have, and what's actually also a big interview question, is what's the difference between an abstract class and an interface?

Let's say we had an interface called AnimalStuff. Here in an interface, we could say, how about public void poop(). Just like every animal might make noise in different ways, every animal is probably also going to poop in different ways. In interfaces, you don't need an abstract keyword in your methods; every method in an interface is assumed to be abstract.

As you probably know, if you want to implement an interface, all you have to do is, instead of extending another class, you just say implements the interface, which is AnimalStuff. And now that we implement this AnimalStuff interface, we have to implement this poop method. You see right now we get an error that says The type Cat must implement the inherited abstract method poop. We can just add the unimplemented method.

@Override
public void poop() {
    System.out.println("...Sounds pretty poopy.");
}

So just like this abstract makeNoise method made any subclass implement this makeNoise method, this interface also makes any classes that implement the interface implement this method. They seem like they're doing the same thing, right? What's the difference?

The first key difference is that you can implement as many interfaces as you want in Java. There's no limit. But you can only extend one class. And you can totally do both if you want to. You can extend the Animal class and implement the AnimalStuff interface.

Another difference is that in interfaces, if you declare any fields, like if you had int age and String name, every field that's declared inside an interface is going to be static and final. That's why you would get an error if you didn't initialize it; because it's final, you have to instantiate it with some kind of a value.

Because every field in an interface is automatically static, that means the same values apply to every object in that class. If you say age = 1, that would have to apply to every class that implements this interface. So it really doesn't make a whole lot of sense to have fields like this inside interfaces.

That's why we have abstract classes like this. So you can say, "Hey, every animal has to have an age and a name," but you're not going to specify what it is right now because that doesn't make sense. Each individual Cat object, Dog object, and Horse object can all have their own age and name values. So you can do that with an abstract class, but not with an interface.

When to Use Each

So you might want to create an abstract class if you have numerous closely related classes that you want to have the same functionality and the same types of fields available. But you might want to make an interface instead if you have various unrelated classes that you all want to be able to do a certain thing. That makes it so you can guarantee that other types of classes will be able to perform a certain action, even if they aren't animals.

Join the 10xdev Community

Subscribe and get 8+ free PDFs that contain detailed roadmaps with recommended learning periods for each programming language or field, along with links to free resources such as books, YouTube tutorials, and courses with certificates.

Recommended For You

Up Next