开发者

What is the use of interface? how dynamic method resolution works at runtime?

What is the difference between an abstract class and an interf开发者_StackOverflow社区ace?


Interfaces are stateless. They cannot gave variables, though they can have constants. Also, interfaces provide the 'design by contract ' capability. Abstract classes force a concrete implementation, where interfaces allow more flexibility because any class that implements that interface can be substituted at run time. Also, since interfaces simply describe behavior that is exposed, not the implementation, then thet allow for multiple inheritance. Also abstract classes are more a design convenience as they provide for compiler enforcement in that subclasses must implement abstract methods. Interfaces and abstract classes are related, but serve different purposes. At runtime the type of object is checked and the corresponding class method invoked. This is also called late binding. This is done by the runtime VM not by the programmer thus taking that If Else test out of your program code. So, your code is more flexible and does not depend on the class type to resolve the correct method to call. Thus us also called polymorphism.


An abstract class can have methods implemented. An interface cannot. Also a class can only extend one abstract class, but can implement many interfaces.


Use of interface : There are a number of situations in software engineering when it is important for disparate groups of programmers to agree to a "contract" that spells out how their software interacts. Each group should be able to write their code without any knowledge of how the other group's code is written. Generally speaking, interfaces are such contracts.

One benefit of using interfaces is that they simulate multiple inheritance. All classes in Java (other than java.lang.Object, the root class of the Java type system) must have exactly one base class; multiple inheritance of classes is not allowed. Furthermore, a Java class may implement, and an interface may extend, any number of interfaces; however an interface may not implement an interface.

Another use of interfaces is being able to use an object without knowing its type of class, but rather only that it implements a certain interface.

Difference bw abstract class and interface : Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes. An abstract class can contain no abstract methods also. A Java Interface can contain only method declarations and public static final constants and doesn't contain their implementation. The classes which implement the Interface must provide the method definition for all the methods present. An abstract class means the class must be extended. An abstract class must be extended by first concrete class in the inheritance tree. In the abstract class we can have both declaration and definition of a method but in interfaces there are only method signatures, no definition at all. An interface is like a 100% pure abstract class. A class can extend only one class but can implement multiple interfaces. Interfaces provides multiple inheritance without causing deadly diamond of death problem.


Extensive discussions here :

  • Abstract classes and interfaces in C#
  • Do/can abstract classes replace interfaces?


Disadvantage : when you have an 1000 class implementing an interface in your library, tomorrow if you want to have an additional method in interface , then changes should be reflected everywhere

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜