Problem with Concepts

F

FET

Hi everyone, I have a slight problem understanding the following
behaviour. I am citing 3 classes below:
class A{
public int c;

A(){
c = 5;
}

public void f(){
System.out.println("Inside A");
}
}

class B extends A{

public int c;

B(){
c = 10;
}

public void f(){
System.out.println("Inside B");
}
}


class Main{


public static void main(String args[]){
A a = new A();
B b = new B();

a = b;

System.out.println("C = "+a.c);
a.f();
}

}

On running Main, I get the output as:
C=5
Inside B


Even though I equate Base Class reference to Derived class reference,
Why is it that the data members of the Base class are accessed ?


Thanks in advance.

Regards.
 
T

Tony Morris

FET said:
Hi everyone, I have a slight problem understanding the following
behaviour. I am citing 3 classes below:
class A{
public int c;

A(){
c = 5;
}

public void f(){
System.out.println("Inside A");
}
}

class B extends A{

public int c;

B(){
c = 10;
}

public void f(){
System.out.println("Inside B");
}
}


class Main{


public static void main(String args[]){
A a = new A();
B b = new B();

a = b;

System.out.println("C = "+a.c);
a.f();
}

}

On running Main, I get the output as:
C=5
Inside B


Even though I equate Base Class reference to Derived class reference,
Why is it that the data members of the Base class are accessed ?


Thanks in advance.

Regards.

Class data members do not have the benefit of dynamic dispatch (where
methods do).
Therefore, because the data member c is accessed from a declared reference
of type A (even though that reference is referring to an object of type B),
that value of c is the one that is accessed.

When you call f(), which does have dynamic dispatch, B.f() is called.

I assume you understand what is happening by reassigning the 'a' object
reference.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
F

FET

thanks for your reply. But no, i dont quite have a good idea as to
what is happening when the object A is assigned object B. It would be
really great if you could elaborate or direct me to some sort of
reference (a link , maybe).
I actually was asked about this in an interview and I was unable to
answer it :-(
But I do want to know.

Thanks again,
Regards.

Tony Morris said:
FET said:
Hi everyone, I have a slight problem understanding the following
behaviour. I am citing 3 classes below:
class A{
public int c;

A(){
c = 5;
}

public void f(){
System.out.println("Inside A");
}
}

class B extends A{

public int c;

B(){
c = 10;
}

public void f(){
System.out.println("Inside B");
}
}


class Main{


public static void main(String args[]){
A a = new A();
B b = new B();

a = b;

System.out.println("C = "+a.c);
a.f();
}

}

On running Main, I get the output as:
C=5
Inside B


Even though I equate Base Class reference to Derived class reference,
Why is it that the data members of the Base class are accessed ?


Thanks in advance.

Regards.

Class data members do not have the benefit of dynamic dispatch (where
methods do).
Therefore, because the data member c is accessed from a declared reference
of type A (even though that reference is referring to an object of type B),
that value of c is the one that is accessed.

When you call f(), which does have dynamic dispatch, B.f() is called.

I assume you understand what is happening by reassigning the 'a' object
reference.
 
T

Tony Morris

FET said:
thanks for your reply. But no, i dont quite have a good idea as to
what is happening when the object A is assigned object B. It would be
really great if you could elaborate or direct me to some sort of
reference (a link , maybe).
I actually was asked about this in an interview and I was unable to
answer it :-(
But I do want to know.

Thanks again,
Regards.

"Tony Morris" <[email protected]> wrote in message
FET said:
Hi everyone, I have a slight problem understanding the following
behaviour. I am citing 3 classes below:
class A{
public int c;

A(){
c = 5;
}

public void f(){
System.out.println("Inside A");
}
}

class B extends A{

public int c;

B(){
c = 10;
}

public void f(){
System.out.println("Inside B");
}
}


class Main{


public static void main(String args[]){
A a = new A();
B b = new B();

a = b;

System.out.println("C = "+a.c);
a.f();
}

}

On running Main, I get the output as:
C=5
Inside B


Even though I equate Base Class reference to Derived class reference,
Why is it that the data members of the Base class are accessed ?


Thanks in advance.

Regards.

Class data members do not have the benefit of dynamic dispatch (where
methods do).
Therefore, because the data member c is accessed from a declared reference
of type A (even though that reference is referring to an object of type B),
that value of c is the one that is accessed.

When you call f(), which does have dynamic dispatch, B.f() is called.

I assume you understand what is happening by reassigning the 'a' object
reference.

"dynamic dispatch" of a method call means that the method that is called is
that of the "object type", not the reference type that is used to perform
the call.
Object o = new SomeClass();
String s = o.toString();

o is an object of type SomeClass, method calls are ALWAYS dynamically
dispatched in Java (unlike other languages), therefore, SomeClass's
toString() method is called (if it doesn't have one, it's superclass'
toString method is called, etc.).
The fact the reference is of type Object is irrelevant, since dynamic
dispatch (aka polymorphism, run-time binding) occurs on the object type
(excuse the redundant explanation, I can't find another way of saying it).

The same cannot be said for class data members. The reference type is that
which is used, since dynamic dispatch does not occur.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
Home : +61 7 5502 7987
Work : +61 7 5552 4076
Mobile : 0408 711 099
(2003 VTR1000F)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top