[go: up one dir, main page]

0% found this document useful (0 votes)
23 views6 pages

Inheritance exercises

The document outlines a series of Java programming exercises focused on object-oriented principles such as inheritance and method overriding. It includes tasks for creating classes like Animal, Vehicle, Shape, and Employee, along with specific methods and attributes to implement. Each exercise encourages additional hands-on practice by suggesting enhancements to the initial class designs.

Uploaded by

davidalieze2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

Inheritance exercises

The document outlines a series of Java programming exercises focused on object-oriented principles such as inheritance and method overriding. It includes tasks for creating classes like Animal, Vehicle, Shape, and Employee, along with specific methods and attributes to implement. Each exercise encourages additional hands-on practice by suggesting enhancements to the initial class designs.

Uploaded by

davidalieze2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

EXERCISE

CSC221/COS202
You can use final to prevent a class from being inherited
Exercise 1
 Write a Java program to create a class called Animal with a method
called makeSound(). Create a subclass called Cat that overrides
the makeSound() method to bark. Write a main class and call the
makeSound method for both the animal class and the cat class.
 For more hands on, do the following to the classes created above:
• Write a Java program where the "Cat" subclass adds a new method
called scratch() that prints a message when called.
• Write a Java program where the "Animal" class includes an attribute
for habitat, and subclasses define specific habitats.
• Write a Java program where the "Animal" class has a life-
span attribute, and subclasses override it based on species.
Exercise 2
 Write a Java program to create a class called Vehicle with a
method called drive(). Create a subclass called Car that over-
rides the drive() method to print "Repairing a car".
 For more hands on, do the following to the classes created
above:
 Write a Java program where the "Car" subclass in-
cludes a method to check fuel level and alert when
low.
 Write a Java program where the "Vehicle" class has a
method to check engine status, and subclasses over-
ride it.
 Write a Java program where the "Car" subclass adds a
feature to calculate trip cost based on fuel consump-
tion.
 Write a Java program where the "Vehicle" class in-
Exercise 3
 Write a Java program to create a class called Shape
with a method called getArea(). Create a subclass
called Rectangle that overrides the getArea() method
to calculate the area of a rectangle.
 For more hands on, do the following to the classes created
above:
 Write a Java program where the "Rectangle" subclass
checks if the shape is a square.
 Write a Java program where the "Shape" class has an
attribute for color, and subclasses modify the color
dynamically.
 Write a Java program where the "Rectangle" subclass
adds a method to resize the shape.
Exercise 4
 Write a Java program to create a class called Employee with meth-
ods called work() and getSalary(). Create a subclass called
HRManager that overrides the work() method and adds a new
method called addEmployee().
 For more hands on, do the following to the classes created above:
 Write a Java program where the "HRManager" subclass includes a
method to schedule interviews for candidates.
 Write a Java program where the "Employee" class tracks overtime
hours and subclasses calculate additional pay.
 Write a Java program where the "HRManager" subclass allows em-
ployees to request salary increments.
 Write a Java program where the "Employee" class includes a
method to display employee hierarchy within the company.
THANK
YOU

You might also like