Is the implementation of the interface related to is-a?

Asked 1 years ago, Updated 1 years ago, 34 views

Is the interface and its implementation class related to is-a?

java

2022-09-30 21:34

1 Answers

Yes, that's right.
However, as long as you implement the class to meet the specifications you expect on the interface side.

Bis-aA refers to the relationship "Specifications for A are also specifications for B".

Now the specification is

  • What methods are available
  • What behavior is expected of the method
  • How does the class work throughout

and so on.

If you want to break this definition a little more, it means that giving B to the user code assembled based on the specification of A will not cause any strange things.
This is also referred to as Liskov Replacement Principle (LSP).

But this seems to be a necessary quality.
Why is Bis-a-subclass-of-A and
Do you bother to say is-a by using different words?

It's Bis-aA and cannot be called (i.e., it's a problem to replace) subclass
This is because there is a past that appeared due to a lack of design ideas.

Object orientation in the early days was the concept of differential programming
Differential programming is the idea of using inheritance to add new features to a feature that is not in the parent class.
That's why I was trying to increase productivity by reuse the code.

Differential programming overrides the parent-class code as you please.
At that time, you will be able to rewrite the specifications that you can expect from the method.

However, in real object-oriented languages, variables declared in the parent class can be substituted for subclass instances.
You won't know when and which subclass will be used until you follow the code.
Therefore, the user code will have to write a code that works well in all subclasses.
Therefore, you will not know if the code will work until you read all the subclass codes.
As a result, if you look at the whole thing, the code is getting more and more confused.

Differential programming is now considered an anti-pattern.

On the other hand, consider using inheritance in a relationship that can be said to be LSP or is-a.
Then, code users will only have to worry about the parent class specifications they want to use.

That's why we decided to divide is-a and LSP into inheritance and not use, and today's design method was born.

Now, when you implement an interface in a class, is it is-a?
The exact answer is
"Yes, You should try to be
is-a"
will be

There is no particular difference whether the parent is an interface or a class.
Bis-aA is "The specification of A is also the specification of B".

Read the comments on the specifications on the interface and the user's peripheral code carefully and
Implement the class to meet the specifications you expect.
This will enable you to provide a good class with is-a.

Or, when you create an interface, make sure that the specifications you expect are clear.

by leaving comments and showing typical sample classes and their tests The person who implements the interface encourages you to have a is-a relationship.


2022-09-30 21:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.