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

1) Write a class called Animal that has a single private instance variable of ty

ID: 654040 • Letter: 1

Question

1) Write a class called Animal that has a single private instance variable of type int that represents the number of legs the animal has. Override the equals method (inherited from Object) so that it returns false if the reference passed in is null, or is not the same type as this, or doesn't have the same number of legs as this, otherwise true. Override the toString method inherited from Object so that it returns the message "I am an Animal object with N legs".

2) Write a class called Person that extends Animal. Person should have a single private instance variable of type String that represents the name of the Person. Write a single consructor that takes a String parameter used to initialize the instance variable AND an int parameter representing the number of legs the person has. Use the super() syntax to call the contructor of the super class Animal. Override the equals method (inherited from Animal that inherited it from Object) so that it returns false if the reference passed in is null, or is not the same type as this, or doesn't have the same number of legs as this, or does not have the same name as this, otherwise true. Override the toString method inherited from Animal that inherited it from Object so that it returns "I am an Animal object with N legs and a Person object whose name is XXX",

3) Write a class called Student that extends Person. Student should have a single private instance variable of type double that represents the GPA of the Student. Write a single constructor that takes a double parameter used to initialize the instance variable AND a String representing the student's name AND an int representing the number of legs the student has. Use the super() syntax to call the constructor of the super class Person.Override the equals method (inherited from Person that inherited it from Animal that inherited it from Object) so that it returns false if the reference passed in is null, or is not the same type as this, or doesn't have the same number of legs as this, or does not have the same name as this, or does not have the same gpa as this, otherwise true. Override the toString method inherited from Person that inherited it from Animal that inherited in from Object so that it returns "I am an Animal object with N legs and a Person object whose name is XXX and a Student Object with a Z.Z gpa",



YOUR HELP IS APPRECIATED

Explanation / Answer

inheritance concepot helps in this...

Suppose we have declared and implemented a class A. In order to declare a class B that is derived from A, Java offers the extend keyword that is used as shown below:

Java supports only public inheritance and thus, all fields and methods of the superclass are inherited and can be used by the subclass. The only exception are the private members of the superclass that cannot be accessed directly from the subclass. Also, constructors are not members, thus they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. In order to call the constructor of the superclass Java provides the keyword super, as shown below:

A sample example to present the inheritance in Java is shown below:

Animal.java:

Bird.java:

Dog.java:

MainClass.java: