• Post Reply Bookmark Topic Watch Topic
  • New Topic

which is better way of code reuse?  RSS feed

 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
in an interview recently, I was asked about a question on inheritence. the situation is like this:



Now if we have to reuse this code in a class B, which one is a better way and why? The options are:



or



this is just a rough sketch, so am not following the proper code conventions...

thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is B really an A?
 
Bala Gangadhar
Ranch Hand
Posts: 119
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the question is really about IS-A vs HAS-A, then we need understand what exactly A and B are. How they relate to each other. If the question is purely technical (ignoring design principles and only considering the resources like memory and CPU) then it does not make any difference. Correct me if i am wrong.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except that we should never ignore design principles and only consider resource usage.

If B is not an A, then inheritance is completely out of the question.
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
k, the question put up was, if I have to use A in B, what will be more efficient, using A class inside B or extending A. and the reason for that choice... in a way, it will show why we shd be using inheritance...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only correct answer is to ask "What is the relationship between A and B?" Without that information, the question cannot be answered.
 
Paul Clapham
Sheriff
Posts: 22836
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:it will show why we shd be using inheritance...


Reusing code is not a good reason to use inheritance. So both of those examples are worse than some other code which doesn't use inheritance, for that version of "worse".
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:k, the question put up was, if I have to use A in B, what will be more efficient, ...

The first question to ask should not be what is more efficient, but what is conceptually correct and what isn't.

When you use inheritance, you should stick to the Liskov substitution principle. This roughly means that if B extends A, then it means that there is an is a relationship: a B is an A. For example you could have a class Animal and a class Monkey extends Animal. In this case, it's correct, because a Monkey is an Animal.

When you would use inheritance purely to reuse code, then you'll most of the time violate the Liskov substitution principle, which means you're misusing inheritance, which will make your program an incomprehensible mess.
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, I had told that B inheriting A would give B a chance to specialize A's methods. but the interview did not take that answer.. I was just stuck up.. not knowing what else to say..
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
s ravi chandran wrote:it will show why we shd be using inheritance...


Reusing code is not a good reason to use inheritance. ...
ergo the first answer (where B does not inherit from A, but instead uses an A) is better (if it is not given that B IS-An A).

An alternate example might be:

Which would be another case where you can re-use the code of A, but you could also pass in different implementations of A* to get different functionality without changing B. This would be an example of dependency injection. This isn't strictly an answer to your question, but is an extension to the reasoning that it is better not to rely on inheritance simply for code-reuse.

* = if you turned A into an interface, for example
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:well, I had told that B inheriting A would give B a chance to specialize A's methods. but the interview did not take that answer.. I was just stuck up.. not knowing what else to say..

Sometimes, the interviewer is not looking for a direct answer but rather the kind of replies given here. If you had argued along the lines that Bear, Jesper and others did, a good interviewer would probably have been impressed. On the other hand, if the interviewer was looking for something other than these, and had you given an answer that satisfied him, and he gave you the job and you would actually work for/with that guy... well, consider yourself lucky that you got stuck on the question.

On second thought, the interviewer might have been fishing for your knowledge of the advice to "Prefer composition over inheritance" -- I would say that's just a rule of thumb. Given that the class names A and B are not particularly useful, I would still argue along the lines that Bear and Jesper drew earlier.
 
Palak Mathur
Ranch Hand
Posts: 343
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
s ravi chandran wrote:well, I had told that B inheriting A would give B a chance to specialize A's methods. but the interview did not take that answer.. I was just stuck up.. not knowing what else to say..

Sometimes, the interviewer is not looking for a direct answer but rather the kind of replies given here. If you had argued along the lines that Bear, Jesper and others did, a good interviewer would probably have been impressed. On the other hand, if the interviewer was looking for something other than these, and had you given an answer that satisfied him, and he gave you the job and you would actually work for/with that guy... well, consider yourself lucky that you got stuck on the question.

On second thought, the interviewer might have been fishing for your knowledge of the advice to "Prefer composition over inheritance" -- I would say that's just a rule of thumb. Given that the class names A and B are not particularly useful, I would still argue along the lines that Bear and Jesper drew earlier.


Correct. That should have actually happen in the interview if interviewer would have really wanted to discuss rather than grill.
 
sandy sean
Ranch Hand
Posts: 69
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally Composition (HAS-A) is preffered over Inheritence (IS-A) if it is considered only for code resuse but it actually depends on the relation between the two classes.

Suppose your A is Vehicle class and B is Car class, then Car IS-A Vehicle so here inheritence comes into play.

class Car extends Vehicle{
}

But suppose A is Car class and B is Wheel class, then there is not a is-a relationship between Car and Wheel classes. we can't say Wheel IS-A Car but we can say Car HAS-A Wheel.

So there is Has-a relationship between them. Here Composition will be preffered over inheritence.

class Car{

Wheel wheelObj;
...
}



 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. I will keep these things in mind, it will be useful in future..
 
irrfan jamadar
Greenhorn
Posts: 10
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sandy sean wrote:Generally Composition (HAS-A) is preffered over Inheritence (IS-A) if it is considered only for code resuse but it actually depends on the relation between the two classes.

Suppose your A is Vehicle class and B is Car class, then Car IS-A Vehicle so here inheritence comes into play.

class Car extends Vehicle{
}

But suppose A is Car class and B is Wheel class, then there is not a is-a relationship between Car and Wheel classes. we can't say Wheel IS-A Car but we can say Car HAS-A Wheel.

So there is Has-a relationship between them. Here Composition will be preffered over inheritence.

class Car{

Wheel wheelObj;
...
}


THAT WAS GOOD EXPLANATION WITH EXAMPLE.
THANKS
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!