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.
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.
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.
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.
It is definitely not appropriate
to derive a relationship between these two UNRELATED classes.
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'.
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
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 :
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?
Java Books
Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|