Reputation: 4917
An abstract class need not include any abstract methods.
Is there any other reason to make a class abstract other than the fact that abstract classes can't be instantiated?
Upvotes: 14
Views: 14380
Reputation: 68905
There cannot be an abstract function in an non abstract class!
So if you need an abstract function in your class for whatever reason you must declare your class to be abstract.
Why would I need an abstract function?
What is the point of using abstract methods?
If an abstract class may not include any abstract method. Than is there any other reason to make class abstract except of abstract class can't be instantiated
As for your question a scenario I can think of is as follows -
Lets say all you need is static functionality of a class. Static variables and function. So basically you don't want users to create any instance of your class. Now you can make your constructor private thereby ruling out the possibility of creating it's instance but you don't want to do that either. Why write additional lines of code? In this case you can make your class abstract.
Upvotes: 3
Reputation: 21
Abstract classes serve as templates for their subclasses. For example, the abstract class Tree and subclass, Banyan_Tree, has all the characteristics of a tree as well as characteristics that are specific to the banyan tree.
Understanding the differences between an abstract class and an interface is essential. An interface only has method declarations or abstract methods and constant data members, while an abstract class may have abstract methods, member variables and concrete methods. Because Java only supports single inheritance, a class can implement several interfaces but can extend only one abstract class.
Upvotes: 1
Reputation: 3186
I have extracted below points from Why should I declare a class as an abstract class?
Abstract classes improve the situation by preventing a developer from instantiating the base class, because a developer has marked it as having missing functionality.
It also provides compile-time safety so that you can ensure that any classes that extend your abstract class provide the bare minimum functionality to work, and you don't need to worry about putting stub methods that inheritors somehow have to magically know that they have to override a method in order to make it work.
Upvotes: 2
Reputation: 8473
You can make a class as abstract ,even if you don't want to implement all of the interface methods that the class implements. According to java docs.
It was noted that a class that implements an interface must implement all of the interface's methods. It is possible, however, to define a class that does not implement all of the interface methods, provided that the class is declared to be abstract.
Any way you can't instantiate a class that declared with abstract.
Upvotes: 4
Reputation: 1209
The principal role of an abstract class is to provide an appropriate root class from which concrete, (i.e. non-abstract) subclasses can be derived. This is a powerful and versatile feature which promotes code re-use. Abstract classes encapsulate general features that are common to a range of data types - features which are too general to be meaningful in the abstract class, but which can be overridden in a subclass
Any class with an abstract method is automatically abstract itself and must define itself as such with the keyword abstract - interestingly, an abstract class need not contain any abstract methods
An abstract class cannot be instantiated - in other words you cannot create instances (objects) of an abstract class
References to objects of an abstract class can be declared even though objects of abstract classes cannot be instantiated, e.g Account a ; will not generate a syntax error
If a subclass of an abstract class overrides, i.e. provides an implementation of every abstract method in its superclass, the subclass is called a concrete class and objects of the subclass can be created
If a subclass of an abstract class does not override (implement) all of the abstract methods it inherits, that subclass itself is also abstract and must be declared as such
Upvotes: 6
Reputation: 7804
Abstract class means the definition of the class is not complete and hence cannot be instantiated. Even though it does not have abstract method, it is an indicator that the class is available for inheritance. Even though it has implementation for all the methods in it, the implementation may still not be complete and must be overridden by the extending class.
Upvotes: 12
Reputation: 14164
Probably not. It would prevent a "static helper" class from being instantiated, but I think using it that way would be poor style.
As others say, if it's intended to be a base class -- with descendants declaring & implementing interfaces/ functionality, which is too specific for the superclass -- then abstract
could be appropriate in the superclass.
In that case it's a declaration that the super is not useful on it's own, and that subclasses will do something more -- which they will specifically declare.
Upvotes: 4