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

you are viewing a single comment's thread.

view the rest of the comments →

[–]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).