Why do we need abstract classes in Java? [closed]
Asked Answered
T

3

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.

Ternary answered 28/2, 2014 at 4:8 Comment(5)
@blunderboy - believe me, it's considerably more gentle than the last time he/she asked.Danner
Not showing much evidence of having read the answers from the last time, but at least it's polite.Tote
@Tote 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.Danner
This is actually a good question. Unfortunately it is closed.Beard
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.Ameba
L
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.

Leprosy answered 28/2, 2014 at 4:13 Comment(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?Ternary
You should accept is as an answer if you are satisfied.Merkley
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 ?Narcotize
T
27

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.

Tote answered 28/2, 2014 at 4:13 Comment(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.Harbison
J
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.

Jerrome answered 28/2, 2014 at 4:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.