• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

which is better way of code reuse?

 
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is B really an A?
 
Ranch Hand
Posts: 157
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 69
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. I will keep these things in mind, it will be useful in future..
 
Greenhorn
Posts: 10
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
reply
    Bookmark Topic Watch Topic
  • New Topic