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;
>}