Why Java interface required A full discussion with Example?

Why Java interface required A full discussion with Example?

For better knowledge of Java interface, the explanation is given in Questions/Answers form, an easy way to understand fast.

1. What is an interface in Java?

a) An interface contains only abstract methods (not even one concrete method).

b) It differs from abstract class in that abstract class can contain concrete methods also.

2. Can an interface contain variables?

Yes, an interface can contain variables apart all abstract methods. These variables must be declared as public, static and final and if not declared, all the variables declared in the interface are assumed as public, static and final by default by JVM.

3. What is the headache with Java interface?
4. What are the disadvantages of Java interface?

a) First, the methods do not have body at all (here, we feel interfaces are mere waste) and just contain method signature or definition.

b) It forces every subclass (which implements the interface) to override all the abstract methods eventhough the subclass does not want all (extra unnecessary labour) to override.

c) Objects cannot be created (reference variables creation is permitted).

5. What is the advantage of Java interfaces?
6. What is the place of Java interface in coding?

a) First and foremost is Java achieves multiple inheritance though interfaces.

b) Note that Java basically does not support multiple inheritance, but supports hierarchical and multilevel.

7. What are other advantages of interfaces apart supporting multiple inheritance?

a) Implementation abstraction is achieved.

b) That is, implementation is shown in subclass and not in interface itself. The subclass can give the implementation in its own style. For example, the Collection interface has an abstract method add(). This is overridden by List and Set in their own style.

c) One more practical example, say the Head office would like to permit discounts on the sales for an upcoming festival and designs an abstract method permitDiscounts(). This method is overridden by all the Branches giving different discount structure that suits a particular town.

d) Declaration and implementation have clear separation of code. Interface does not have implementation of methods and in a different place (its subclass) have.

e) Any class can implement the interface eventhough it is not related to. As an example, the interface Cloneable and interface Comparable are implemented differently by a number of unrelated classes in Java API.

f) Interface gives a template of methods from which other classes can be developed easily. Behaviour is given by the subclasses.

8. Give one example of template and behaviour as they are confusing?

Infact, this I feel the prime importance of interfaces than its support for multiple inheritance. Go through the notes keenly which after reading, you feel the immense importance of interfaces in Java coding.

I would like to go on a business tour from Chennai to Delhi attending some board meeting at Indore on the way. The business tour is finalized by my Secretary and put into an interface. The interface tells from where to where to go but now how to go – by plane, by bus or by train. The interface "TourPlan" is given with dates to a travel agent to book tickets. Now the travel agent writes a Java class implementing the interface and overriding each method. He knows very well if any abstract method is not overridden, I cannot go to Delhi. It is a must to override or implement with the mode of travel. Let us see how the travel agent overrides.

Java interface

The travel agent book the tickets as per availability. As from Chennai to Hyderabad, the flight tickets are not available, the agent prefers to go by Train as the tour time also permits. Now imagine, if the interface tells to go to Hyderabad from Chennai by plane and if plane tickets are not available, I have to cancel my whole trip. Interface simply tells to go to Hyderabad. How to go is the implementation part of subclass depending on situation.

If the Secretary and travel agent do not exist, I have to make the plan and book tickets myself. Now tell me which will take more time – the planning or booking tickets. It is definitely the plan. While planning I have to fix the schedule on the availability of time and nearest way to go between the places. Booking tickets is not that much laborious.

Now tell me what interface has done in the code and what TravelAgent has done? Interface gives a plan or template of methods from which other classes like TravelAgent can be developed easily. Interface is like a blue print for a house. I would like to construct a house and the Engineer gave me blue print paper. Is the blue print paper the house? Can I keep a chair over it and sit? Then, what after all a paper has given to me? The blue print tells you how to construct a house, but itself is not a house. As per the blue print construct a house and in that house you put a chair. Interface is also a blue print of methods or template of methods. Depending on the convenience, the subclass overrides the methods. If the same interface is given to the travel agent at some other time, he may book the tickets in a different way.

7. Now finally what are advantages of Java interface or uses?

a) Interface gives abstracted methods.

b) It gives a template structure from which other classes (subclasses) can be developed easily.

Interface tells what to do but not how to do. How to do is the job of subclasses.

8. Can an interface be used in nesting?

Interface can be used as nested interface also – options:
a) interface inside interface
b) interface inside class
c) class inside interface

All the above three options are shown in Nested Interfaces.

8. Can we get an object of Java interface?

Yes, there is a way to get an object of an interface (and not directly). Given in Can you create instance of Interface?

In and Outs of Interfaces

1. Interfaces – Partial implementation of Multiple Inheritance
2. Abstract class constructor & Interface abstract
3. Abstract Static Method
4. Interface Polymorphism
5. Play with Implements
6. Why the methods of an interface should be overridden as public only?

Leave a Reply

Your email address will not be published. Required fields are marked *