What is an Abstract class?
An abstract class’s main purpose is to be subclassed, you cannot instantiate an abstract class, and if you try to instantiate you will get compiler error. Advantage of using an abstract class is polymorphism and it gives greatest degree of flexibility and extensibility. Abstract class is used when you want to implement most generic methods in superclass (abstract class) and more specialized behavior in subclass. For ex: there is a class Animal has a generic methods common to all animals. You cannot create object of animal class because behavior of all animals is different, each animal has its own specific behavior. Let see an example,
1 2 3 4 5 6 7 |
abstract class Animal { private int age; public void getAge(); public abstract void eat(); public abstract void makeNoise(); } |
If you try to create an object of Animal, you will get compiler error. Abstract class or method can be defined using abstract keyword. An abstract methods ends with semicolon, rather than curly braces.
Abstract class cannot be declared as final or private as abstract class’s main purpose is inheritance and if any class is final or private we cannot create subclass from it.
Abstract Methods
An abstract method declared using abstract keyword, ends with semicolon and it cannot be implemented.
public abstract void eat();
If class has even a single abstract method, then class must be declared as abstract class. Concrete class cannot contain abstract method; if it has it should be declared as abstract class. Abstract class can contain abstract methods and concrete methods.
The first non-abstract subclass of an abstract class must implement all abstract methods of the superclass; otherwise you will get compiler error. If your abstract class is extended by another abstract class, then abstract class need not required to implement methods of superclass. Later when concrete class is subclassed from abstract class; it must provide implementation for all the methods from up the inheritance tree.
Abstract methods cannot be declared as final, private or static. We need to override the methods of superclass and has to provide implementation for all methods in subclass. If any of these modifiers is used in combination with abstract we cannot override methods in subclass.
Abstract class can have constructor and can be accessed or called when subclass is instantiated.
The following example gives an inheritance tree with abstract class and concrete class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<strong> public</strong> <strong>abstract</strong> <strong>class</strong> Animal { <strong> public</strong> Animal () { System.<em>out</em>.println("In Animal constructor"); } <strong> private</strong> <strong>int</strong> age; <strong> public</strong> <strong>abstract</strong> <strong>void</strong> eat(); <strong> public</strong> <strong>abstract</strong> <strong>void</strong> makeNoise(); <strong> public</strong> <strong>int</strong> getAge() { <strong> return</strong> age; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<strong> public</strong> <strong>class</strong> Cat <strong>extends</strong> Animal { <strong> public</strong> Cat() { System.<em>out</em>.println("In cats Constructor"); } <strong> public</strong> <strong>void</strong> eat() { System.<em>out</em>.println("Cat is eating fish"); } <strong> public</strong> <strong>void</strong> makeNoise() { System.<em>out</em>.println("Meow meow"); } <strong> public</strong> <strong>static</strong> <strong>void</strong> main(String [] args) { Animal cat = <strong>new</strong> Cat(); cat.eat(); cat.makeNoise(); } } |
Output:
In Animal constructor
In cats Constructor
Cat is eating fish
Meow meow
pulipati udaykiran says
November 8, 2011 at 11:03 amgood tutorial about abstract class and methods. I clarified a doubt on abstract class and methods by reading this article.