开发者

Why did Java's designers choose interfaces and single inheritance? [duplicate]

This question already has answers here: Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed? (21 answers) Closed 9 years ago.

I can make abstract class equivalent to interface by putting all abstract methods within abstract class. Why did Java's designers choose to provide support for interfaces and single inheritance instead of multiple inheritance and abstract classe开发者_C百科s. What is the advantage?


At least for one reason (besides the conceptual differences between the two): you can implement multiple interfaces but you can only inherit from a single abstract class at most.


Class-based multiple inheritance is not supported by Java. You can only inherit from a single class, but you can implement multiple interfaces.

This becomes handy when you need to treat multiple classes polymorphicly when they have different inheritance trees.


An interface is a contract between a class and its behavior. If a class implements an interface, it MUST provide implementation for the methods specified in the interface. There's a number of things that differentiate the two, however.

Check out the Oracle website for more on the differences between interfaces and abstract classes:

http://download.oracle.com/javase/tutorial/java/IandI/abstract.html


If you have multiple classes and you want to force that there would be some methods that should be common among them, irrespective of their behavior(implementation), Interface does that for you

Interface, is like a contract, which every other class which implements it has to follow

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜