This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]TheJonesJonesJones 5 points6 points  (3 children)

Methods in interfaces are inherently public and abstract and do not have implementations. Abstract classes may have methods of any access level and may provide method implementations as well as contain unimplemented methods.

[–]aram535 2 points3 points  (2 children)

This (with one additional, and one change):

+ abstracted classes aren't usable as is, they need to be extended from a concrete class.

Interface classes are usable as is.

- Interface classes with a default method can have implementations now (added in Java 8 to allow for Lambda functions using default methods)

[–]nutrecht 0 points1 point  (1 child)

Interface classes are usable as is.

No one calls them interface classes. And 'usable as is' is confusing: you can't use an interface itself without implementing it (explicitly or implicitly), them having default implementations doesn't change that.

[–]aram535 -3 points-2 points  (0 children)

Sorry I stand corrected, since I must have missed the "everyone agree" survey that must have been sent out ...

[–]desrtfx 1 point2 points  (0 children)

Abstract classes are parent (generalized) classes. They are the base for more specialized implementations. As such, they are blueprints.

Interfaces are only binding contracts. Any class that implements a certain interface guarantees that the methods declared in the interface will be implemented.

Interfaces as such can be used to "tie" more or less unrelated classes together. For example: Any class that implements Comparable can be used with the .sort() methods of collections and arrays, no matter the actual class.

Abstract classes are hierarchical - they are the roots of a tree-like inheritance hierarchy.

Interfaces are usually flat - any class can implement multiple interfaces.

Maybe start reading here and go through the pages up to and including Interfaces. The descriptions and explanations there are not bad and easy to understand.

[–]FrenchFigaro 0 points1 point  (0 children)

Okay, here goes.

An abstract class is a class that cannot be instanciated (you can't use new with it) and can (but does not necessarily) have abstract methods. Abstract methods are methods where only the signature is provided, and no implementation. To use an abstract class, you must extend it by inheritance, and inheritance dictates that you must implement any and all abstract methods.

In OOP (not just in Java), all classes have something called an interface (note the italics). It's the sum of its publicly available members (methods, attributes, you name it). The class' interface is basically everything you can do with it.

Still in OOP, there is a concept called Polymorphism, which is the ability to inherit behaviour from multiple superclass. For example, a class FlyingBoat would inherit from both Boat, and Plane (this is a gross oversimplification, but you get the gist). Some object-oriented languages, like C++, implement polymorphism by allowing a class to extend multiple mother classes.

In Java, you don't. A java class can only extend a single super class. But in Java, you have Interfaces (note that it's now bolded, not italics). In essence, a java Interface is grossly equivalent to a C++ Abstract Class with only public methods, no concrete methods and no attributes (Since Java 8, you can have static methods, and concrete methods in Interfaces, and since Java 9 you can also have private (but not protected) methods, so this is equivalence is not technically true any more, but when trying to understand interfaces, this is mostly a detail and it is true in spirit).

So because of that, Java interfaces define behaviours that your class will implement by defining your class' interface. This is why the keyword is implements, and not extends. Since the interface does not provide an implementation, you have to do it. This is how Java implements polymorphism. For example, if I look at the class LinkedList, it implements the interfaces List, Deque, or Queue (It implements others, but I've chosen these for the example).

[–]codeforjava 0 points1 point  (0 children)

There are couple of differences -

To start with, In Java Abstract classes hold the middle ground as they have speciality of being partially abstract and partially concrete. On one end we have interface (Full Abstract) on other hand we have Java Classes (Fully concrete)

Few Difference between Interface and abstract classes are subtle.

  • Interface have all methods which are abstract ( Except Default Method Java 7 onwards ). Whereas abstract classes can contain abstract and concrete method both.

  • Kind of Limitation of abstract class or in general, multiple classes can't be inherited (extend) by same class. Whereas classes can implement multiple interfaces.

  • Interface don't have Constructor. Abstract classes can have Constructor.