java - Difference between abstract class with all method abstract and interface? -
I had an interview where the interviewer asked me first that all the methods between the intangible class are summarized and the difference with an interface What is it
I replied that if there are some things to be successor in the future, then you will not be able to do this, if you have already increased the class. Then he said that it was a situation where no one would have to expand any other class and you have to implement a contract in this situation, which would be better, an abstract class or an interface?
I told them that you can use any of them but they were not satisfied. I could not understand why - I believe it's a developer / design choice.
Explaining that an interface represents an agreement, is not acceptable. This is the answer we give to Junior because it can be complicated to understand the difference without having much interface experience between abstract abstract and abstract of interface and without reading many classic books. Any abstract class with the An abstract class that does not provide any implementation 99% of cases is a representation of an object's role . A Interface represents a role . I am explaining this with this example : Your interviewer might say: I can run Based on this matter, he asks you: Do I have the convenience of walking in an abstract base class or an interface, knowing that there is nothing in the implementation? You think ... "Oh, I know: In this case, keeping an abstract class with an abstract method Why? Let's see the next expectations: One can eat If you implemented the facility of walking with an abstract class? You will end up with: How do you plug the Of course, you can only add a special feedback interface that is dedicated to humans only: The signature is created: Starting with the interface via interface is really preferred. With one interface, we can easily compose roles and the last solution is: Does this remind you of interface isolation theory ? ;) If you specify a IS-A connection, use an abstract class if you think that you role (we A Isabel-Off relationship is about model), go with the interface. public methods also work as an interface along with a contract.
Each object can have many different roles that should not be compromised robots and a
human can also run.
walking () , then clearly Even with the announcement of an interface, with the
walking () method. "Then your answer will definitely be:" This is the developer's choice! "And this is not always a valid answer.
human , but clearly the
robot is not required and even if it is not needed.
Biped Public Essentials class {Public Void Abstract Walking (); } Public Robot BPD (Walking Public Zero) [Running at 10 km / hr Speed}} Public Human Extended (Public Zero Walking) (Runs at 5/5 km / hr Speed)}
food / /> feature? You are stuck because you can not apply it to the
Biped base class because it will break the Liskov replacement principle , because
robot doesn ' Eat t! And you can not extend another base class due to the known java rule
human .
public interface feeds {zero} (none); }
Public Human Bipi Tool Expands {Clearly, it does not make any sense and a role Is misleading in implementing through
public interface Voil (running the abstract); } Public Interface Feedback {zero} (); } The implementation of public robots (walks of public zero) {// walking at 10 km / hr speed}} Public walkable, public (walkable at public speed) (public walks at 5 / km / h) () {// ...}}
Comments
Post a Comment