Question:
I had seen it somewhere now I don't remember, I think it was in an object orientation course that Ruby has. But in Java I never saw it. Is this why abstract classes are used? Or does this make no sense?
Answer:
In Java it is not possible for a class to inherit multiple classes.
The reason for this, imagine that a class inherits two other classes, however these two inherited classes have methods with the same signature but with different implementations. It would be difficult to deal with, as how would the subclass know which method implementation it should use? Therefore it is not possible for a class to extend from more than one class in Java.
An abstract class can have abstract methods as well as concrete methods, that is, methods that have already been implemented, so the fact that the class is abstract doesn't do much to solve the aforementioned problem.
What can be done in Java is that a class implements several interfaces, as interfaces are like 100% abstract classes, that is, any kind of implementation in it is completely prohibited, so there is no problem if the two interfaces have a method with the same signature, because who will implement the method is the class that implements the interfaces.
A curious thing is that the reserved word extends
can be used for interfaces, which is when an interface inherits another interface(s), but in that case there is no problem for an interface to inherit from several other interfaces as there is no implementation involved in this case. It would be a code that would look like this:
Interface1:
public interface Interface1 { public void teste(); }
Interface2:
public interface Interface2 { public void teste(); }
SubInterface:
public interface SubInterface extends Interface1, Interface2 { }
Note that there is no problem in doing this, as the implementation goes in the first concrete class that implements the SubInterface:
public class MinhaClasse implements SubInterface {
@Override
public void teste() {
System.out.println("sou apenas um teste");
}
}