Sometimes certain method in a superclass does not contain any logic and needs to be overridden by the subclass. In such situations the method in the superclass should be declared by using the keyword, abstract. Such methods are referred as subclass responsibility as they have to implementation specified in the superclass. Thus it’s the responsibility of the subclass to override it. The syntax is:

abstract type methodname( parameter list );

no method body is present. The class which have one or more abstract methods are abstract. To declare a class as abstract, use the abstract keyword in front of the class keyword, before the class declaration. Abstract classes cannot be instantiated. Similarly the new keyword cannot be used to create an object of the abstract class. Remember that the constructors and static variables cannot be declared as abstract. Any subclass of an abstract class must either implement all of the abstract methods in the superclass or be itself declared abstract.

The following example declares area ( ) as an abstract method inside AbsDemo class. This means that all classes derived from AbsDemo must override area( ).

abstract class AbsDemo
{
    double len;
    double width;
    AbsDemo(double num, double num1)
        {
        len = num;
        width = num1;
        }
    abstract double area();
}

class Square extends AbsDemo
{
    Square(double num, double num1)
    {
        super(num, num1);
     }
    double area()
    {
        System.out.println("Area of the Square is : ");
        return len * width;
    }
}

class Rectangle extends AbsDemo
{
    Rectangle(double num, double num1)
    {
        super(num, num1);
    }
    double area()
    {
        System.out.println("Area of the Rectangle is : ");
        return len * width;
    }
}

class AreaDemo
{
    public static void main(String args[])
    {
    AbsDemo aObj;
    Square sObj = new Square(10, 10);
    Rectangle rObj = new Rectangle(20, 25);
    aObj = sObj;
    System.out.println("Calculated : " + aObj.area());
    aObj = rObj;
    System.out.println("Calculated : " + aObj.area());
    }
}

In above program an object of AbsDemo cannot be declared abstract. All the subclasses of AbsDemo override the method, area( ). The variable aObj is declared as a reference to AbsDemo, i.e. it do not  refer to an object of any class derived from AbsDemo.

The output of the program is:

synchronized Modifier

While implementing threads, the synchronized modifier can be used. It permits only one thread access to a block of code at a time.

volatile Modifier

The volatile tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program. If a variable is declared as volatile then it means that the compiler must always use the master copy of a volatile variable. Its value is not stored in registers.