Facebook
Banner
XMPP JavaScript Library READ MORE

What is an abstract Class?

OOPs, Sachin Puri, 2013-02-20 01:12:54

Abstract class is a special kind of class which is declared with the keyword abstract and which can't be instantiated. An abstract class can contain zero or more abstract methods.

  • Abstract class cannot be instantiated.
  • It enforces some common behaviors across different class
  • An abstract class can contain abstract or non abstract methods
  • An abstract method can only be declared in abstract class.
  • An abstract method can't contain body.
  • It is compulsory for the class extending from abstract class to implement all the abstract methods of abstract class.
  • We can create abstract class without any abstract method.
  • We can't declare an abstract method as private or static
  • We can't change access modifier of abstract in child class
  • Class has to be declared as abstract which contains abstract method(s)

Example

abstract class car
{

    //May contain abstract methods 
    abstract public void stop();

    //May contain concrete (non-abstract) methods
    public void move()
    {
        //Implementation goes here
    }
    
}

Why to create an abstract class when we can't create instance of it?

If there is a situations where we need to create abstract methods then we have to create abstract class, because abstract methods can't be declared in normal class they can only be declared in abstract class or in an Interface.

Basic idea behind creating abstract class is to enforce some protocol which must be implemented by all the classes extending from abstract class.

Add Your Comment
   
    Yes! I want to receive all comments by email

  by adfa on 20-Feb-2013 01:17 am
Hi
  • Reply
  •  12 Like
  •  3 Dislike
  • Report