• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Another question from Khalid

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again,
There is one more question in Khalid and R.R. I am little confused with the answer. The q no. is 6.26 and it is as follows.
Q. Given the following code, which statements are true?
public interface HeavenlyBody { String describe(); }
class Start {
String starName;
public String describe() { return "star " + starName; }
}
class Planet extends Star {
String name;
public String describe() {
return "Planet " + name + "orbiting star " + starname;
}
}
Select all valid answers.
a) ----
b) The use of inheritance is justified, since planet is-a star.
c) --
d) --
e) --
I thought option b is valid since class Planet extends class Star. but the book does not give b) as the valid answer. Their explanation is as follows:
The use of inheritance in this code is not justifiable, since conceptually, a planet is-not-a star.
Could anyone please tell me why Planet is-not-a star?
Thanks in advance.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
A star is defined as a large ball of hot gas, thousands to millions of kilometers in diameter. A planet is defined as a non-luminous body that revolves around a central star. Stars differ fundamentally from planets in that they are self-luminous, where planets shine by reflected light from their near-by sun.
Although planets and stars are related, there does not exist the "is-a" relationship that good inheritance demands. You could probably use inheritance to define a relationship between black-hole's and stars, or white-dwarves and stars, as these objects are closely related.
hth...
Pat B.
Pat B.
 
Seema Das
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
But when class B extends class A, at that time, do we consider what A and B mena in real world?
I thought, if class B extends class A in the code, then B has "is-a" relationship with A.
If this kind of question comes in the test, should I think what planet and star mean in real world or should I just see how it is defined in the code?
I am little confused.
Please help.
 
Pat Barrett
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Technically, you could have any class extend any other class. However, good OOP design demands that there be an "is-a" relationship between a class and any other classes that may inherit from it. This is one reason why OOP is so versitile as it allows for easy expndability and extensibility.
The classic example given is that of the Shape class. Conceptually, it's not too hard to envision that a Square or a Circle are both Shapes (a square "is-a" shape, a circle "is-a" shape), yet they look quite a bit different. I believe that the lack of the same "is-a" relationship between stars and planets is the reason for the unexpected answer on the Khalid question. There's no technical reason why you can't have a planet inherit from a star, there's just not a good conceptual one.
Pat B.
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic