Interview Questions

Abstract Class and Interface

An Abstract class in java is a class for which objects cannot be created. It has the following properties.

1. It may contain zero or more abstract methods.
2. Abstract methods should be preceded with the keyword abstract.
3. It may or may not contain concrete methods.
4. Abstract classes cannot be instantiated. You cannot create objects for abstract class.
5. The concrete subclass which extends the abstract class, should implement all the abstract methods present in that extended abstract class.
6. It can have both private and public constructors.
7. An abstract method cannot be final as well.
8. A concrete class cannot extend more than one concrete or abstract class.

Shape.java

abstract class Shape
{
        double  length  = 0.0;
        double  breadth = 0.0;

        abstract public double getArea();
        abstract public void fillColor(String color);
}

class Rectangle extends shape
{
        @Override
        public void fillColor(String color)
        {
                System.out.println("Fill Red color inside the shape");
        }

        @Override
        public double getArea()
        {
                return length * breadth;
        }
}

Have look at the above class shape. It consists of two abstract methods which should be implemented by the first concrete class which extends the abstract class. For example, classes like Rectangle, Triangle and circle can extend from that abstract class.

Since we have talked so much about abstract class, its now times to explore his best friend Interface. There are lots of similarities (and of course differences) between the two.

What is an Interface?
Interface in general is an entity through which we can communicate with another entity. In java, an Interface is similar to a class, which contains a set of methods. These methods need to be implemented by a class. Now let’s see the features of an Interface in java.

1. All the methods in an interface are public & abstract.
2. A concrete class implementing an interface should implement all the methods in the interface.
3. It can contain variables, which are by default public static and final.
4. A class can implement more than one interface.
5. Interface is the powerful tool using which java incorporates the concept of polymorphism.

Consider the following Interface car

 interface Car
{
        int             price   = 0;
        String  color   = "";
        String  name    = "";

        int getPrice();
        String getColor();
        String getName();
}

class HondaCity implements Car
{
        @Override
        public String getColor()
        {
                return "Black";
        }

        @Override
        public String getName()
        {
                return "HondaCity";
        }

        @Override
        public int getPrice()
        {
                return price + 1000000;
        }
}

The methods in the car interface are implemented by the class HondaCity. Similarly in JDK we have many api's using interfaces. For example, consider List interface, it is being implemented by ArrayList, LinkedList. Interface comes in handy when we have to depend on any third party or vendor api. For example, consider the interfaces available in java.sql.* package like, ResultSet, Connection PreparedStatement etc,. These are interfaces in the jdbc api which should be implemented by the particular database vendor. As a developer, you write your code adhering to the JDBC api interfaces and its up to the vendor to implement them. This is really useful because we can change the vendor in future without changing our code. In this way we achieve polymorphism (one entity multiple forms) in java using interfaces.

Difference between Abstract class and Interface?

When you ask the above question to a java developer, the ready made answer we get as a reply is “interface should have all abstract methods, interface is used to achieve multiple inheritance concepts in java ..... “. In reality there is more to it than the above difference. By now a question will be pondering in your mind as to what is the need of interface, when we can have all the methods in abstract class as abstract. Well, other than multiple inheritances we can practically achieve almost everything in abstract class, instead of an interface.

Let’s take up this question in design point of view. Usually go for an abstract class when you have a set of classes having a common behavior. That abstract class can contain methods which are common for all the concrete classes which extend from it and have other methods implemented in their own way. For example, consider an abstract class Person; this class can be inherited by Jerry, Thompson and Verica. Because all three are human beings, they have certain features common to all human beings like hands, legs, head, neck etc, and they also differentiate themselves from others in terms of their own behavior (character). Go for interfaces when you want role specific behavior. In the above example, a person may perform multiple roles. For instance, Jerry may have roles such as Doctor (by profession), Father (to his children), Husband (to his wife) etc. Now we can have Doctor, Father and Husband as interfaces implemented by Jerry.

Code to an interface than an implementation

You would have come across the above quote many times. Here the word interface doesn't actually mean the interface in java. It just means coding to a super type than to the concrete sub type. It may be implemented either using interface or abstract class based on requirement.

To conclude, use abstract class and interface based on requirement (or design) and not for sake. Always think in terms of design than implementation. When they are used effectively, it improves code maintenance and re usability.