aspose file tools*
The moose likes Beginning Java and the fly likes inheritance queries... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inheritance queries..." Watch "inheritance queries..." New topic
Author

inheritance queries...

Fendel Coulptz
Greenhorn

Joined: Sep 23, 2004
Posts: 24
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?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Sep 23, 2004
Posts: 24
does that mean the website posted incorrect information... -___-
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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.
 
wood burning stoves
 
subject: inheritance queries...