What is Interface and Concept of Interface in Java?

While learning Java you might think or this question may raise in your mind  that what is an INTERFACE in java, and  how does it differ from the Class, Subclass or any Abstract class.

What is an Interface in Java:

An interface looks pretty much like a class but actually is not a class. Let us define some necessary elements in java.

Class: Use it when there is no IS-A relation.

Subclass: Create it when a class is a more specific type of another class, which needs to add new behaviour or override a method.

Abstract class: Make it when there is a template for a group of subclasses and you want to make sure nobody can make an object of that type.

Interface: Design the Interface when different classes from different inheritance tree share a role. Another issue is that not only you can use supper() for constructor overriding, you can use super.superclass_method_name() to override ordinary methods.

Concept of the Interface:

When we design our classes, sometimes we need to add more functionality to the subclasses. For example consider we have a class called animal. Some of the animals can be pets. Therefore they have additional functionality such as play(), beFriendly() and lick().
Another examples can be designing a game, which needs actors such as fighters, alien

birds, friendly creatures and wild animals. A fighter needs to useGun() but not others. Consider a university that needs staff such as academic, administrative and contractors. Academic staffs should be able to gradeStudents() but not others.

A hospital needs staff such as doctors, administrative and contractors. Doctors should be able to prescibe() but not others. How can we add such functionalities without harming the entire design? There are four alternatives to solve this problem that each has its advantages and disadvantages. These four are:

  1. Put all the above methods in superclass.
  2. Put all the above methods in superclass and mark it abstract so that the subclasses have to override them.
  3. Put the methods only in subclasses that they belong to.
  4. Make an interface.

In next subsections we look at how these approaches are different about each other.

1- Put all the above methods in superclass:

In this approach we put all the desired methods in superclass (e.g animal, hospital staff, university staff).

The advantage of this approach is that all the subclasses will inherit the methods. Subclasses need not to be changed even those that is created later in future. Supperclass (i.e animal) can be used as a polymorphic type in any program. (i.e. a pet as an animal). But there are some disadvantages for this approach. Some functionalities should not be allowed to be inherited; for example a contractor staff in university is not an academic and should not be able to mark students. Also not all subclasses behave the same; for example a nurse and a doctor both can prescribe but in a different way.

2- Put all the above methods in superclass and mark it abstract so that the subclasses have to override them:

In this way, we put all the required methods in superclass and mark the superclass abstract so that the subclasses have to override those methods.

The advantages of this approach are that all the subclasses will inherit the methods, which is defined in superclass. The subclasses, even those, which will be created in the future, need not to be changed. Supperclass can be used as a polymorphic type in any program. Sub-types cannot run other sub-types methods (i.e. non-pet animals can not run the pets method).

But it should be noted that since the methods are abstract, the concrete subclasses have to override the methods. For example, even the non-pet concrete classes have to override the methods even if it supposed to do nothing.

3- Put the methods only in subclasses that they belong to:

In this approach we put the methods only in subclasses that they belong to.

Therefore subtypes do not possess their siblings’ behaviours. (i.e non-pet animals do not possess the pet behaviours.). Also different subclasses can have their specific behaviour in their method. (e.g. cat’s play is different from dog’s play)

The disadvantage of this method is that we have defined a protocol so that every subclass that is added in future should obey that protocol, which is having all the pet methods. Think of a small mistake by programmers (e.g. define beFriendly() as doFriendly()). Someone wants to use beFriendly() method but s/he finds that the would not work for some pets. Also in this method you spoiled polymorphism (i.e. compiler will not recognize a method such as lick() when you define your variable as ‘animal’).

4- Make an interface:

In this approach you make an interface in java. Let us remind ourselves of the requirements. We need to have sub-types behaviour only in similar classes. (i.e. to have pet behaviour only in pet classes). We also require that all similar sub-types have the same methods with the same name, argument and return type.(i.e. all pet methods have lick() method). Most importantly we want to be able to use polymorphism.

The solution is to have multiple inheritance but it is not possible in Java. Because java does not want to have Deadly Diamond of Death. For example a staff can work in a department and be part of a project team. If she requires to report(), she does not know which report she should produce? (department report or project report). Therefore to solve this problem we use interface.

How to Define an Interface in Java:

To define an interface you mark your class name with interface keyword. It should be noted that interfaces’ methods are implicitly public abstract.

To implement an interface, you use implements keyword. All interface methods are abstract and they MUST end with semicolon (no body). These methods will be overridden in the concrete classes.

With this solution you can use interface instead of concrete method as an argument and return type. Then you can pass/return anything that implements that interface. Therefore we can have polymorphism.

With interface, a class does not have to come from just one inheritance tree.

Also a class can implements more than an interface.

Now you should be able to distinguish between using classes, subclasses, abstract classes and interface.

keep going on with codejow

Leave a Reply