An abstract class is simply a class that serves as the base
class of other classes in a class hierarchy, without allowing the instantiation
of its objects. Okay, let me go a bit slowly and use an example.
Let’s say you want to create a class hierarchy of birds in
Java. You would obviously start by creating the base class Bird. Then you would
create other classes such as Hawk, Chicken, Turkey, etc., all which extend the
base class Bird.
Now think about it. There is nothing in this world just
called a bird. What I mean is that there is no animal that can just be
described as a bird. In other words, there is no creature that just sits in no
man’s land of taxonomy that we can say is not a chicken, or a hawk, or a kiwi,
or an ostrich and just say that the creature we are talking about is a bird.
So, while you create new objects of the class Chicken, or
Turkey, or Hawk, it wouldn’t make much sense creating new objects of the type
Bird. So in this example, Bird serves as the base class of all the birds that
extend it, but in itself doesn’t have any functionality.
Here is how we declare the abstract class:
public abstract class Bird {
}
Now, the bird class could have several member variables and
even methods.
public abstract class Bird {
private String
name;
public void
fly() {
System.out.println(“I
am flying”);
}
}
Also, it’s not just classes that can be declared abstract.
Methods can also be declared abstract. When you declare a method in a parent
class as abstract, Java will force you to implement the method in all the child
classes of the parent class. Here is an example of an abstract method. Also,
abstract methods in the base class have no method body. You implement the
method body in the child class as you implement the abstract methods. Also,
note that only abstract classes can have abstract methods:
public abstract class Bird {
private String
name;
public
abstract void fly();
}
Now, we can create the child classes of the abstract class
like this:
public class Hawk extends Bird {
@Override
public void
fly() {
System.out.println(“I
am a hawk and I am flying”);
}
}Figure 1: Class Hawk extending abstract class Bird
public class Chicken extends Bird {
@Override
public void
fly() {
System.out.println(“I
am a chicken and I am flying”);
}
}Figure 2: Class Chicken extending abstract class Bird
public class Turkey extends Bird {
@Override
public void
fly() {
System.out.println(“I
am a turkey and I am flying”);
}
}Figure 3: Class Turkey extending abstract class Bird
If you look closely, you will notice that there is a lot of
similarities between abstract classes and interfaces. We discussed interfaces
in a past tutorial. The difference is that with interfaces, we cannot define
the default functionality of an abstract method. We just list the method and
then define its behavior in the classes that implement it.
Another difference between abstract classes and interfaces is
the fact that a class can only extend one class, abstract or otherwise.
However, a class can implement any number of interfaces and we show this by
using a list of interfaces separated by commas.
So an abstract class just says what the class that extends it
really is. It makes a strong statement about the identity of the child class.
For example, the fact that the class Hawk in the above example extends the
abstract class Bird makes a statement that a Hawk is a Bird. So is the case
with Chicken and Turkey.
We normally use abstract classes when making a class
hierarchy. The base class shows the properties that all child classes will
have, but with abstract classes, we cannot create new objects. In the above
example, Java will not allow us to write Bird bird1 = new Bird(); because Bird
is an abstract class.
That’s it for now about abstract classes and their uses. If
anything is unclear in this tutorial, please drop your question in the comments
section below and I will make sure to answer it.
In the next tutorial, we are going to discuss the “try with
resources” functionality that was introduced in Java 7.
Until next time, stay safe.
No comments:
Post a Comment