Inheritance

K

kathukutti

Hi all!!!!!!!

consider the following inheritance hierarchy
Class A --> Class B --> Class C

As per rule: Object of A can be referenced by their subclass objects.
Eg: consider a,b,c are objects of classes A,B,C . Here a=b &a=c is
applicable but 'object a' can access only its own member nor the
members of 'B & C'

question: But consider All the classes (A,B,C) override a single
method namely disp(). Now
case 1: a=b; a.disp();==> calls the Class B's disp() method though it
is not a member of A.
IIIy for a=c; a.disp()

Thanx in advance!
 
O

Oliver Wong

kathukutti said:
Hi all!!!!!!!

consider the following inheritance hierarchy
Class A --> Class B --> Class C

You should clarify whether A inherits from B or B inherits from A.
As per rule: Object of A can be referenced by their subclass objects.
Eg: consider a,b,c are objects of classes A,B,C . Here a=b &a=c is
applicable but 'object a' can access only its own member nor the
members of 'B & C'

"&a=c" doesn't make sense in the context of Java.
question: But consider All the classes (A,B,C) override a single
method namely disp(). Now
case 1: a=b; a.disp();==> calls the Class B's disp() method though it
is not a member of A.
IIIy for a=c; a.disp()

I don't see a question here, just a bunch of statements.

- Oliver
 
R

Robert Klemme

You should clarify whether A inherits from B or B inherits from A.


"&a=c" doesn't make sense in the context of Java.


I don't see a question here, just a bunch of statements.

And I see only "case 1" but no other cases. Also case 1 is already
wrong, because first OP states that all override method disp() and then
he says it's not member of A. Now what?

Weird...

robert
 
M

Mark Space

Robert said:
And I see only "case 1" but no other cases. Also case 1 is already
wrong, because first OP states that all override method disp() and then
he says it's not member of A. Now what?

Weird...


I assume all this results from not understanding one's homework question.
 
O

Owen Jacobson

Hi all!!!!!!!

consider the following inheritance hierarchy
Class A --> Class B --> Class C

As per rule: Object of A can be referenced by their subclass objects.
Eg: consider a,b,c are objects of classes A,B,C . Here a=b &a=c is
applicable but 'object a' can access only its own member nor the
members of 'B & C'

question: But consider All the classes (A,B,C) override a single
method namely disp(). Now
case 1: a=b; a.disp();==> calls the Class B's disp() method though it
is not a member of A.
IIIy for a=c; a.disp()

Thanx in advance!

1. Ask a question.

2. Show the code, not a description of the code. Simplify it if you
need to, but make sure it compiles (or if not, comment the lines which
provoke errors from the compiler).
 
T

Tor Iver Wilhelmsen

As per rule: Object of A can be referenced by their subclass objects.
Eg: consider a,b,c are objects of classes A,B,C . Here a=b &a=c is
applicable but 'object a' can access only its own member nor the
members of 'B & C'

Correct: The type of the reference a is A, so the compiler only knows
about A's methods.
question: But consider All the classes (A,B,C) override a single
method namely disp(). Now
case 1: a=b; a.disp();==> calls the Class B's disp() method though it
is not a member of A.
IIIy for a=c; a.disp()

Here you come in contact with virtual methods: The code that is invoked,
even though the method signature is looked up in the class A is the code
of the actual object's class (B or C).

So there you have it: There is a difference between the type of a
reference (used by the compiler to restrict what operations can be
performed) and the type of the object (used by the runtime to decide which
actual operation takes place).
 

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

Forum statistics

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

Latest Threads

Top