Friday, January 9, 2015

Abstract class

Abstract classes in Java are classes which cannot be instantiated, meaning you cannot create new instances of an abstract class. The purpose of an abstract class is to function as a base for subclasses.

The only time a subclass of an abstract class is not forced to implement all abstract methods of its superclass, is if the subclass is also an abstract class.

Abstract Methods

An abstract class can have abstract methods. You declare a method abstract by adding the abstract keyword in front of the method declaration. Here is how that looks:
An abstract method has no implementation. It just has a method signature.
If a class has an abstract method, the whole class must be declared abstract. Not all methods have to be abstract, even if the class is abstract. An abstract class can have a mixture of abstract and non-abstract methods.

The Purpose of Abstract Classes

The purpose of abstract classes is to function as base classes which can be extended by subclasses to create a full implementation. For instance, imagine that a certain process requires 3 steps:

The step before the action.
The action.
The step after the action.
If the steps before and after the action are always the same, the 3-step process could be implemented in an abstract superclass like this:

public abstract class MyAbstractProcess {

    public void process() {
        stepBefore();
        action();
        stepAfter();
    }

    public void stepBefore() {
        //implementation directly in abstract superclass
    }

    public abstract void action(); // implemented by subclasses

    public void stepAfter() {
        //implementation directly in abstract superclass
    }
}
Notice how the action() method is abstract. Subclasses of MyAbstractProcess can now extend MyAbstractProcess and just override the action() method.

When the process() method of the subclass is called, the full process is executed, including the action() method of the subclass.

Of course, the MyAbstractProcess did not have to be an abstract class to function as a base class. Nor did the action() method have to be abstract either. You could have just used an ordinary class. However, by making the method to implement abstract, and thus the class too, you signal clearly to the programmer, that this class should not be used as it is, but be used as a base class for a subclass, and that the abstract method should be implemented in the subclass.

Static

Static means that can be access in an static way (that not need to instanced the class like new ExampleClass)
You can treat static as a global variable which has scope. It basically means if you change it for one object it will be changed for all just like a global variable(limited by scope).

Methods
Java static method program: static methods in Java can be called without creating an object of class. Have you noticed why we write static keyword when defining main it's because program execution begins from main and no object has been created yet. Consider the example below to improve your understanding of static methods.

http://www.programmingsimplified.com/java/source-code/java-static-method-program