About interfaces in Java

Why are there interfaces in java?  If we have them for different implementations of same methods in different classes, We can do that by overriding methods of a class also.
Tell me the importance of interfaces with examples.

Bhushan Vinayak DamleAnkit

What is an interface in computing terms?

Interface is a system or a device that is used to connect two unrelated or non-understanding entities.
This definition to an interface could be the key to your answer.

As you might already know, Interface does not provide any implementation but just defines a set of protocols that can be implemented by any class.

Let us consider a few examples...

You and the bicycle. Both of you are not related in anyway. But you want to ride the bicycle. How do you interact with the bicycle? You do it via the pedal which is an interface between you and the bicycle.
You could extend this concept to 'You' and the 'Television' where the interface is a remote control.

Speaking in Java terms.....

Think of the CPU and You. You are an object of the HumanBeing class. Intel Pentium is an object of the CPU class(or an extension of the CPU class).

Human being and the CPU class....are totally UNRELATED.
Now you need to develop a mechanism for objects of these classes to interact with each other. How do you do it?

It is definitely not appropriate to derive a relationship between these two UNRELATED classes.
Deriving a relationship in the sense....making one the subclass(inheritance) of another or making one the nested class of another.

This is where Interface comes into picture.

You develop a interface called 'Keyboard'. Your Interface called keyboard just defines a set of methods but does not implement it. For ex: if you look at your keyboard you have a button called 'DELETE'.
And when you look at this keyboard it says just DELETE and nothing else. Its just a definition of a method called DELETE whose implementation is not concerned with YOU (the object of the HumanBeing class). All YOU know is to press the button DELETE when you want to delete something. In this case, the CPU class 'implements' the interface Keyboard and gives a
implementation for the method 'Delete'.

So what have you done here? You have helped two UNRELATED entities(HumanBeing and CPU)  to interact with each other without deriving a relationship between these two.

Interfaces can be when you do not want to derive a relationship between two unrelated objects.

Your Second Question:

"If we have them for different implementations of same methods in  different classes, We can do that by overriding  methods of a class also."

Let us consider you develop a keyboard class with generic functions. Different processors give their own implementation by overiding the generic functions of your keyboard class. In order to do this your CPU class has to extend the keyboard class. If your class Pentium5 extends Keyboard class then it will have to provide implementations for the common methods and
fields between it(Pentium5) and Pentium4, as you cannot extend Pentium4 since Java does not permit multiple inheritance.
In this case, I would design an Interface called keyboard, give different implementations for different CPU classes(Pentium,
AMD..etc) rather than extending it and overriding methods.

I hope whatever I've told is correct. It would be helpful if the elite ppl of this group or any experienced Java programmers point out any mistake in my understanding of interfaces.

I would like to know if this helped you Bhushan.

Vinay Binny

Why do u say Java  does not permit multiple inheritance when it is possible through interface to inherit properties of 2 different classes?

Rimzim

*interfaces do not facilitate multiple inheritance,

*interfaces force classes to implement methods as defined in the interfaces they are implementing,  an interface has no properties.

*inheritance only occurs when a class uses the methods and properties of other classes.

Jonathan Roberts

As I know HttpServlet is an interface which has doGet(), doPost(), doOptions and so on, while you implement this interface to your servlet, you simply implement doGet or doPost alone.  Why we don't do implement others, as per the rule we've to implement all methods which are declared in the inteface while it is to be implemented.

Mahadevan

HttpServlet is not an interface.Its an abstract class.

Basically Servlet is the interface that specifies the contract between the web container and the servlet.  This is the refernce which containers use to refer the servlet.it has five methods :
1.init 2.service 3.destroy() 4.getServletConfig()5.getServletInfo()...

GenericServlet is the class which provides basic implementation of Servelt Interface.  It is an abstract class i.e. it does not implement service method of Servlet Interface..

HttpServlet is the class which extends GenericServlet and provides the Http specific implementation of Servlet interface. Its also an abstract class..normally all servlets extend this class.

Shahena Iqbal

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.