help with objects

dzem68
02-10-2002, 07:03 PM
Ok, I know this code i am about to post is not vb but I cant find a site even remotely close to this one to post it on. So I hope you will bare with me and offer some help....the help i am requesting is more logic related rather than code specific.

can you explain why the output from the following java code produces "Hello from B3" and not "Hello from C3".

class A3{
void hello(){
System.out.println("Hello from A3");
}
void hello(int i) {
System.out.println("Hello from A3 " + i);
}
}
class B3 extends A3{
void hello(){
System.out.println("Hello from B3");
}
}
class C3 extends B3{
void hello(String s){
System.out.println("Hello from C3 " + s);
}
}
class MethodOverriding3{
public static void main(String args[]){
A3 obj = new C3();
obj.hello();
}
}




thanks,

dz

giladg22
02-11-2002, 02:34 AM
The hello in B3 is printed because you did not enter a string in
obj.hello(). The Compiler - not recognizing this function in C3 falls back on the previous B3 function which is recognized.
In essence, the hello function in B3 was not replaced by the hello function in C3. Try using obj.hello("mystring") and see if then you get the C3 function working.

dzem68
02-11-2002, 09:02 AM
thanks for the help. I think i get it now....class C3 inherrits all of the methods of dirived from A3. hello.obj was looking for a method with no params and that is the method in B3.

I tried passing a string from obj.hello but it errored out. I was able to pass an int and it returned Hello from A3 + int.

dz

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum