wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance Question" Watch "Inheritance Question" New topic

Inheritance Question

victor kamat
Ranch Hand

Joined: Jan 10, 2007
Posts: 247
This code has me confused:

public class T extends A {
private int k = 200;

public static void main (String[] args){
T t = new T();
System.out.println(t.doIt()); // 100 is output
System.out.println(t.k); // 200 is output


class A {
private int k = 100;
public int doIt() { return k; }

The output of doIt is 100. Class T inherits method doIt()from class A.
I would expect the output to be 200 of the doIt() call.
The value of k in the T class is 200 as the second println() shows.

Can somebody explain to me what I am missing ?
Anton Uwe
Ranch Hand

Joined: Jan 10, 2007
Posts: 122
"Class T inherits method doIt()from class A." Now, what does that mean (and what does it NOT mean)?

It means: You can call doIt() on an object of type T (although doIt() is defined in A, not in T), because a T is an A, too.

It does NOT mean: JVM, please act as if the method code would be in class T.
I agree. Here's the link: http://aspose.com/file-tools
subject: Inheritance Question
It's not a secret anymore!