Board index » jbuilder » Re: Inheritance question.

Re: Inheritance question.


2003-07-30 07:36:17 AM
jbuilder22
Hi,
Methods "m" in "Hija" overloaded "m" in Padre not override it! because of
their difference in parameter types.
So in "Hija" there exists two different "m" methods, first one "m(A)" and
other one "m(B)".
when you call "h.m(b)" compiler (at compile time) sees that "h" is a "Padre"
(you declare it as a Padre), so it calls the "m(A)" which is available in
"Padre" and is not overrided.
by the way how can you expect "p" to be casted to "Hija" ? "p" is instance
of "Padre" and can not be casted to "Hija", and it can be determined at
runtime, not at compile time!
I think everything happens right as it has to, and nothing is strange in the
results.
Amir Pashazadeh
"Quique" < XXXX@XXXXX.COM >wrote in message
Quote
I've been programing in Java since 1.1.7. So I have some experience
with Java.
Recently I have a weird problem with inheritance. Here goes an
ilustrating code:

public class Prueba {
class Padre {
public void m(A a) { System.out.println("Padre"); }
}
class Hija extends Padre {
public void m(B b) { System.out.println("Hija"); }
}
class A {}
class B extends A {}

public Prueba() {
A a = new A();
B b = new B();

Padre p = new Padre();
Padre h = new Hija();

p.m(a); // prints: "Padre" <-- OK!
h.m(b); // prints: "Padre" <-- Why? I thought this should outputs
"Hija" instead of "Padre".

((Hija)h).m(b); // prints: "Hija" <-- OK!

try {
((Hija)p).m(b); // Rise ClassCastException.
// Why the compiler does not detect the error?
// Why the exception error and stack trace are
// outputed BEFORE anything else?

} catch (Exception ex) {
ex.printStackTrace();
}
}

public static void main(String[] args) {
new Prueba();
}
}

The questions are in the code. I'm using JBuilder 9 with bmj, so this
uses project javac. I tryed this code with j2sdk 1.4.1_02-b06 and
1.4.1_01-b01 having the same results.

tia,
Quique
 
 

Re:Re: Inheritance question.

Hi Amir!
I got confused when I call "h.m(b)". "h" is declared as "Padre" but
the object is really a "Hija". So, i thought that polimorfism applys.
Moreover I explicitly call "m(B)" because b is instance of B. I think
all of this is because the method calls is resolved at comile time.
But not always because all Java methods are virtual (speaking in C++).
May be the fact that B extends A has some influence.
And, I don't expect that "p" could be casted to "Hija". Just testing
if the compliler can catch this errors! The only thing I don't
understand is why the ClassCastException and its stack trace is
outpued BEFORE the "Padre", "Padre"
and "Hija".
Thanks,
Quique
"Amir Pashazadeh" < XXXX@XXXXX.COM >wrote in message news:<bg6tr9$lcqpv$ XXXX@XXXXX.COM >...
Quote
Hi,
Methods "m" in "Hija" overloaded "m" in Padre not override it! because of
their difference in parameter types.

So in "Hija" there exists two different "m" methods, first one "m(A)" and
other one "m(B)".

when you call "h.m(b)" compiler (at compile time) sees that "h" is a "Padre"
(you declare it as a Padre), so it calls the "m(A)" which is available in
"Padre" and is not overrided.

by the way how can you expect "p" to be casted to "Hija" ? "p" is instance
of "Padre" and can not be casted to "Hija", and it can be determined at
runtime, not at compile time!

I think everything happens right as it has to, and nothing is strange in the
results.

Amir Pashazadeh


"Quique" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...
>I've been programing in Java since 1.1.7. So I have some experience
>with Java.
>Recently I have a weird problem with inheritance. Here goes an
>ilustrating code:
>
>public class Prueba {
>class Padre {
>public void m(A a) { System.out.println("Padre"); }
>}
>class Hija extends Padre {
>public void m(B b) { System.out.println("Hija"); }
>}
>class A {}
>class B extends A {}
>
>public Prueba() {
>A a = new A();
>B b = new B();
>
>Padre p = new Padre();
>Padre h = new Hija();
>
>p.m(a); // prints: "Padre" <-- OK!
>h.m(b); // prints: "Padre" <-- Why? I thought this should outputs
>"Hija" instead of "Padre".
>
>((Hija)h).m(b); // prints: "Hija" <-- OK!
>
>try {
>((Hija)p).m(b); // Rise ClassCastException.
>// Why the compiler does not detect the error?
>// Why the exception error and stack trace are
>// outputed BEFORE anything else?
>
>} catch (Exception ex) {
>ex.printStackTrace();
>}
>}
>
>public static void main(String[] args) {
>new Prueba();
>}
>}
>
>The questions are in the code. I'm using JBuilder 9 with bmj, so this
>uses project javac. I tryed this code with j2sdk 1.4.1_02-b06 and
>1.4.1_01-b01 having the same results.
>
>tia,
>Quique