Just as a counterpoint to this question: what is an interface in Java?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
An interface is a special form of an abstract class which does not implement any methods. In Java, you create an interface like this:
Since the interface can’t implement any methods, it’s implied that the entire thing, including all the methods, are both public and abstract (abstract in Java terms means “not implemented by this class”). So the interface above is identical to the interface below:
To use this interface, you simply need to implement the interface. Many classes can implement an interface, and a class can implement many interfaces:
Now if you wanted you could write a method like this:
However, you could never do the following:
The reason you can’t do this is that
yis of typeinterfaceA, and there is nointerfaceMethodB()ininterfaceA. Likewise,zis of typeinterfaceBand there is nointerfaceMethodA()ininterfaceB.I mentioned earlier that interfaces are just a special form of an abstract class. To illustrate that point, look at the following code.
You would inherit from these classes almost exactly the same way:
In fact, you could even change the interface and the abstract class like this:
However, there are two differences between interfaces and abstract classes.
The first difference is that interfaces cannot implement methods.
The interface above generates a compiler error because it has an implementation for
implementedMethod(). If you wanted to implement the method but not be able to instantiate the class, you would have to do it like this:That’s not much of an abstract class because none of its members are abstract, but it is legal Java.
The other difference between interfaces and abstract classes is that a class can inherit from multiple interfaces, but can only inherit from one abstract class.
The code above generates a compiler error, not because the classes are all empty, but because
InheritsFromTwoAbstractClassesis trying to inherit from two abstract classes, which is illegal. The following is perfectly legal.The first difference between interfaces and abstract classes is the reason for the second difference. Take a look at the following code.
There’s no problem with the code above because
InterfaceAandInterfaceBdon’t have anything to hide. It’s easy to tell that a call tomethodwill print “method()”.Now look at the following code:
This is exactly the same as our other example, except that because we’re allowed to implement methods in abstract classes, we did, and because we don’t have to implement already-implemented methods in an inheriting class, we didn’t. But you may have noticed, there’s a problem. What happens when we call
new InheritsFromTwoAbstractClasses().method()? Does it print “Hello” or “Goodbye”? You probably don’t know, and neither does the Java compiler. Another language, C++ allowed this kind of inheritance and they resolved these issues in ways that were often very complicated. To avoid this kind of trouble, Java decided to make this “multiple inheritance” illegal.The downside to Java’s solution that the following can’t be done:
AbstractClassAandAbstractClassBare “mixins” or classes that aren’t intended to be instantiated but add functionality to the classes that they are “mixed into” through inheritance. There’s obviously no problem figuring out what happens if you callnew InheritsFromTwoAbstractClasses().hi()ornew InheritsFromTwoAbstractClasses().bye(), but you can’t do that because Java doesn’t allow it.(I know this is a long post, so if there are any mistakes in it please let me know and I will correct them.)