It seems that an abstract class means the definition of the class is not complete and hence cannot be instantiated.
And I saw some simple Java code which has an abstract class with all of the methods defined. Then I was wondering, why do they make it as an abstract class instead of a real class? Are they doing this so we cannot instantiate from this abstract class? Or they are getting other benefits from making an abstract class with everything defined?
It's not necessary for an abstract class to have abstract method. We can mark a class as abstract even if it doesn't declare any abstract methods. If abstract class doesn't have any method implementation, its better to use interface because java doesn't support multiple class inheritance.
The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.
An abstract class is a class that is declared abstract —it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class.
If a class is declared abstract, it cannot be instantiated. To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it. If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.
It is possible that even though all the methods had a default implementations, these implementations weren't actually meaningful in the context of the application. The methods might only do internal bookkeeping while the actually useful implementation must be provided by a derived class which does what it needs to do and then calls the superclass method.
However, this is just speculation. You would have to show an actual example to tell what could be the reason for this design.
As an example, let's take a simple game engine. I have lot's of different GameObject
s in my game.
Some are visible, so my basic class gets a draw()
method. But there might be invisible objects like trigger areas which don't show up at all, so I implement it as a no-op in the base class.
Some do something when they collide with something, so each one gets a collide(other)
method. But some don't do anything when they collide like a purely visual particle effect, so I also provide a no-op in the base class.
Some do something every game tick, so they get a update()
method. But some objects, like a wall, might not do anything at all on their own. So I also provide a no-op for this.
So what do I do when I have an object which is invisible, doesn't do anything on its own and doesn't interact with anything? There is no reason to have this in the game. So I make this basic class abstract
. Theoretically you could instance it because all methods have an implementation, but practically you have no reason to ever do this, and when you try, you misunderstood how my game engine works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With