We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,151 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Inheritance...

I have 3 very simple classes and a question about their inheritance.

public class A {
    int a;
    A(int a){
        this.a = a;
    }

    void meetoda(){
        System.out.println("a = " + a);
    }

    @Override
    public String toString() {
        return "A [a=" + a + "]";
    }

}




public class B extends A {
    int b;

    B(int a, int b){
        super(a);
        this.b = b;
    }

    void meetodb(){
        System.out.println("b = " + b);
    }

    @Override
    public String toString() {
        return "B [b=" + a + ", " + b + "]";
    }

}



public class TestABC {
    public static void main(String[] args) {
        A a3 = new A(45);
        B b3 = new B(34,28);
        a3 = b3;
        System.out.println(a3);
    }
}

When I print out a3, I get "B [b=34, 28]", but when I try to print a3.b, I get an error. Why?

4
Contributors
7
Replies
1 Day
Discussion Span
11 Months Ago
Last Updated
8
Views
Kert
Light Poster
25 posts since Feb 2012
Reputation Points: 21
Solved Threads: 0
Skill Endorsements: 0

Does class A have a b variable? a3 is defined as an instance of an A object.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

No, just a.
But why does printing out a3 uses toString() method from class B?

Kert
Light Poster
25 posts since Feb 2012
Reputation Points: 21
Solved Threads: 0
Skill Endorsements: 0

I guess that the toString() method is overriden.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Yes, it is. Then main problem for me is that it is 50% class A (no field b, no meetodb()) & 50% class B (uses toString() from B). What is the logic behind it?

Kert
Light Poster
25 posts since Feb 2012
Reputation Points: 21
Solved Threads: 0
Skill Endorsements: 0

Class A with overrides.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

But why does printing out a3 uses toString() method from class B?

it doesn't. but, you stated:

a3 = b3;

from that moment on, a3 actually is an instance of B, that's why it'll look for the toString method in the B class first, only if it doesn't find one there, it'll go to the parent class.

stultuske
Industrious Poster
4,377 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

Yes,it is as if doing an implicit casting of a child type object to a parent type reference.So a3 now refers to the child type object and during runtime,due to dynamic method despatching,it uses toString of B.

delta_frost
Junior Poster in Training
69 posts since Aug 2009
Reputation Points: 10
Solved Threads: 9
Skill Endorsements: 2

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0787 seconds using 2.76MB