abstract

 

  abstract can only appear on three things: a class, a method, or a named member inner class.

 

  You cannot put abstract on a variable, a constructor, or an interface definition.

 

abstract can appear on methods inside an interface. But it is not required there.  Unlike the methods in an abstract class, some or all of which can be real, an interface's methods must always all be empty and abstract-like whether or not the keyword abstract actually appears on them..

 

■ A class must be declared abstract if any of the following three circumstances are true:

 

(a) It contains one or more abstract methods, or

(b) It inherits abstract methods which as yet have no implementations, or

(c) It implements an interface but does not implement every method in it.

 

■ You can declare a class to be abstract any time you want.  An abstract class doesn't necessarily need to have any abstract methods in it.

 

■ An abstract class can contain real members as well as abstract methods.

 

  On a class, abstract can be placed anywhere prior to the class keyword.  i.e.

 

public abstract class MyClass   works

abstract public class MyClass   also works

 

  You cannot ever instantiate an object of an abstract class. You can declare one however.

 

  An abstract class can contain final methods.

 

  An abstract method or class cannot also be declared final.  If that were allowed, you wouldn't be able to override and thus implement the method!  In the case of a class, you wouldn't be able to instantiate the class. 

 

  On a method, abstract indicates two things:

 

(1) That the method must be implemented by any subclass.

(2) That the method should have no curly braces.  That's because abstract methods must end with  just a semicolon.  Nor can you put curly braces on the empty abstract-like methods in an interface.

 

  A semicolon ending on a method (meaning no curly braces), without an abstract modifier, indicates that the method should be considered abstract.  Such semicolon methods (except in interfaces) will be errored, and abstract will be called for by the compiler.

 

  This sample code illustrates the above rules about abstract:

 

abstract class Abstracto {

                String s = "real";                                   // abstract classes can have member variables

                String realMethod( ) { return s; }        // abstract classes can have member methods

                final void goodFinalMethod( ) { return; }          // abstract classes can have real non-overridable final methods

                abstract String goodEmptyMethod( ) ;             // subclasses must implement this correct abstract method

                void badEmptyMethod( ) ;                  // the compiler will reject this method - you must mark it abstract

                final abstract void badFinalMethod( );              // the compiler will reject this non-overridable "abstract" method

}  

      

public class SubClass extends Abstracto {     

                Abstracto Good;                                   // you can merely declare abstract classes if you must  

                Abstracto Bad = new Abstracto( ) ;    // the compiler will reject this instantiation of an abstract class

                String goodEmptyMethod( ) { return s;}           // class must implement this method or become abstract itself

}

 

  Non-abstract subclasses of any abstract class can be instantiated successfully.

i.e. (Still using the class names from above) -  The statement  SubClass SC = new SubClass( ); will work in any other class which can see SubClass.