"JavaRanch, where the deer and the Certified play" - David O'Meara
"JavaRanch, where the deer and the Certified play" - David O'Meara
Note that a method declared in an interface must not be declared final or a compile-time error occurs. However, a method declared in an interface may be
implemented by a method that is declared final in a class that implements the interface.
Note that a method declared in an interface must not be declared static, or a compile-time error occurs, because static methods cannot be abstract.
For compatibility with older versions of the Java platform, it is permitted but discouraged, as a matter of style, to redundantly specify the abstract modifier for methods
declared in interfaces.
It is permitted, but strongly discouraged as a matter of style, to redundantly specify the public modifier for interface methods.
"JavaRanch, where the deer and the Certified play" - David O'Meara
The funny thing is that inheritance is one of the easier aspects to understand and yet the one that you should probably code with the least. "Design Patterns" second principle of OOD is "favor object composition over class inheritance". As it says, "Inheritance breaks encapsulation." Inheritance causes a tight coupling between the parent and child classes which violates OO. The goal should be to inherit interfaces and not implementations. There is of course a tradefoff since an application built around object composition will have more objects.
"JavaRanch, where the deer and the Certified play" - David O'Meara
Originally posted by Paul Keohan:
All these definitions of interfaces are great but one question I've always had is; when would it be a good idea to actually use one? If I'm writing an application why should I write an interface to force me to use the methods when I could just write a class that invokes the methods anyway? Perhaps if I had a better knowledge of system architecture this would be plain to me.
An example would be great. I've seen examples that simply show interfaces in use but it still doesn't answer my question. When would you write an interface as opposed to a class?
Thanks for any help!
Paul
Originally posted by Siva Prasad:
A good and small example here would give the importance of using an interface....
abstract class Vechicle
{
}
class Car extends Vehicle
{
}
class Taxi extends car
{
}
class Horse
{
}
If I have a method say [b]speed() is applicable to all classes mentioned above.
So if I declared that in the Vechicle, this cannot be inherited by Horse (eventually it also has speed()) because its a different class.
So now to get this feature, I define an interface called Traffic which has this method speed() and other methods related to traffic.
And all the above classes can implement this interface, since they are part of the traffic.
I the code would look like this....
interface Traffic
{
public void speed();
.....
.....
}
abstract class Vechicle implements Traffic
{
}
class Horse implements Traffic
{
}
Here I've not mentioned this for Car and Taxi, why?
Since they are inherited from Vehicle, they get this method from class Vehicle.
Excuse me if I'd repeated anything too much.
Cheers
Siva[/B]
Originally posted by Paul Keohan:
This is a good description but it still brings me back to the same question. The interface doesn't give you the actual code so what is the advantage?
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog