LAB 5 - Inheritance
LAB 5 - Inheritance
LAB 5 - Inheritance
Task# 1:
Create two classes, "Animal" and "Dog", where "Dog" extends "Animal".
The "Animal" class should have an instance variable called "name" and a method called
"speak()" that prints "An animal makes a sound" to the console.
The "Dog" class should have an instance variable called "breed" and a method called
"speak()" that prints "A dog barks" to the console.
Write a test program, create an instance of "Dog" called "myDog" and set its name to "Buddy"
and breed to "Golden Retriever". Call the "speak()" method on "myDog".
Design a class named Person and its two subclasses named Student and Employee. Make
Faculty and Staff subclasses of Employee.
Draw the UML diagram for the classes and implement them. Write a test program that
creates a Person, Student, Employee, Faculty, and Staff, and invokes their toString()
methods.
The following UML class diagram illustrates an inheritance relationship, wherein the
classes Circle and Rectangle have been extended from the class GeometricObject.
▪ Two double data fields named width and height that specify the width and height of
the rectangle. The default values are 1.0 for both width and height.
▪ A no-arg constructor that creates a default rectangle.
▪ A constructor that creates a rectangle with the specified width and height.
▪ A method named getArea() that returns the area of this rectangle.
▪ A method named getPerimeter() that returns the perimeter.
▪ A method named toString() that returns a string description for the rectangle.
Write a test program that prompts the user to enter width and height of the rectangle, a
color, and a Boolean value to indicate whether the rectangle is filled. The program should
create a Rectangle object and set the color and filled properties using the input. The
program should display the area, perimeter, color, and true or false to indicate whether it is
Implement the Shape hierarchy shown in below figure. Each TwoDimensionalShape should
contain method getArea to calculate the area of the two-dimensional shape. Each
ThreeDimensionalShape should have methods getArea and getVolume to calculate the
surface area and volume, respectively, of the three-dimensional shape.