Abstract Classes

1.    A method without a body is called abstract method.
2.    If a class has an abstract method the whole class should be declared as abstract class.
3.    Any sub class extending the abstract class should override all the abstract methods of the super class.
4.    Abstract classes can have non abstract methods.
5.    Abstract classes can not be instantiated.
6.    Abstract classes need not have an abstract method.

In order to prevent Instance creation
-    Declare class as Abstract.
-    Declare constructor of the class private.


abstract class Shape1
{
    abstract void area();
    void met1()
    {
        System.out.println("met1 in shape1");
    }
}
class Square extends Shape1
{
    void area()
    {
        System.out.println("area in Square");
    }
}
class Rectangle extends Shape1
{
    void area()
    {
        System.out.println("area in Rectangle");
    }
}
class Abstract1
{
    public static void main(String args[])
    {
        //Shape1 x = new Shape1();
        Square a = new Square();
        a.area();
        a.met1();
        Rectangle b = new Rectangle();
        b.area();
        b.met1();

    }
}

OUTPUT : Compile Error – Shape1() is abstract; can not be instantiated. Comment it.

OUTPUT :    
met1 in Shape1
area in Square
area in Rectangle
met1 in Shape1