Tuesday, December 03, 2013

abstract keyword


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

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

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

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


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

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

7  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

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

9  An abstract class can contain final methods.

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

11 On a method, abstract indicates two things:

         (a) That the method must be implemented by any subclass.
         (b) 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.

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

13  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
}

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

No comments:

Post a Comment

Distributed Transactions

What is a distributed transaction?  Transactions that span over multiple physical systems or computers over the network, are simply termed D...