views:

422

answers:

5

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

A: 

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

sepp2k
A: 

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.

JesperE
A: 

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.

Jamie Lewis
+2  A: 

The Modifiers Matrix answers your question.

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

HTH, flokra

flokra
+2  A: 

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

Alberto Zaccagni
An abstract method cannot have a body, concrete classes that extend the abstract base class *must* have an implementation of the abstract method. The example above should have been:public abstract void test();
RKitson
Whoops... you are right. My fault.
Alberto Zaccagni