Yuvaraj Deena wrote:Im beginner in java programming, i just wanted to know main difference between abstract class and interface in java. However it seems both are similar.
In an abstract class, you can implement (ie, put code in) some or all of its methods if you want. With interfaces, you can't.
When Java 8 comes out, you can make default implementations for methods in interfaces as well.
There is another significant difference between interfaces and abstract classes. And that is multiple inheritance:
A class can only extend ONE other class (includes abstract classes)
A class can implement an unlimited number of interfaces
An interface can extend an unlimited number of other interfaces
One more thing to add about difference between Abstract Class and an Interface is:
All the member methods declared in an Interface are Public by default and member variables are Public and Static by default. Whereas in an Abstract class, nothing is default. In fact an Interface itself is Public by default.
In Abstract Class you can have both abstract and non abstract methods, but in interface all the methods are implicitly abstract and all methods are public, there will not be any implementation in Interface.
Whilst the key differences are listed here, it is also worth pointing out when to use one or the other. In fact, a possible employer will seek for more than just the differences.
An interface is a contract which implementing classes must fulfill. The implementations can be completely different, whereas with abstract classes, there is usually some common functionality already defined which subclasses can share or override.
Look for entities/nouns to map to abstract classes e.g. Animal. Look for actions/verbs to map to interfaces e.g. Comparable, Listener.
Whilst this rule may not apply in all cases, it is a good starting point during the design phase.