Object-Oriented Programming (OOP) Assignment
This assignment is designed to demonstrate all the core concepts and pillars of Object-
Oriented Programming (OOP), including:
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
- Association
- Aggregation
- Composition
- Constructor / Destructor / Super Constructor
- Shallow Copy vs Deep Copy
- Interfaces
Assignment Statement
Design an OOP-based system using an Animal class as the base. The system must cover the
following concepts:
1. Inheritance ("is-a" relationship)
Create a base class `Animal`. Derived classes like `Dog`, `Cat`, and `Bird` should extend
the `Animal` class, showcasing the "is-a" relationship.
2. Polymorphism
Demonstrate both compile-time (method overloading) and runtime polymorphism
(method overriding) using `speak()` methods in derived classes.
3. Abstraction
Use abstract classes and/or interfaces like `Flyable` and `Swimmable` with abstract
methods `fly()` or `swim()`.
4. Encapsulation
Encapsulate all class properties like `name`, `age`, `species` using private access
modifiers and provide public getters/setters.
5. Association
Show a simple association between a `Veterinarian` class and `Animal` class (e.g., vet
checks an animal).
6. Aggregation ("has-a" relationship, lifetime independent)
A `Zoo` class should contain a list of `Animal` objects. The animals can exist outside the
zoo.
7. Composition ("must-have" relationship, lifetime dependent)
Each `Animal` must have a `Heart` object. When the `Animal` object is destroyed, its
`Heart` should also be destroyed.
8. Constructor, Destructor, Super Constructor
Implement default and parameterized constructors in each class. Use the `super()`
keyword to call the parent class constructor. Use destructors or finalize (language-
dependent) to display messages on object destruction.
9. Shallow Copy vs Deep Copy
Demonstrate the difference by cloning an `Animal` object and modifying its composed
`Heart` object to observe behavior.
10. Interface
Create an `AnimalActions` interface with methods like `eat()`, `sleep()`, and implement
them in all subclasses.
Instructions:
Implement the assignment in **C++** or **Java** (choose one). Use appropriate OOP syntax
and language-specific features.
Example Structure (Java style):
abstract class Animal implements AnimalActions {
private String name;
private int age;
private Heart heart; // Composition
public Animal(String name, int age) {
this.name = name;
this.age = age;
this.heart = new Heart();
}
public abstract void speak();
// Getters and Setters
}
class Dog extends Animal {
public Dog(String name, int age) {
super(name, age);
}
@Override
public void speak() {
System.out.println("Woof Woof");
}
public void guard() {}
}
interface AnimalActions {
void eat();
void sleep();
}
class Zoo {
private List<Animal> animals; // Aggregation
public void addAnimal(Animal a) {
animals.add(a);
}
}