5

I have abstract class Animal and inheritance classes Fish and Dog. I need to create various objects of fish and dogs and give them names and breeds and also make methods of the way they move. Finally i need to create an array of them and do 4 random prints that will show their name and breed.

Main Class:

public class JavaApplication38 {


public static void main(String[] args) {


   Animal[] arr = new Animal[20];

    arr[0] = new Fish("Riby", "Sea Fish");
    arr[1] = new Dog("Any", "Great Dane");
    arr[2] = new Fish("Ribytsa", "River fish");
    arr[3] = new Dog("Jackie", "Pug");
    arr[4] = new Fish("Bobi", "Mix");
    arr[5] = new Dog("Ruby", "Labrador");    

    }

}

Animal class

public abstract class  Animal {

  public Animal(String name, String breed){

  } 



   public Animal(){
   }

   public abstract void moving();

}

Dog class

Public class Dog extends Animal{


private String breed;
private String name;

public Dog(){
}

 public Pas(String name, String breed){
     this.name = name;
     this.breed =breed;
}


@Override
public void moving() {
    System.out.print("Walk\n");
}

}

Fish class

public class Fish extends Animal {


private String breed;


private String name;

public Fish(){

}

public Fish(String name, String breed){
    this.name = name;
    this.breed= breed;
}

@Override
public void moving(){
    System.out.print("Swims\n");
}

}

The question is, what do i have to write in a loop to print names and breeds via array?

2
  • 4
    Any particular reason you're not putting breed and name into the abstract Animal class? Commented Nov 29, 2018 at 21:53
  • 1
    for sure you should move those fields to super class and treat every entry as Animal instance Commented Nov 29, 2018 at 21:55

1 Answer 1

3

The problem was that you defined name and breed separately in each subclass of Animal. You need to make name and breed instance variables in Animal. That way, Java knows that every single Animal has a name and breed.

public abstract class Animal {
    private String name;
    private String breed;

    public Animal(String name, String breed) {
        this.name = name;
        this.breed = breed;
    }

    public getName() { return name; }
    public getBreed() { return breed; }

    public abstract void moving();
}

public class Dog extends Animal {
    public Dog(String name, String breed) {
        super(name, breed);
    }

    @Override
    public void moving(){
        System.out.print("Walks\n");
    }
}

public class Fish extends Animal {
    public Fish(String name, String breed) {
        super(name, breed);
    }

    @Override
    public void moving(){
        System.out.print("Swims\n");
    }
}

Now you can print the name and breed for any Animal, whether it's a Dog or Fish.

Animal[] arr = new Animal[6];

arr[0] = new Fish("Riby", "Sea Fish");
arr[1] = new Dog("Any", "Great Dane");
arr[2] = new Fish("Ribytsa", "River fish");
arr[3] = new Dog("Jackie", "Pug");
arr[4] = new Fish("Bobi", "Mix");
arr[5] = new Dog("Ruby", "Labrador");

for (Animal a : arr) {
   System.out.println(a.getName() + " " + a.getBreed());
   a.moving();
}
Sign up to request clarification or add additional context in comments.

2 Comments

arr[0] = new Fish("someName", "someBreed"); means that name and breed are assigned to fish constructor not animal constructor. When i compile it, it gives me null null for name and breed.
Whoops! You're right. I forgot to add constructors for Dog and Fish. I added them to the example. They accept a name and breed and then pass them to the superclass (Animal) constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.