19

Why do we need abstract classes in Java? If you're never going to make it into an object, why have it in the first place? How do you use it? Why is it there?

I'm wondering the same thing with abstract methods. I find it seems like a similar concept to having a super class without any subclasses that could ever matter to be made.

5
  • 4
    @blunderboy - believe me, it's considerably more gentle than the last time he/she asked. Feb 28, 2014 at 4:13
  • 1
    Not showing much evidence of having read the answers from the last time, but at least it's polite.
    – keshlam
    Feb 28, 2014 at 4:15
  • @keshlam It may have been deleted before he/she had the opportunity to read them. I edited the question just as it was being deleted, in the hope of saving it, but I was too slow. Feb 28, 2014 at 4:46
  • This is actually a good question. Unfortunately it is closed. Oct 31, 2015 at 4:52
  • SO is a great place when you have a specific question about a specific chunk of code. It is an unforgiving abyss when you have questions about the "whys" or the overall concept or you're just in need of direction.
    – Tim
    Aug 18, 2022 at 19:55

3 Answers 3

37

An abstract class can be used as a type of template for other classes. The abstract class will hold common functionality for all classes that extend it.

For example:

Abstract Class Animal 

All animals move and breathe and reproduce so these can be put into the Animal Class.

Now

 Concrete Class Dog, Cat etc.

Have these base functions already provided.

3
  • That's a nice and simple explanation. I like that answer. So you CAN use the SUBCLASSES of an abstract class. Is it the same for abstract methods or are there no such things as sub methods? Feb 28, 2014 at 4:55
  • 1
    You should accept is as an answer if you are satisfied.
    – nafizh
    Jan 3, 2016 at 8:47
  • But if to declare for example "breathe()" on abstract "Animal" and even "breathe()" is just declared (without "body" of function) and so we'll need re-create "breathe()" in "dog" and "cat", what is benefits of "Animal" so ?
    – Sushi
    Nov 27, 2017 at 17:09
26

Abstract classes permit providing a partial set of default implementations of methods in a class. Since they're incomplete, they can't be instantiated and used as they stand, but they can be subclassed to add the missing details in a way that's specific to that particular implementation, and those subclasses can be instantiated.

Without abstract classes, you would have to provide dummy implementations of the methods you intend to override ... which could be done, but then there'd be the risk of forgetting to implement one of them. Having some methods remain entirely abstract ensures that the real implementations have to fill in the gaps, or continue to be abstract themselves and force their descendents to do so.

It's not something the language couldn't live without. But it's very useful. You'll discover just how useful as you become more proficient in Java and OO design.

1
  • can you please give a example where we need not to write dummy implementation for abstract class with another class where we need to write the dummy implementation. I am confused and wish if you can explain and elaborate it. May 22, 2018 at 5:53
16

There are many uses of abstract classes. The main purpose of abstract classes is to function as base classes which can be extended by subclasses to create a full implementation.

For example, you may have three steps to be implemented in your program:

  • A few steps before the action
  • Some action to be performed
  • A few steps after the action

So in this case you can define an abstract class with the three methods like this:

public abstract MyAbstractProcess {
    public void stepBefore() {
        // Implementation directly in abstract superclass
    }

    public abstract void action(); // Implemented by subclasses

    public void stepAfter() {
        // Implementation directly in abstract superclass
    }
}

Also, the above example of abstract class Animal is also a very good example.

Not the answer you're looking for? Browse other questions tagged or ask your own question.