TL;DR
What is an abstract class?
An abstract class is a class that is declared abstract - may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all abstract methods in its parent class.
I will decipher the concept of an abstract class a little. In connection with the question why I use an abstract class. Sometimes I just add the phrase abstract
to a class, because some methods must be implemented by the user of that class, and specify which methods must be abstract. What is it for, however, if the user uses an abstract class, then some of its methods already have a default implementation that can be overridden, and abstract methods also overlap, only the syntax assumes the presence of the abstract
modifier and the absence of the method body, thereby defining only the signature. This is a property of the language and different from other object-oriented languages. The phrase abstract
does not necessarily imply abstractness or such an interpretation, but only the possibility of specifying methods that need to be overridden in descendant classes.
What is abstraction in programming?
In object-oriented programming, abstraction is one of the three main principles (along with encapsulation and inheritance). Through the abstraction process, the programmer hides everything but the relevant data about the object in order to reduce complexity and increase efficiency.
Can an abstract class have no abstract methods?
Yes, we can have an abstract class without abstract methods since both are independent concepts. Declaring an abstract class means that it cannot be instantiated by itself and can only be subclassed. An abstract method declaration means that the method will be defined in a subclass.
Can you create an object of an abstract class in Java?
If we could create an object of an abstract class and call its method without a body (because the method is purely virtual), then it would give an error. That's why we can't create an object of an abstract class. In short, it is legal to have a public constructor of an abstract class. An abstract class cannot be created, but it can be used if there is an appropriate implementation of this class. Abstraction itself, unlike inheritance, implies the absence of an implementation code. If you are using an abstract class, then you can limit yourself to declaring methods. Interfaces are abstract classes in which all methods are abstract.
Now to answer your questions:
Q: Are interfaces associated with the "has a" relationship?
A: No, because they can't instantiate.
Q: Are interfaces only used with unrelated classes?
A: No, they are used with any classes.
Q: Why is the interface List
in Java considered an "is a" relationship even though it is an interface?
A: Actually it has no relationship with classes. Only classes can have relationships except abstract classes.
In addition to that, you are saying that
in Java the classes ArrayList
and LinkedList
are related.
But it's absolutely wrong, because these classes are different implementation of the List
interface.
Why is an interface used?
The interface List
is a an abstraction of the list collection. It might have different implementations but the same behaviour.
It has been mentioned that interfaces are used for unrelated classes.
Actually it doesn't matter if classes have a relationship or not, interfaces are unrelated here.