Question

Add an animal (similar to cat) to the AnimalInheritance Example. class Animal { public Animal() {...

Add an animal (similar to cat) to the AnimalInheritance Example.

class Animal

{

public Animal()

{

System.out.println("A new animal has been

created!");

}

public void sleep()

{

System.out.println("An animal sleeps...");

}

public void eat()

{

System.out.println("An animal eats...");

}

}

public class Cat extends Animal

{

public Cat()

{

super();

System.out.println("A new cat has been

created!");

}

public void sleep()

{

System.out.println("A cat sleeps...");

}

public void purr()

{

System.out.println("A cat purrs...");

}

public void eat()

{

System.out.println("An cat eats...");

}

public static void main(String args[]){

Cat demo = new Cat();

demo.eat();

demo.sleep();

demo.purr();

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the updated code below:

TestAnimals.java

package classes9;

class Animal

{

public Animal()

{

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

}

public void sleep()

{

System.out.println("An animal sleeps...");

}

public void eat()

{

System.out.println("An animal eats...");

}

}

class Cat extends Animal

{

public Cat()

{

super();

System.out.println("A new cat has been created!");

}

public void sleep()

{

System.out.println("A cat sleeps...");

}

public void purr()

{

System.out.println("A cat purrs...");

}

public void eat()

{

System.out.println("A cat eats...");

}

}

class Dog extends Animal

{

public Dog()

{

super();

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

}

public void sleep()

{

System.out.println("A Dog sleeps...");

}

public void bark()

{

System.out.println("A Dog barks...");

}

public void eat()

{

System.out.println("A Dog eats...");

}

}

public class TestAnimals{

public static void main(String args[]){

Cat cat = new Cat();

cat.eat();

cat.sleep();

cat.purr();

System.out.println();

Dog dog = new Dog();

dog.eat();

dog.sleep();

dog.bark();

}

}

output:

Console X terminated> TestAnimals [Java Application] CAProgram Files Java jre71b A new animal has been created! A new cat has been created! A cat eats.. . A cat sleeps... A cat purrs. . . urrs. . - A new animal has been created! A new dog has been created! ats...) A Dog sleeps... A Dog barks...

Add a comment
Know the answer?
Add Answer to:
Add an animal (similar to cat) to the AnimalInheritance Example. class Animal { public Animal() {...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with this Java question in 1 hour please JAVA Using the files from the...

    Need help with this Java question in 1 hour please JAVA Using the files from the in-class assignment add two additional animals of your choice. For each animal add two additional methods appropriate to your particular animal. As the majority of the code for this program exists you will not need an algorithm. Use the 3 java classes below. Thank you //Animal.java public class Animal {    public Animal() {    System.out.println("A new animal has been created!");    }   ...

  • Examples: Example 1-Runtime Polymorphism: Lets say we have a class Animal that has a method sound()....

    Examples: Example 1-Runtime Polymorphism: Lets say we have a class Animal that has a method sound(). Since this is a generic class so we can't give it a implementation like: Roar, Meow, Oink etc. We had to give a generic message public class Animal public void sound System.out.println("Animal is making a sound"); Now lets say we a subclass of Animal class. Horse that extends Animal class. We can provide the implementation to the same method like this: class Horse extends...

  • What is wrong with the following Java Code. public class TestEdible { abstract class Animal {...

    What is wrong with the following Java Code. public class TestEdible { abstract class Animal { /** Return animal sound */ public abstract String sound(); } class Chicken extends Animal implements Edible { @Override public String howToEat() { return "Chicken: Fry it"; } @Override public String sound() { return "Chicken: cock-a-doodle-doo"; } } class Tiger extends Animal { @Override public String sound() { return "Tiger: RROOAARR"; } } abstract class Fruit implements Edible { // Data fields, constructors, and methods...

  • public class Polymorph public static class CAL public void printHello System.out.println("Hya"); public static class w extends...

    public class Polymorph public static class CAL public void printHello System.out.println("Hya"); public static class w extends CAC public void printHello System.out.println("Howdy"); public static class PB extends WE public void printtello System.out.println("Hello") public static void main(String[] args) { A l = new CÁO: CA ab - (CA) new W(); CA ac - (CA) new PC); Wbc - (W) new P80; 33.printHello(); ab.printHello(); ac.printHello(); be.printHello(); Type your answer, do not copy/paste

  • 2016 points output of following Java Program? public class Base { public final void show() {...

    2016 points output of following Java Program? public class Base { public final void show() { . System.out.println("Base::show() called"); public class Derived extends Base { public void show() { System.out.println("Derived::show() called"); } e lor rested in order public class Main { ects from and public static void main(String[] args) { Base b = new Derived(); b.show();

  • What is wrong with this Code? Fix the code errors to run correctly without error. There...

    What is wrong with this Code? Fix the code errors to run correctly without error. There are seven parts for one code, all are small code segments for one question. All the 'pets' need to 'speak', every sheet of code is connected. Four do need need any Debugging, three do have problems that need to be fixed. Does not need Debugging: public class Bird extends Pet { public Bird(String name) { super(name); } public void saySomething(){} } public class Cat...

  • The method m() of class B overrides the m() method of class A, true or false?...

    The method m() of class B overrides the m() method of class A, true or false? class A int i; public void maint i) { this.is } } class B extends A{ public void m(Strings) { 1 Select one: True False For the following code, which statement is correct? public class Test { public static void main(String[] args) { Object al = new AC: Object a2 = new Object(); System.out.println(al); System.out.println(a): } } class A intx @Override public String toString()...

  • What is the output of the following code? class C1 { public double PI=3.1; public void...

    What is the output of the following code? class C1 { public double PI=3.1; public void m1() { System.out.println("M1 of C1"); } } class C2 extends C1 { public double PI=3.14; public void m1() { System.out.println("M1 of C2"); } } public class C { public static void main(String[] args) { C1 obj = new C2(); System.out.print(obj.PI); } } 3.1 3.14 None of the above

  • What is the output of the following code? class C1 { public double PI=3.1; public void...

    What is the output of the following code? class C1 { public double PI=3.1; public void m1() { System.out.println("M1 of C1"); } } class C2 extends C1 { public double PI=3.14; public void m1() { System.out.println("M1 of C2"); } } public class C { public static void main(String[] args) { C1 obj = new C2(); obj.m1(); } } M1 of C1 M1 of C2 None of the above

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT