Java abstract modifier

半世苍凉 提交于 2020-01-05 03:06:12

问题


Can the abstract modifier appear before a class, a method or a variable?


回答1:


Abstract can be put in a class declaration, as in

public abstract class Test{
    //class implementation
}

...and in a method declaration, as in

public abstract void test();

On the argument: http://java.sun.com/docs/books/tutorial/java/IandI/abstract.html




回答2:


The Modifiers Matrix answers your question:

  • class: yes
  • method: yes
  • variable: no



回答3:


The abstract modifier is placed before classes or methods. For a class, it means that it cannot be directly instantiated, but has to be subclassed. For a method, it means that it does not have an implementation in the class, but has to be implemented in a subclass. It cannot be applied to variables.




回答4:


It can appear before classes (to prevent from being instantiated and allow them to have abstract methods) and before methods (to signify that the method is not implemented in this class, but any non-abstract subclass must implement it).




回答5:


A class and a method. The abstract modifier is used to signify that a class/method is expected to be overridden. As a guide:

class - Contains unimplemented methods and cannot be instantiated.

method -     No body, only signature. The enclosing class is abstract

Hope that helps.



来源:https://stackoverflow.com/questions/1352109/java-abstract-modifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!