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 public methods also work as an interface along with a contract.

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 .
Each object can have many different roles that should not be compromised

I am explaining this with this example :

Your interviewer might say: I can run robots and a human can also 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 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.

Why? Let's see the next expectations: One can eat human , but clearly the robot is not required and even if it is not needed.

If you implemented the facility of walking with an abstract class? You will end up with:

  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)}   

How do you plug the 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 .

Of course, you can only add a special feedback interface that is dedicated to humans only:

  public interface feeds {zero} (none); }   

The signature is created: Public Human Bipi Tool Expands {Clearly, it does not make any sense and a role Is misleading in implementing through

Starting with the interface via interface is really preferred.

With one interface, we can easily compose roles

and the last solution is:

  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) () {// ...}}   

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.

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -