Tuesday, September 30, 2014

Abstract Cass

Abstract Class


Abstract classes are used to achieve abstraction. Any class which contains an abstract method should be declared as abstract. We can’t create object of an abstract class. Abstract class can also contain concrete methods so that’s why it doesn’t provide 100% abstraction.

Syntax:           abstract class class-name {  }

abstract is a keyword which is used to achieve abstraction.

Abstract class doesn’t contain the definition of abstract method. It only contains the declaration of the abstract method. Abstract methods are defined to achieve runtime polymorphism. An abstract method can’t be a private, static and final method. Abstract class can contain more than one abstract functions but subclass should contain the definition of all the abstract methods.  Example:          
                               
abstract class A
{
      abstract void absfun(); // abstract method ends with semicolon.
      public void concfun()
      {
            System.out.println("concrete method");
      }
}
class C extends A
{
      void absfun()
      {
            // definition of abstract method in derived class.
            System.out.println("abstract method");
      }
      public static void main(String args[])
      {
            C ob= new C();
            ob.absfun();
            ob.concfun();
      }
}
Output: abstract method
        Concrete method

If any class is derived from an abstract class then it is subclass’s responsibility to fulfil the body part of abstract method of superclass. And if any derived class doesn’t fulfil the body part of abstract method of super class then it is necessary to declare the derived class as abstract class. One abstract class can also inherit or extends another abstract class.  

Abstract class can also contain constructors, member variables. Abstract class is different from interface. Object of abstract class can’t be created. Abstract class must contain ab abstract method. Abstract methods are used where two or more subclasses are expected to do similar things in different ways through different implementation. Let’s see an example:  

abstract class figure
{
public int dim1=10, dim2=20;
abstract int area (); // abstract method ends with semicolon.
}
class rectangle extends figure
{
int area()
{
return(dim1*dim2);
}
}
class triangle extends figure
{
int area()
{
return((dim1*dim2)/2);
}
}
class abstractcls
{
public static void main(String args[])
{
//figure ob= new figure();  illegal, coz we can’t create instance of abstract class
figure ob= new rectangle(); // or rectangle ob= new rectangle();
triangle ob1= new triangle();
System.out.println("area of rect = "+ ob.area()+"\n area of tri = "+ ob1.area());
}
}
Output: area of rect = 200
        area of tri = 100

Abstract methods are used where two or more subclasses are expected to do similar things in different ways through different implementation. Abstraction is used to hide the complexity.


No comments:

Post a Comment