Interface is used to hide the implementation in a separate class, ie what ever the method need to be in implementation class should be defined in interface. For example ,if I want a static method in implementation class ,but I can�t define static method in interface, is there any alternatives?
When you put a method inside an interface it has specific purpose, specific reason and it is this specific reason, it is this spefic contract the class is actually bound to implement. A static method normally tends to move away from objective-ness.
Akhilesh Trivedi has explained it. An interface (keyword) specifies the "interface" (common English word) between an object and other classes. So the methods are implicitly instance type (specific to that object = not static) and public (accessible by other classes).
Joined: Apr 02, 2008
"interface" (common English word) between an object and other classes
an interface is nothing more than a contract. For example (from the Sun tutorial web page :
This does nothing more that says "Hey, when people talk about a Bicycle, you can be GUARANTEED that these four methods will bet here and will look like this".
By itself, it does no good. What happens is someone comes along and wants to write a "SchwinnTenSpeed" class. So, they IMPLEMENT the interface:
by typing "implement Bicycle", they are saying "i agree to abide by the terms of the Bicycle contract" - basically they are agreeing to implement those methods. They can implement other, additional methods, but they are REQUIRED to have those four with those signiatures.
Again, if this was all that happened... big deal. The neat part is that someone ELSE can write code that does this:
Bicycle myBike = someFactory.getBicycle();
That 'someFactory' method may be a call to some code they don't own. They can get back ANYTHING, as long as it implements the Bicycle contract. It could be a SchwinnTenSpeed, a Campenella, or even a Huffy, as long as they implement that contract.
The REALLY neat part is that the owner of the someFactory code can change the specific type of Bicycle whenever they want, and the calling code DOESN'T CARE. All they care about is that they get SOMETHING back that implements the Bicycle interface.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Oct 13, 2005
The interface between sea and sky is called the surface. The interface between radio and listener is called the knobs. The interface between car and driver is called the steering wheel, pedals, key, etc.
It is wherever the two touch, and more specifically where they can interact with each other. So the interface (common English word) between object of class "A" and object of class "B" is A's (or, vice versa B's) public methods and fields. There ought not to be any public fields, so the interface consists of public methods. Anybody can use the interface of the sea to watch the waves. Anybody can use the interface of the radio (turn the knobs) to listen to music. Anybody who can access the interface of a car (key, wheel, pedals) can drive it.
The interface may appear the same and work differently in different cases.
A diesel car has a right pedal which works by pumping more diesel into the cylinders. A car with a carburettor has a right pedal which works by opening the throttle. A petrol(gas) car without a carburettor has a right pedal which works by pumping more petrol(gas) into the intake manifold. The driver isn't interested in how the "interface" works; he thinks the car has a right pedal which works by making the car go faster.
The interface shows all other programs which methods are available and what they do.
is an example; it obviously gets the temperature in whole numbers of �, but doesn't explain how it works.
So . . . you add to the interface (common word) with an interface (keyword) so the interface (keyword) adds public methods to the object. So interface methods are implicitly public object (instance) methods.So the people who designed Java decided not to allow class methods (static methods) or private methods in interfaces (keyword).