keyur
keyur

Reputation: 249

Java abstract modifier

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

Upvotes: 2

Views: 11085

Answers (5)

user156676
user156676

Reputation:

The Modifiers Matrix answers your question:

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

Upvotes: 4

Alberto Zaccagni
Alberto Zaccagni

Reputation: 31560

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

Upvotes: 2

AAA
AAA

Reputation: 4956

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.

Upvotes: 0

JesperE
JesperE

Reputation: 64404

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.

Upvotes: 1

sepp2k
sepp2k

Reputation: 370112

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

Upvotes: 0

Related Questions