• 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

inheritance queries...

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
stumbled upon these on one of the websites.

A class Car and its subclass Yugo both have a method run() which was written by the programmer as part of the class definition. If junker refers to an object of type Yugo, what will the following code do?

junker.show();

A. The show() method defined in Yugo will be called.
B. The show() method defined in Car will be called.
C. The compiler will complain that run() has been defined twice.
D. Overloading will be used to pick which run() is called.

the answer is B.

why isn't it A? if show() is present in Yugo example an overidden method, isn't it used first?

and here's another puzzling question:

Does a subclass inherit both member variables and methods?

A. No�only member variables are inherited.
B. No�only methods are inherited.
C. Yes�both are inherited.
D. Yes�but only one or the other are inherited.

the answer is D.

i don't understand the logic behind "but only one or the other are inherited".

isn't it C, where both are inherited?
say

class A

private int hi = 5

dothis() {
print(hi)
}

class B extends A

main method {
dothis()
}

doesn't B use both methods and member variables of A in this case?
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is this, a C# advocate's attempt at Java misinformation?

First, run() is defined in both Car and Yugo. What happens when you call show() on a variable declared as Yugo? Who knows? Nothing was said about the definition of a show() method. If the question should be "what happens if run() is called..." then the correct answer is D, but A is the result: run() of class Yugo is executed because junker is a Yugo and Yugo overrides run().

Second, both member variables (fields) and methods are inherited independently and in exactly the same way (all public, protected and package members). The answer is C.

[ November 09, 2004: Message edited by: David Harkness ]
[ November 09, 2004: Message edited by: David Harkness ]
 
Fendel Coulptz
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
does that mean the website posted incorrect information... -___-
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Fendel Coulptz:
does that mean the website posted incorrect information... -___-

The answer is D -- "All of the above". Perhaps you could post a link to the website in question. It's hard to make a judgment call from two questions, unless those were the only two there.
 
Have you no shame? Have you no decency? Have you no tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic