0

I am facing some problems about inheritance in Java. I can't understand why the following two programs have those outputs! Could anyone help me? :)

1)

public class A {
    int foo() {
        return 1;
    }
}

public class B extends A {
    int foo() {
        return 2;
    }
}

public class C extends B {
    int bar(A a) {
        return a.foo();
    }
}

C x = new C();  
System.out.println(x.bar(x));

// OUTPUT:2

2)

public class A {
    int e=1;
}

public class B extends A {
    int e=2;
}

public class C extends B {
    int bar(A a){
        return a.e;
    }
}

C x= new C();
System.out.println(x.bar(x));

// OUTPUT:1
0

1 Answer 1

1

In both cases, you're passing in an object of type C into the print function. The bar function asks for an object of type A, but it's still acceptable for you to pass in an object of type C since it is a subclass of A. So first of all, it's important to keep in mind that a.foo() and a.e are being called on a C object.

So what is happening in both cases is that it's searching for the lowest attribute or method in the list. Here is a very simplified version of what Java is doing in part 1:

  1. Hey, you've passed in an object of type C to the bar method! Now let's call its foo method.
  2. Whoops! C doesn't have a foo method! Let's take the next step up to the B class to see if it has a foo method.
  3. Yay! B has a foo method, so let's call it. No need to work our way up to the A class because we've already found what we need in B.

It's all about understanding that the parameter was downcast from A to C. The exact same sort of logic is used in part 2. It notices that an object of type C was passed in, so it gets the e attribute from object B since its the lowest class in the hierarchy that contains that attribute.

Hopefully that answers your question!

Sign up to request clarification or add additional context in comments.

6 Comments

You wrong about example 2 - there output is "1". The class B has its own member e, which is hidden for outside. If B wouldn't have the member e but set a value to it, we'd get an output "2", but it's not so we read a member's value of class A
Hm, with your logic the answer in the second program would be 2, wouldn't it?
@Johanna Is question for me?
No, the question is for Jake Fairbaim, but if you can explain it to me better, I don't have a problem :p
@Johanna I already did. Jake Fairbairn is right about example 1 - there's virtual method works, but in second one a member e from B doesn't overrides member from A. Class it's like a structrure: {members; [table of virtual funcions]}. When an app is executed, and you call a method, JVM looks for it in a table and calls function of a real object, but it can't resolve something like that for a member, so it just gives you e of A.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.