Hi, Take a simple code as an example, public interface SayHello { void printMsg(); } class SayHelloClass implements SayHello { void printMsg() { System.out.println("Hello"); } } In this example of interface, the class SayHelloClass needs to have the same method as interface and implement its printlnMsg method. However, any class can implement its method with the same function e.g. printMessage() {System.out.println("Hello"); }. It is not necessary to implement SayHello. Also, class cannot inherit behaviour from the interface definition. So, I don't understand why the necesscity of using interface? or my example is not good enough to illustrate this concept, anyone could help? Does interface like header file in C++? Thanks Andrew
Hi Andrew, Interfaces allows classes to make use of multiple inheritance that exists in some languages such as C++. In java, a class can only have one superclass. This issue, is recovered by the use of Interfaces. An interface is essentially a contract that the implementing class will implement certain methods. The implementation can be anything they want, but other class users will know the API because the Interface is known. Regards, Manfred.
Andrew Parker
Ranch Hand
Joined: Nov 12, 2001
Posts: 178
posted
0
Hi, Take the example again. public interface SayHello { void printMsg(); } class SayHelloClass implements SayHello { void printMsg() { System.out.println("Hello"); } }
Add a new class class SayHelloAgain implements SayHello { void printMsg(); // Will it print out "Hello" that is defined in SayHelloClass? } Otherwise, how can it simulate the multiple inheritance? Thanks for help. Andrew
Andrew, what you just posted will not compile. You are implementing an interface without providing an implementation for the method the interface decalres. i.e you are not fulfilling the contract put forth by that interface. It's either you omplement the method or declare your class as abstract. The fact that you can only inherit from one class in Java, an interface gives you the ability to somewhat inherit from another class. It's just that in this case, you are inheriting a method declaration and you have to provide an implementation. Thet's where the "multiple inheritance" comes in
------------------ Bosun SCJP for the Java� 2 Platform
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
The way I look at it is that interfaces give your class several aliases. Along with these aliases, you promise to implement certain functions. Your example doesn't really show the power of an interface. Let's extend your example to see what interfaces can do:
The pertinent part of this code is the last function sayHello(). Notice that it expects the class to implement the SayHello interface. It doesn't care whether it is actually a SayHelloClass or SayHelloAgain object. This is where interfaces can be useful. They are often used in this manner in the standard library. Layne
Hi, Thank you very much for your code. I understand the power of interface now. Yes, it can simulate the multi-inheritance and polymorphism. I changed the code a little bit as it cannot show the "Hello" string. interface SayHello { public void printMsg(); } class SayHelloClass implements SayHello { public void printMsg() { System.out.println("Hello 456"); } } class SayHelloAgain implements SayHello { public void printMsg() { System.out.println("Hello again 123"); } } public class MySayHello { public static void main(String[] args) { MySayHello mine = new MySayHello(); SayHelloClass hello = new SayHelloClass(); SayHelloAgain helloAgain = new SayHelloAgain(); mine.sayHello(hello); mine.sayHello(helloAgain); } public void sayHello(SayHello msg) { msg.printMsg(); } }