Use of an abstract class without any abstract meth

2019-01-26 05:30发布

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?

7条回答
小情绪 Triste *
2楼-- · 2019-01-26 05:34

I have extracted below points from Why should I declare a class as an abstract class?

  1. Abstract classes improve the situation by preventing a developer from instantiating the base class, because a developer has marked it as having missing functionality.

  2. 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.

查看更多
Viruses.
3楼-- · 2019-01-26 05:46

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.

查看更多
疯言疯语
4楼-- · 2019-01-26 05:50

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.

查看更多
5楼-- · 2019-01-26 05:55

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

查看更多
你好瞎i
6楼-- · 2019-01-26 05:58

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.

查看更多
你好瞎i
7楼-- · 2019-01-26 05:58

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.

查看更多
登录 后发表回答