You are on page 1of 3

Problem A

Objectives:
To define a subclass from a superclass through inheritance (§11.2).
To invoke the superclass’s constructors and methods using the super keyword (§11.3).
To override instance methods in the subclass (§11.4).

Task: Basic Inheritance


1. Create a class named `Animal` with the following attributes:
- `name` (String)
- `sound` (String)

2. Implement a constructor in the `Animal` class to initialize the `name` and `sound`
attributes.

3. Create a subclass named `Bird` that extends the `Animal` class.

4. In the `Bird` class, add an additional attribute:


- `wingSpan` (double)

5. Implement a constructor in the `Bird` class that calls the superclass's constructor
using the `super` keyword and initializes the `wingSpan` attribute.

Method Overriding:

1. In the `Animal` class, create a method named `makeSound()` that prints the sound
of the animal.

2. Override the `makeSound()` method in the `Bird` class to make it specific for birds.
For example, print "Chirp!" for bird sounds.
Problem B
Objectives:
To distinguish differences between overriding and overloading (§11.5).
To explore the toString() method in the Object class (§11.6).

Task 1: Overriding vs Overloading:

1. Create a class named `MathOperations`.

2. Implement two methods named `add`:


- One that takes two integers and returns their sum.
- Another that takes two doubles and returns their sum.

3. Explain the concept of method overloading and how it allows methods with the same
name but different parameter types in the same class.

4. Create a subclass named `AdvancedMathOperations` that extends `MathOperations`.

5. Override the `add` method in the `AdvancedMathOperations` class to provide a different


implementation.

6. Explain the concept of method overriding and how it allows a subclass to provide a
specific implementation for a method defined in its superclass.

Task 2: toString() Method:

1. In the `MathOperations` class, override the `toString()` method provided by the `Object`
class.

2. Implement the `toString()` method to return a string representation of the object, including
information about the class and its attributes.

3. Create objects of both the `MathOperations` and `AdvancedMathOperations` classes.


4. Demonstrate the usage of the `toString()` method by printing the string representation of
the objects.

You might also like