Interface vs Abstract class vs Concrete class

Interface vs Abstract class vs Concrete class

Image for postWell, it?s more than that?

While starting with java as your core language, the only thing that should be on your mind is to understand every native feature that the language has to offer. As java is all about classes, it has some neat design patterns for developers to follow. Your duty as a responsible programmer is to question these design patterns quite often; after all the engineers who built java planned on designing it the way it is now for a reason. So without wasting much time on gossip, lets dive in?.

The why ?

Java being an object oriented language gives you the bliss to write your code in the form of reusable classes. Now as the word reusable has been used, it is there for a reason. Code re-usability doesn’t start by creating objects out of classes, it starts way before that; while you are creating classes itself.

So we have Interface, Abstract class and Concrete class.

PS: Interface is not a class.

1. Interface

Interface is a blueprint for your class that can be used to implement a class ( abstract or not); the point is interface cannot have any concrete methods. Concrete methods are those methods which have some code inside them; in one word – implemented. What your interface can have is static members and method signatures. The example below shall help you understand how to write an interface.

public interface Brain{ public static final int number = 1; public void talk( String name ); public abstract void doProgramming();}

The declaration is much like a class but inside the interface there are some strict rules you need to follow:

  • All methods that you declare in an interface can have ? static ?, ? default ? or ? abstract ? modifiers ( Since Java 8 ). Implicitly they are ? public abstract ?.
  • Since Java 8, methods can be implemented ( can have a code body ) in an interface if only if it is declared static or default. Abstract methods cannot have a body; all they can have is a method signature as shown in the example above.
  • Variables are not allowed in interface. Hence any data declaration is ? public static final ?; hence only constants.
  • Interfaces can extend other interfaces ( one or more ) but not classes ( abstract or not ).
  • Interfaces cannot be instantiated as they are not concrete classes.
  • Methods and constants cannot be declared ? private ?, methods cannot be declared ? final ?.

2. Abstract class

Abstract classes are a bit different from interfaces. These are also used to create blueprints for concrete classes but abstract classes may have implemented methods. Abstract classes can implement one or more interfaces and can extend one abstract class at most. There is a logical reason to this design which we will talk about later in this post. Here is an example of Abstract class creation.

public abstract class Car{ public static final int wheels = 4; String turn( String direction ){ System.out.println( “Turning” + direction ); } public abstract void startWithSound( String sound ); public abstract void shutdown( );}

The declaration rules are as follows:

  • A class can be an abstract class without having any methods inside it. But if it has any methods inside it, it must have at least one abstract method. This rule does not apply to static methods.
  • As abstract classes can have both abstract and non abstract methods, hence the abstract modifier is necessary here ( unlike in interface where only abstract methods are allowed ).
  • Static members are allowed.
  • Abstract classes can extend other at most one abstract or concrete class and implement several interfaces.
  • Any class that does not implement all the abstract methods of it?s super class has to be an abstract class itself.

3. Concrete class

Concrete classes are the usual stuff that every java programmer has come across for sure. It is like the final implementation of a blueprint in case you are extending it some abstract super class. A concrete class is complete in itself and can extend and can be extended by any class.

public class Rocket{ public static final int astronauts = 4; String turn( String direction ){ System.out.println( “Turning” + direction ); } public abstract void startWithSound( String sound ){ System.out.println( “Engines on ” + sound + “!!”); } public abstract void shutdown( ){ System.out.println( “Ignitions off !!” ); }}

There are no unusual rules of declaration to talk about other that the fact that all the methods have to be concrete and it can extend abstract or concrete class as well as implement several interfaces. The only condition is that all the methods have to be implemented in order for it to qualify as a concrete class.

Extends and Implements

Now for the sake of understanding, lets consider an example of some interfaces, abstract classes and concrete classes that might help you to clarify your doubts about who can implement and extend what. I will put some comments where ever necessary. One thing to keep in mind is the fact that implements keyword is used when you can implement the inheritance, while extends is used to when we have some implemented methods that we can use from the inheritance.

interface Cognition{ void doProgramming();}interface Motor{ void write(); void bite();}interface Brain extends Cognition, Motor{ //the logic behind extends here is we cannot //implement anything in an interface int number = 1;}abstract class Body{ //Totally valid}abstract class clothes extends Body{ abstract void whatDoYouLike( String type );}//As in the next class I don’t plan to implement everything so I //have to leave it as abstractabstract class LivingBeing extends Clothes implements Brain{ void bite(){ System.out.println( “I know how to do that, ghaawww” ); } void whatDoYouLike( String type ){ System.out.println( “I like to wear a” + type ); }}//Now I am planning to implement all the abstract methods so time to //switch to a concrete classclass Human extends LivingBeing{ void write(){ System.out.println( “I will write a medium post !” ); } void doProgramming(){ System.out.println( “I would love to code !!” ); }}class Dog extends LivingBeing{ void write(){ System.out.println( “Woof Woof ??” ); }void doProgramming(){ System.out.println( “Woof Woof ??” ); }}

PS: Java does not allow multiple class inheritance because if two class had two different implementation of the same method then the compiler won?t know which one to use; while on the other hand you can inherit multiple interfaces because there is no implementation for the compiler to be confused about, and its up to you how you wish to implement it.

When to use what ?

The above example might have helped you in understanding the use cases and I am sure now you are convinced that it is not complicated design but simple ingenuity. So whenever you need multiple inheritance and a clean and clear blueprint that has only the design and not the implementation; you go for interface. If you don?t need multiple inheritance but you need a mix of design plan and pre-implementation then abstract class is your choice.

Knowledge not shared is wasted ? Clan Jacobs

? Like, Share and Follow?. ?

No Responses

Write a response