Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need a quick awnser! Will give good raiting for fast correct awnser. Thank you!

ID: 3701158 • Letter: N

Question

Need a quick awnser! Will give good raiting for fast correct awnser. Thank you!

Java Class and Inheritance Lab Practice Make a new class called Animal. Animal will have a default constructor that prints to the console "A new animal has been created." Animal will have a function called eat () that will print to the screen, "An animal is eating." Make a new class called Dog that extends Animal Dog will have a default constructor that prints to the console "A new dog has been created." This constructor should call the Animal constructor using super ( ) . Dog will also have the eat () function and it will override the Animal version. Dog's eat () function will print "A dog is eating." There should be no call to the super version of this This class will have an additional function, speak () that will print to the screen "A dog has barked." Make a new class called Puppy that extends Dog Puppy will have a default constructor that prints to the console "A puppy has been created." This constructor should call the Dog constructor using super ) . The eat function for puppy should just be inherited, nothing should change The speak() function should been overwritten for Puppy to print to the screen "A puppy has yipped." Your main function should create a new animal, a new dog, and a new puppy, and then call all of the functions for each object, resulting in a printout that is similar to this A new animal has been created. A new animal has been created. A new dog has been created. A new animal has been created. A new dog has been created. A new puppy has been created. An animal is eating. A dog is eating. A dog has barked. A dog is eating. A puppy has yipped

Explanation / Answer

Answer.)

Animal.java

package com.example.practice;

//Animal class
public class Animal {
//default constructor
public Animal() {
System.out.println("A new animal has been created");
}
//eat method of animal
public void eat(){
System.out.println("An animal is eating.");
}
}

Dog.java

package com.example.practice;

//Dog class
public class Dog extends Animal {
//default constructor
public Dog() {
//calling parent class default constructor
super();
System.out.println("A new dog has been created.");
}
//overriding eat() method of parent class
@Override
public void eat(){
System.out.println("A dog is eating.");
}
//speak method
public void speak(){
System.out.println("A dog has barked.");
}
}

Puppy.java

package com.example.practice;

//Puppy class
public class Puppy extends Dog {
//default constructor
public Puppy(){
//calling default constructor of parent class
super();
System.out.println("A new puppy has been created.");
}
//overriding parent class eat() method
@Override
public void eat(){
super.eat();
}
//overriding parent class speak() method
@Override
public void speak(){
System.out.println("A puppy has yipped.");
}
}

DriverMain.java

package com.example.practice;

//Driver class
public class DriverMain {
//Main method
public static void main(String[] args) {
//creating object of Animal
Animal animal=new Animal();
//creating object of Dog
Dog dog=new Dog();
//creating object of Puppy
Puppy puppy=new Puppy();
//calling eat() method of animal
animal.eat();
//calling eat() method of dog
dog.eat();
//calling speak() method of dog
dog.speak();
//calling eat() method of
puppy.eat();
//calling speak() method of puppy
puppy.speak();
}
}

Output :

A new animal has been created
A new animal has been created
A new dog has been created.
A new animal has been created
A new dog has been created.
A new puppy has been created.
An animal is eating.
A dog is eating.
A dog has barked.
A dog is eating.
A puppy has yipped.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote