I have a question about Java inheritance.

Asked 2 years ago, Updated 2 years ago, 20 views

Look at the Main Class and complete the Animal, Bird, and Dog classes using the following output. # Execution result #

A new animal has been created!

A new animal has been created!

A new bird has been created!

A new animal has been created!

A new dog has been created!

An animal sleeps...

An animal eats...

A bird sleeps...

A bird eats...

A dog sleeps...

A dog eats...

A new animal has been created!

A new dog has been created!

A new animal has been created!

A new bird has been created!

A dog eats...

A bird sleeps...


public class MainClass {

  public static void main(String[] args) {

  Animal animal = new Animal();

  Bird bird = new Bird();

  Dog dog = new Dog();



  System.out.println();



  animal.sleep();

  animal.eat();



  bird.sleep();

  bird.eat();



  dog.sleep();

  dog.eat();



  Animal a1 = new Dog();

  Animal a2 = new Bird();



  a1.eat();

  a2.sleep();



    }

}


public class Animal {

void sleep() { System.out.println("An animal sleeps...");}

void eat() { System.out.println("An animal eats...");}

}


public class Bird extends Animal{

void sleep() { System.out.println("A bird sleeps...");}

void eat() { System.out.println("A bird eats...");}

}

public class Dog extends Animal{

void sleep() { System.out.println("A dog sleeps...");}

void eat() { System.out.println("A dog eats...");}

}

This question is incomplete Don't touch the main class Only Animal, Dog, Bird classes need to be modified, but I don't know what to do here. The part where I'm stuck is the execution result up there

A new animal has been created!

A new animal has been created!

A new bird has been created!

A new animal has been created!

A new dog has been created!

This part and below

A new animal has been created!

A new dog has been created!

A new animal has been created!

A new bird has been created! I don't know how to do this part. Please save me

java

2022-09-21 17:35

1 Answers

You can use the constructor.

Since the Animal class is inherited from Bird and Dog classes, when you create objects in Bird and Dog classes, the creator of Animal, the parent class, and the creator of Bird and Dog classes, the child class, runs.


public class Animal {

    Animal(){
        System.out.println("A new animal has been created!");
    }

    void sleep() { System.out.println("An animal sleeps...");
    }

    void eat() { System.out.println("An animal eats...");
    }

}
public class Bird extensions Animal {

    Bird(){
        System.out.println("A new bird has been created!");
    }

    void sleep() { System.out.println("A bird sleeps...");
    }

    void eat() { System.out.println("A bird eats...");
    }

}
public class Dog extensions Animal {

    Dog(){
        System.out.println("A new dog has been created!");
    }

    void sleep() { System.out.println("A dog sleeps...");
    }

    void eat() { System.out.println("A dog eats...");
    }

}


2022-09-21 17:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.