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 My First Program:  Output ---> Frog@360be0 ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "My First Program:  Output ---> Frog@360be0 ?" Watch "My First Program:  Output ---> Frog@360be0 ?" New topic
Author

My First Program: Output ---> Frog@360be0 ?

Rob Chong
Greenhorn

Joined: Jan 26, 2004
Posts: 3
This is my first post to this site. Please bear with me.I have trouble to find out why the program I have written has produce an output of Frog@360be0. Can anyone please point to where I got it.

class Amphibian {
public void sound () { System.out.println("Croak...");}
static void makeSound(Amphibian a) {
a.sound();
}
public void populate() {
System.out.println("Populating...");
}
static void produce(Amphibian a) {
a.populate();
}

}
public class Frog extends Amphibian {
public static void main(String [] args) {
Frog croak = new Frog();
Amphibian.makeSound(croak);
System.out.println(croak);
Amphibian.produce(croak);
System.out.println(croak);
}
}
Output:
Croak...
Frog@360be0
Populating...
Frog@360be0
Press any key to continue . . .
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

When you call System.out.println(X), where 'X' is any object, Java calls the method "toString()" on that object, which returns a String; that String is then displayed.

Your Frog class doesn't define a toString() method, so it inherits the one from java.lang.Object. That method -- Object.toString() -- returns the name of the class, an '@' character, and the value returned by hashCode().

So the Frog@28876 you're seeing is just what Object.toString() returns.

Now, if you want something else to be printed -- like, say, "I'm a Frog", then you just have to define toString() in your Frog class, like this:



Now printing a Frog object will cause "I'm a Frog" to be displayed.


[Jess in Action][AskingGoodQuestions]
Rob Chong
Greenhorn

Joined: Jan 26, 2004
Posts: 3
Thank you for your reply. I've reworked my codes and try to demonstrate how overriding methods and upcasting can be performed. What I am not sure is whether my codes comply with the rules of overriding methods and upcasting.

Here is my codes:

class Amphibian {
public void sound () {}
static void makeSound(Amphibian a) { a.sound();}
public void populate() {}
static void produce(Amphibian a) {a.populate(); }

}
public class Frog2 extends Amphibian {
public void sound() {
System.out.println("I can croak.");
}
public void populate() {
System.out.println("We have populated.");
}

public static void main(String [] args) {
Frog2 croak = new Frog2();
Amphibian.makeSound(croak);
Amphibian.produce(croak);

}
}
/*
Ouput:
I can croak.
We have populated.
Press any key to continue . . .
*/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: My First Program: Output ---> Frog@360be0 ?