-
Re: but
here u just call Cat's and animal's constructor,why the toString function
will be called?
u have not call the toString function...um this mean when u call a class,
all functions of the class will automatically called? or i got wrong concept?
"Niklas Junel" <niklas_junel@hotmail.com> wrote:
>
>import java.util.Random;
>public class Animal
>{
>
> public Animal(String aType)
> {
> type=new String(aType);
> }
>
> public String toString()
> {
> return "This is a " + type;
> }
>
> public void sound(){}
>
>
>
> private String type;
>
>//put Your main-method in the public class
>
> public static void main(String[] args)
> {
> Animal[] theAnimals={new Cat("Max","Abyssinian")};
>
> Animal Choice;
>
> Random select=new Random();
>
> for(int i=0;i<6;i++)
> {
> Choice = theAnimals[select.nextInt(theAnimals.length)];
>
> System.out.println("\nYour choice:\n"+Choice);
>
> Choice.sound();
> }
> }
>}
>
>//other class
>
>class Cat extends Animal
>{
> public Cat(String aName)
> {
> super("Cat");//call the base constructor
> name=aName;
> breed="Unknown";
> }
>
> public Cat(String aName, String aBreed)
> {
> super("Cat");
> name=aName;
> breed=aBreed;
> }
>
> public String toString()
> {
> return super.toString()+"\nIt's"+name+"the"+breed;
> }
>
> public void sound()
> {
> System.out.println("Miiaooww");
> }
>
> private String name;
> private String breed;
>}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks