1

I already know the differences between interfaces and abstract classes because it's a well-known topic. I won't repeat those differences in this question. I have another questions. This answer raised a new question in me.

  • Is it accurate to say that the "interface" in oop provides a "has a" capability for classes?

  • Is the capability "has a" the same as the "has a" relation?

  • Are interfaces only used with unrelated classes?

In addition to that, For example in Java the classes ArrayList and LinkedList are related. Why is an interface used? (List) It has been mentioned that interfaces are used for unrelated classes.

If I am mistaken, please guide me accordingly.

Ali Dehkhodaei
  • 426
  • 3
  • 15
  • @Progman I edited my question and removed the ambiguous part. But why is the relationship "is a" applicable to the List interface? – Ali Dehkhodaei Jul 23 '23 at 11:16
  • Why do you think inheritance/interfaces describe a "has a" relationship? The linked answer doesn't say that. What is the problem in saying "An `ArrayList` is a `List` (implementation)"? – Progman Jul 23 '23 at 11:23
  • Interfaces not only have no "has a" relationship but also "is a" relationship. – Roman C Jul 23 '23 at 14:48

4 Answers4

1

Interfaces define a contract of methods that classes must implement. They allow unrelated classes to share common behavior and enable polymorphism.

The "has a" relationship represents composition in object-oriented programming, where one class contains an object of another class. It indicates a strong ownership relationship. The "is a" relationship is established through inheritance. When a class inherits from another class (or implements an interface), it is considered to be a specific type of that class or interface.

Interfaces are used to establish common behavior among unrelated classes. They enable unrelated classes to be treated uniformly based on their shared interface.

List Interface Example: In Java, the List interface represents a contract for list implementations. Classes like ArrayList and LinkedList implement the List interface, creating an "is a" relationship, indicating they fulfill the contract and behave as lists.

Summary: interfaces provide a way to define common behavior for unrelated classes, while the "has a" relationship signifies composition, and the "is a" relationship denotes inheritance. Interfaces enable unrelated classes to share common functionality, promoting polymorphism and code flexibility.

0

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.


Roman C
  • 49,761
  • 33
  • 66
  • 176
  • In Java 8, it is possible to have implemented methods in an interface. Thank you for the explanations, but I already know everything about abstract classes and interfaces. My question is about the specific points mentioned in the question. – Ali Dehkhodaei Jul 23 '23 at 11:37
  • You were guided to the topic of abstract classes and interfaces because you asked for it. Answers to your questions added to the answer. – Roman C Jul 23 '23 at 11:44
  • I have edited my question to better convey my intention. – Ali Dehkhodaei Jul 23 '23 at 13:32
  • You can edit it many times, but it didn't explain your problem. I have already answered all your questions. – Roman C Jul 23 '23 at 14:11
0

As you know, Java only allows single inheritance but allows you to implement as many interfaces as you want. You can think of that implementation as a form of inheritance because you override those methods similar to an abstract class.

Prior to Java 8, to use an interface meant having to implement all of the methods. Since Java 8 you can now add default methods that won't break client code implementing the interface but that extends the interface functionality.

Learn more here.

Hope this helps!

stujar
  • 5
  • 4
0

Is it accurate to say that the "interface" in oop provides a "has a" capability for classes?

No. The field which represents a "has a" relationship can be any type, interface, abstract or concrete. (it's not clear what "capability" means to you)

Is the capability "has a" the same as the "has a" relation?

The HAS-A relation is the relationship between a class which has a field of a type, and other classes which are of the type of that field. I don't understand what distinction you are drawing between "capability" and "relation".

Are interfaces only used with unrelated classes?

However we define "related", the answer is no. Any class can implement an interface.

Why is the interface List in Java considered an "is a" relationship even though it is an interface?

It's not. A single type doesn't have a relationship. An IS-A relationship is between two types, so an ArrayList IS-A List, and an ArrayList also IS-A AbstractList (which is an abstract class, not an interface), a RandomAccess and other interfaces.

tgdavies
  • 10,307
  • 4
  • 35
  • 40
  • `You'll need to define what "related classes" are.` For example Animal and Car are unrelated classes. For apply abstraction and a common method should i use interface or abstraction? – Ali Dehkhodaei Jul 23 '23 at 12:15
  • You'll need to define what *related* classes are. – tgdavies Jul 23 '23 at 12:19
  • You have a concept in your mind of "related" classes. I don't know what you mean by that. You'll need to define that. – tgdavies Jul 23 '23 at 12:32
  • I think this is correct: "Related classes in OOP have a meaningful association or inheritance hierarchy, while unrelated classes have no significant relationship." Base this sentence Animal and Car are unrelated classes and mobile and Laptop are related classes. – Ali Dehkhodaei Jul 23 '23 at 12:43
  • In that case I don't really understand the question "Are interfaces only used with unrelated classes?" Any class can implement interfaces and extend a parent class. So both `Animal` and `Car` may implement many interfaces and extend base classes, but they are unrelated. Likewise, two related classes can implement interfaces and extend base classes. – tgdavies Jul 23 '23 at 12:46
  • https://stackoverflow.com/a/33957698/11532220 This answer has mentioned this matter. – Ali Dehkhodaei Jul 23 '23 at 12:54
  • I've edited my answer. – tgdavies Jul 23 '23 at 12:59
  • I have edited my question to better convey my intention. – Ali Dehkhodaei Jul 23 '23 at 13:32
  • There's no relationship between interfaces or classes and interface. – Roman C Jul 23 '23 at 14:32