tags:

views:

88

answers:

5

One more question, what is the difference between abstract class and interface? Thanks for your help

+3  A: 

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

Jeanne Boyarsky
+6  A: 

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.

Joe
+2  A: 

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.

Chankey Pathak
A: 

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

Suresh S
that is not a disadvantage, that is just a symptom of a poor design, not a problem with the reason interfaces exist. it probably means the interface should be multiple interfaces, but on the other hand an interface is supposed to represent a unified group of methods for a reason, if a new method fits into that group then it is justified. also this is a place for abstract classes in between the concrete classes to have a "default" no op implementation that throws an UnsupportedOperationException, either way, this "answer" is complete FUD.
fuzzy lollipop
if a new method fits into that group then it is justified.
Suresh S