File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes plz tell me about my prolem? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "plz tell me about my prolem?" Watch "plz tell me about my prolem?" New topic
Author

plz tell me about my prolem?

Van Cam
Greenhorn

Joined: Aug 05, 2006
Posts: 13
I have code
public class Adder {
public int addThem(int x,int y){
return x + y;
}
public double addThem(double x, double y){
return x + y;
}

}

public class TestAdder {
public static void main(String[] args) {
Adder a = new Adder();
int b = 27;
int c = 3;
int result = a.addThem(b,c);//Compile is OK
//Adder a1 = new Adder();
double m = 494.55;
double n= 123.35;
double doubleResult a1.addThem(m,n);//Why Compile error???
}

}

plz tell me more.
Thanks
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
The line with the compile error is not a valid statement. Missing the equals sign. Once you put the equals sign in it will tell you that it does not recongize a1. Well, a1 is commented out.
[ December 02, 2006: Message edited by: Carol Enderlin ]
Van Cam
Greenhorn

Joined: Aug 05, 2006
Posts: 13
Thanks you very much for you reply!
I am sorry for my stup*d and my confused!
I have new question,Please help me to understands
Here is code !

public class Animal {
public void eat(){
System.out.println("Generic animal eating generically");
}

}
/*******************************************************************/

public class Horse extends Animal {
public void eat(){
System.out.println("Horse eating hay");
}
public void eat(String s){
System.out.println("Horse eating "+ s);
}

}
/******************************************************************/
public class TesAnimals {
public static void main(String[] args) {
Animal a = new Animal();
Animal ah = new Horse();
Horse he = new Horse();
a.eat();//Ok,yes I understand
a.eat("treats");//Compile error,but I understands
ah.eat();//Polymorphism works,I am OK
he.eat("Apples");//I am understands
ah.eat("Carrots");//I dont understands,

}
}

I dont understand this ,only because Horse is an instanceof Animal type,so Horse have a method eat(String s)
so ah variable can call it when compile but when compile is it is error,Plz explanation for me.
Thanks in advance!
Regards,
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi Van Cam,

your "ah" cannot eat carrots.
You coded:
Animal ah = new Horse();

So the variable is polymorph.
Its reference type is Animal.
Its object type is Horse();

What methods "ah" can call at all depends only on the reference type. "ah" can call methods of type Animal (and of supertype object) only. Animal does not have a method void eat(String s). It doesn't matter here, that the object type is Horse!

When it has a method, e.g. void eat(), it calls this method from the object type, if it is overridden. Only in this case the object type matters.

But what if "ah" gets hungry and needs some vitamine A?
You can cast this variable:
Horse castedHorse = (Horse) ah; // explicit downcast
castedHorse.eat("Carrots");

then the reference type is Horse and it can perform the method with the String.


Yours,
Bu.


all events occur in real time
Van Cam
Greenhorn

Joined: Aug 05, 2006
Posts: 13
Hi Burkhard Hassel
Thanks you,Thanks you,I understood what you said.
This is great forums!
Regards,
[ December 03, 2006: Message edited by: Van Cam ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: plz tell me about my prolem?