Question

I want to create a Zoo class, which keeps track of all the animals in the...

I want to create a Zoo class, which keeps track of all the animals in the zoo using an Animal class. Would inheritance make sense here? Why, or why not? Explain your reasoning clearly.

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

What is Inheritance:

  • Inheritance is a mechanism wherein a new class is derived from an existing class.
  • Classes may inherit or acquire the properties and methods of other classes.
  • Derived class: A class that inherits another class is known as derived class or sub class or child class.
  • Base class: The class that is being inherited by other class is known as base class, super class or parent class.
  • Following diagram demonstrate inheritance.

​​​Since you want to make a class Zoo and for that class Inheritance make sense.

  • First of all we will create the Animal base class.
  • The central idea is that we have a group of animals living inside a zoo.
  • This group of animals consists of Lions, Tigers, Cats, Hippos, Wolfs , Dogs and many more.
  • They can do certain things like sleeping, roaming, making noise or eating.
  • Along with these things that they can do they also carry specific characteristics like the kind of food they prefer (meat or grass), how much hungry they are at a specific moment and their location in the zoo.
  • One picture of every animal. This is also a key characteristic of every animal.

Using any programming language of your choice you can create a base class Animal and inherit other class like Dogs, Lions etc from the base class.

For example,

Step 1:

a) First step is to create a base class Animal.

b) In this class define some methods e.g. sleep, roam, eat etc. which are common to all the animals.

c) Define some properties which are common to all the animals e.g. food, hunger, picture etc.

d) Refer the diagram:

​​​​​​

Step 2:

​​​​​​a) Create another class e.g. Lion, Cat, Dog, Hippo.

b) For this derived class, you can inherit the methods and properties defined in the base class.

c) Refer the diagram:

Step 3:

Using this inheritance concept you can create multiple classes and reuse the methods and properties defined in the base class.

Advantages of using inheritance:

  • Promotes code reusability.
  • When a class inherits or derives another class, it can access all the functionality of inherited class.
  • Reusability enhanced reliability. The base class code will be already tested and debugged.
  • Since the existing code is reused, it leads to less development and maintenance costs.
  • Inheritance makes the sub classes follow a standard interface.
  • Helps to reduce code redundancy and supports code extensibility.
  • Facilitates creation of class libraries.
Add a comment
Know the answer?
Add Answer to:
I want to create a Zoo class, which keeps track of all the animals in the...
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
  • Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and...

    Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and a Zoo class that holds all the animals. You should create a general Animalclass where all other classes are derived from (except for the Zoo class). You should then create general classes such as Mammal, Reptile, and whatever else you choose based off of the Animalclass. For...

  • Provided code Animal.java: public class Animal {    private String type;    private double age;   ...

    Provided code Animal.java: public class Animal {    private String type;    private double age;       public Animal(String aT, double anA)    {        this.type = aT;        if(anA >= 0)        {            this.age = anA;        }    }    public String getType()    {        return this.type;    }    public double getAge()    {        return this.age;    } } Provided code Zoo.java: public class Zoo {...

  • Please help me with the following question. This is for Java programming. In this assignment you...

    Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and a Zoo class that holds all the animals. You should create a general Animalclass where all other classes are derived from (except for the Zoo class). You should then create general classes such as Mammal, Reptile, and whatever else you choose based off of the Animalclass. For...

  • I need some help i need to do this in C# Objectives: • Create an application...

    I need some help i need to do this in C# Objectives: • Create an application that uses a dictionary collection to store information about an object. • Understanding of abstract classes and how to use them • Utilize override with an abstract class • Understanding of Interfaces and how to use them • Implement an Interface to create a “contract” between classes. • Compare and contrast inheritance and interfaces. Instructions: Interface: Create an interface called ITrainable which contains the...

  • UML Class Diagram with Inheritance Objectives Use UML Correctly indicate inheritance Demonstrate permissions Understand inheritance relationships...

    UML Class Diagram with Inheritance Objectives Use UML Correctly indicate inheritance Demonstrate permissions Understand inheritance relationships Labwork Please read all of the directions carefully. You will create a UML class diagram reflecting the class hierarchy for a fictional program that manages university personnel as constructed according to the graph displayed below. You will need to think about the attributes and behaviors that are unique to each class, and also those attributes and behaviors that are common amongst the subclasses and...

  • i have problem trying to create the class in this python 3 promblem In this problem...

    i have problem trying to create the class in this python 3 promblem In this problem you will be creating a Caterpillar class that will draw a caterpillar using turtle graphics. Your caterpillar object will contain the following information: 1. Body color (default "green") 2. Legs color (default "purple") 3. Body size (the radius of the 5 circles that make the caterpillars body) (default- 50) You are going to want to create a turtle object that you will use to...

  • The U of C graduating class of 2019 elects to give Hyde Park a tiny zoo...

    The U of C graduating class of 2019 elects to give Hyde Park a tiny zoo with two adorable pandas. The zoo and pandas cost a total of 10 million dollars. There is no admission charge, and the equilibrium line length to see the pandas is 10 minutes. Professor Landsburg* visits the campus, and he notes that instead of going to see the pandas, Hyde Park residents always have the option of going to the lakeside, which is large enough...

  • Question 51 2 pts Which moral stance is taken toward the rights of animals by William...

    Question 51 2 pts Which moral stance is taken toward the rights of animals by William Baxter? o Other animals have no rights at all--only human beings have rights. Except for rights like voting, humans and animals have the same rights. The fact that animals come from different species is morally irrelevant The rights of animals are extremely limited, mostly just the right to life. Question 52 2 pts In regard to the rights of animals, which is morally permissible...

  • IN C# Objectives: Create an application that uses a dictionary collection to store information ...

    IN C# Objectives: Create an application that uses a dictionary collection to store information about an object. Understanding of abstract classes and how to use them Utilize override with an abstract class Understanding of Interfaces and how to use them Implement an Interface to create a “contract” between classes. Compare and contrast inheritance and interfaces. Instructions: Interface: Create an interface called ITrainable which contains the following: Dictionary<string, string> Behaviors{ get; set; } string Perform(String signal); string Train(String signal, string behavior);...

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