• 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

why the inheritance is not justified

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ranchers
how can be the ans is a b c
can any one explain

public interface Automobile { String describe(); }
class FourWheeler implements Automobile
{
String name;
public String describe(){ return " 4 Wheeler " + name; }
}
class TwoWheeler extends FourWheeler
{
String name;
public String describe(){ return " 2 Wheeler " + name; }
}






Select 3 correct options
a An instance of TwoWheeler is also an instance of FourWheeler.


b An instance of TwoWheeler is a valid instance of Automobile.


c The use of inheritance is not justified, since TwoWheeler is not a FourWheeler.


d The code will compile only if 'name' is removed from TwoWheeler.


e The code will fail to compile.

------------------------------------------------------------------
$ Velan Vel @ SCJP 1.4 $
You learn From Your Failures, Others Will Learn From Your Success
------------------------------------------------------------------
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because, Two wheeler is not a 4 wheeler.
Syntactically the code is correct , but logically not.
You wont find questions like this in exam
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
an instance of two wheeler is obviously an instance of automobile,hope u can get it.
Here the inheritance is not justified since the concept ogf inheritence is that u inherit something from your parents, but u can also add features to it. That means while we are moving from superclasses to subclasses in an inheritance hireararchy,the methods or members of the superclass will remain,only we can have some additional features to it. Thats why the kewword extends is used.
But here two wheelers never extends four wheelers, because its not an addition or spelization to a property of the four wheeler, but a change. At best two wheelers and four wheelers can both extend automobiles, or they might be a pair of siblinfg classes extending from a common parent class. This question is purely based on ur perception of the concept of inheritance and not like questions that 2+2 always makes 4. Hope I have not confused
 
velan vel
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so if we extends (or use inheritance then something should be
done in the subclass am i right correct me if i am wrong
------------------------------------------------------------------
$ Velan Vel @ SCJP 1.4 $
You learn From Your Failures, Others Will Learn From Your Success
------------------------------------------------------------------
 
Tan Richards
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
conceptually u should add more features, or specilize certain properties. Otherwise what is the need to inherit? But the code will of course run if u don't do anything in the subclass, or even drop some features(likw dropping the two wheels in youer problem).
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic