Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Help needed on Inheritance

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the question is like this....





Given the following code, which statements are true?

public interface HeavenlyBody { String describe(); }

class Star {
String starName;
public String describe() { return "star " + starName; }
}

class Planet extends Star {
String name;
public String describe() {
return "planet " + name + " orbiting star " + starName;
}
}

Select the two correct answers:

a.The code will fail to compile.

b.The use of inheritance is justified, since Planet is-a Star.

c.The code will fail to compile if the name starName is replaced with the name bodyName throughout the declaration of the Star class.

d.The code will fail to compile if the name starName is replaced with the name name throughout the declaration of the Star class.

e.An instance of Planet is a valid instance of HeavenlyBody



which one do you think suits correctly
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Above,

I guess it will compile fine with b as the correct option.

Regards,
Jothi Shankar Kumar. S
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
b. and c.
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd answer c and d. If starName is renamed in the body of the Star class, then it is not defined anywhere, but it is still referenced in the body of the Planet class.

I know the terms have been changed recently, but I believe that in astronomy it is incorrect to say that a planet is a star. Therefore, having a Planet class as a subclass of Star is very likely a design error.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi cowboys,

a. is surely false, as you can test yourself.
b. is showing that the question cannot turn up in the exam (see below)
c. is true, when you replace name by starName in class Star, class Planet can no longer compile as it uses starName, and this variable does no longer exist.
d. true, same as c.
e. I think, I'm not sure, though, that it's wrong. Interfaces don't have instances. An instance of a class implementing HeavenlyBody is of reference type HeavenlyBody, but I wouldn't call it an instance of this interface. My opinion.


About b:
b.The use of inheritance is justified, since Planet is-a Star.
That depends. Astronomers would say, it is wrong, a planet is not a star, so Planet should have a Star, not extend one.
But what about this:
Jake(having the exam next monday): Hey Jill, how is this bright star called, over there, near the horizon?
Jill: That's the evening star, also called Venus.

Astronomy is not in the exam's objectives. Not even elementary.


Yours,
Bu.
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Burkhard Hassel:

I think, I'm not sure, though, that it's wrong. Interfaces don't have instances. An instance of a class implementing HeavenlyBody is of reference type HeavenlyBody, but I wouldn't call it an instance of this interface. My opinion.


Yeah, the terminology is a bit dubious. I suppose you might say that "an object is an instance of interface X" as shorthand for "an object is an instance of a class that implements interface X". Certainly that usage pops up a couple of times in the Java API docs, and also fits how you would read the "instanceof" operator.

(And, of course, something like "new Planet() instanceof HeavenlyBody" evaluates false).
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

e.An instance of Planet is a valid instance of HeavenlyBody


Or as I'd say:
An instance of Planet can be stored in a variable of type HeavenlyBody.


My mistake:
Is false in any case as neither Star nor Planet actually implements any interface....


Yours,
Bu.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So finally the answers to above question are C and D. Do you conquer ?
 
reply
    Bookmark Topic Watch Topic
  • New Topic