What is abstract class? What
are advantages of using abstract class?
Ans
Abstract classes are classes that contain one or more abstract
methods. An abstract method is a method that is declared, but contains no
implementation. Abstract classes may not be instantiated, and require
subclasses to provide implementations for the abstract methods. Let's look at
an example of an abstract class, and an abstract method.
Suppose we were modeling the behavior of animals, by creating
a class hierachy that started with a base class called Animal. Animals are
capable of doing different things like flying, digging and walking, but there
are some common operations as well like eating and sleeping. Some common
operations are performed by all animals, but in a different way as well. When
an operation is performed in a different way, it is a good candidate for an
abstract method (forcing subclasses to provide a custom implementation). Let's
look at a very primitive Animal base class, which defines an abstract method
for making a sound (such as a dog barking, a cow mooing, or a pig
oinking).
public abstract Animal
{
public void eat(Food
food)
{
// do something with food....
}
public void
sleep(int hours)
{
try
{
//
1000 milliseconds * 60 seconds * 60 minutes * hours
Thread.sleep
( 1000 * 60 * 60 * hours);
}
catch
(InterruptedException ie) { /* ignore */ }
}
public abstract void
makeNoise();
}
Note that the abstract keyword is used to denote both an
abstract method, and an abstract class. Now, any animal that wants to be
instantiated (like a dog or cow) must implement the makeNoise method -
otherwise it is impossible to create an instance of that class. Let's look at a
Dog and Cow subclass that extends the Animal class.
public Dog extends Animal
{
public void
makeNoise() { System.out.println ("Bark! Bark!"); }
}
public Cow extends Animal
{
public void makeNoise()
{ System.out.println ("Moo! Moo!"); }
}
Now you may be wondering why not declare an abstract
class as an interface, and have the Dog and Cow implement the interface. Sure
you could - but you'd also need to implement the eat and sleep methods. By using
abstract classes, you can inherit the implementation of other (non-abstract)
methods. You can't do that with interfaces - an interface cannot provide any
method implementations.
By using the Abstract class, we achieve the Polymorphism.
Polymorphism simplifies the binding of the objects. By using the abstract class
as the reference as object definition time, we can easily replace it with the
concrete object at the runtime. This is called as the Dynamic Binding.
When we use the dynamic binding, the caller or the user of
the object doesn't need to worry of what type of object is being passed to it
at design time. At runtime the object would be assigned to it.
You can achieve the same Polymorphism and Dynamic binding
using the interfaces, too. But the advantage using the Abstract class is, you
could have the implementation of the non-abstract methods of the class where as
Interface would only define the object. This non-abstract methods could contain
the common logic which is valid for both Super class and Subclass.
An Abstract class is a way to organize inheritance, sometimes
it makes no since to implement every method in a class (i.e. a predator class),
it may implement some to pass to its children but some methods cannot be
implemented without knowing what the class will do (i.e. eat() in a Tiger
class). Therefore, abstract classes are templates for future specific
classes.
A disadvantage is that abstract classes cannot be instantiated, but most of the
time it is logical not to create a object of an abstract class.
No comments:
Post a Comment