Assignments On Class 22
Object-Oriented Programming (OOP) - Part 4
Here are some assignments based on the topics covered in Class 22: Object-Oriented Programming (OOP) - Part 4, focusing on inheritance and interfaces in PHP. These assignments will help reinforce the concepts learned during the lecture.
Assignment 1: Creating a Class Hierarchy
Objective: To practice implementing inheritance in PHP.
Task:
1. Create a parent class called Person that has properties for name, age, and methods to get the person's details.
2. Create two child classes, Student and Teacher, that inherit from the Person class.
o The Student class should have an additional property for grade and a method to display the student’s details.
o The Teacher class should have an additional property for subject and a method to display the teacher’s details.
3. Instantiate objects of Student and Teacher and display their details.
Expected Output:
- A clear display of details for both the student and teacher, showing inherited properties and methods.
Assignment 2: Implementing Interfaces
Objective: To understand how to define and implement interfaces.
Task:
1. Define an interface named Playable with methods play() and stop().
2. Create two classes, Music and Video, that implement the Playable interface.
o The Music class should include properties like title and artist, along with implementations for play() and stop() methods.
o The Video class should include properties like title and duration, along with implementations for play() and stop() methods.
3. Instantiate objects of Music and Video and demonstrate the use of the play() and stop() methods.
Expected Output:
- Messages indicating that the music or video is playing or has stopped, demonstrating the interface functionality.
Assignment 3: Combining Inheritance and Interfaces
Objective: To explore the combination of inheritance and interfaces in PHP.
Task:
1. Define an interface named Shape with a method area().
2. Create an abstract class Polygon that implements the Shape interface. Include a method to get the number of sides.
3. Create two concrete classes, Triangle and Rectangle, that extend the Polygon class and implement the area() method.
o The Triangle class should have properties for base and height.
o The Rectangle class should have properties for width and height.
4. Instantiate objects of both Triangle and Rectangle, calculate, and display their areas and the number of sides.
Expected Output:
- Output displaying the area of both shapes and the number of sides, showing how inheritance and interfaces work together.
Assignment 4: Real-World Scenario
Objective: To apply knowledge of inheritance and interfaces to a real-world scenario.
Task:
1. Consider a scenario in a library system where you have different types of library items: books, magazines, and DVDs.
2. Create an interface named LibraryItem with methods checkOut() and returnItem().
3. Create a parent class called Library that implements the LibraryItem interface, which includes properties such as title and availability.
4. Create three classes, Book, Magazine, and DVD, that extend the Library class and provide specific implementations for checkOut() and returnItem() methods.
5. Instantiate objects of each class and demonstrate checking out and returning items.
Expected Output:
- Outputs indicating the status of each library item when checked out or returned, illustrating the use of inheritance and interfaces in a practical application.
Assignment 5: Reflection
Objective: To reflect on what has been learned about inheritance and interfaces.
Task:
1. Write a short essay (300-500 words) discussing the importance of inheritance and interfaces in OOP.
2. Include examples from the assignments completed and how they can help in developing scalable and maintainable code.
Expected Output:
- A well-structured essay that reflects understanding of the concepts, complete with examples and personal insights.
These assignments aim to solidify the understanding of inheritance and interfaces in PHP while encouraging practical application and reflection on the concepts learned.
4o mini
You said:
write the solutions with explanations and output
ChatGPT said:
ChatGPT
Here are the solutions for each of the assignments on Object-Oriented Programming (OOP) topics: inheritance and interfaces in PHP. Each solution includes explanations and expected outputs.
Assignment 1: Creating a Class Hierarchy
Solution:
php
Copy code
<?php
class Person {
public $name;
public $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
public function getDetails() {
return "Name: " . $this->name . ", Age: " . $this->age;
}
}
class Student extends Person {
public $grade;
public function __construct($name, $age, $grade) {
parent::__construct($name, $age); // Call parent constructor
$this->grade = $grade;
}
public function getDetails() {
return parent::getDetails() . ", Grade: " . $this->grade;
}
}
class Teacher extends Person {
public $subject;
public function __construct($name, $age, $subject) {
parent::__construct($name, $age); // Call parent constructor
$this->subject = $subject;
}
public function getDetails() {
return parent::getDetails() . ", Subject: " . $this->subject;
}
}
// Instantiate objects
$student = new Student("Alice", 20, "A");
$teacher = new Teacher("Mr. Smith", 40, "Mathematics");
echo $student->getDetails() . "\n"; // Output: Name: Alice, Age: 20, Grade: A
echo $teacher->getDetails() . "\n"; // Output: Name: Mr. Smith, Age: 40, Subject: Mathematics
?>
Explanation:
- The Person class is defined with properties for name and age, along with a method to retrieve the details.
- The Student and Teacher classes extend the Person class, inheriting its properties and methods, while adding their own specific properties and methods.
- The getDetails() method in both subclasses calls the parent method to include inherited properties.
Expected Output:
yaml
Copy code
Name: Alice, Age: 20, Grade: A
Name: Mr. Smith, Age: 40, Subject: Mathematics
Assignment 2: Implementing Interfaces
Solution:
php
Copy code
<?php
interface Playable {
public function play();
public function stop();
}
class Music implements Playable {
private $title;
private $artist;
public function __construct($title, $artist) {
$this->title = $title;
$this->artist = $artist;
}
public function play() {
return "Playing music: " . $this->title . " by " . $this->artist;
}
public function stop() {
return "Stopped music: " . $this->title;
}
}
class Video implements Playable {
private $title;
private $duration;
public function __construct($title, $duration) {
$this->title = $title;
$this->duration = $duration;
}
public function play() {
return "Playing video: " . $this->title . " Duration: " . $this->duration . " minutes";
}
public function stop() {
return "Stopped video: " . $this->title;
}
}
// Instantiate objects
$music = new Music("Song Title", "Artist Name");
$video = new Video("Video Title", 120);
echo $music->play() . "\n"; // Output: Playing music: Song Title by Artist Name
echo $music->stop() . "\n"; // Output: Stopped music: Song Title
echo $video->play() . "\n"; // Output: Playing video: Video Title Duration: 120 minutes
echo $video->stop() . "\n"; // Output: Stopped video: Video Title
?>
Explanation:
- The Playable interface defines the methods play() and stop().
- The Music and Video classes implement this interface and provide their own versions of the methods.
- The constructors of both classes initialize properties such as title and artist for music, and title and duration for video.
Expected Output:
less
Copy code
Playing music: Song Title by Artist Name
Stopped music: Song Title
Playing video: Video Title Duration: 120 minutes
Stopped video: Video Title
Assignment 3: Combining Inheritance and Interfaces
Solution:
php
Copy code
<?php
interface Shape {
public function area();
}
abstract class Polygon implements Shape {
protected $sides;
public function getNumberOfSides() {
return $this->sides;
}
}
class Triangle extends Polygon {
private $base;
private $height;
public function __construct($base, $height) {
$this->base = $base;
$this->height = $height;
$this->sides = 3; // Triangle has 3 sides
}
public function area() {
return 0.5 * $this->base * $this->height;
}
}
class Rectangle extends Polygon {
private $width;
private $height;
public function __construct($width, $height) {
$this->width = $width;
$this->height = $height;
$this->sides = 4; // Rectangle has 4 sides
}
public function area() {
return $this->width * $this->height;
}
}
// Instantiate objects
$triangle = new Triangle(10, 5);
$rectangle = new Rectangle(4, 6);
echo "Triangle area: " . $triangle->area() . "\n"; // Output: Triangle area: 25
echo "Triangle sides: " . $triangle->getNumberOfSides() . "\n"; // Output: Triangle sides: 3
echo "Rectangle area: " . $rectangle->area() . "\n"; // Output: Rectangle area: 24
echo "Rectangle sides: " . $rectangle->getNumberOfSides() . "\n"; // Output: Rectangle sides: 4
?>
Explanation:
- The Shape interface defines the method area().
- The Polygon abstract class implements the Shape interface and includes a method to get the number of sides.
- The Triangle and Rectangle classes extend the Polygon class, providing specific implementations for the area() method.
Expected Output:
mathematica
Copy code
Triangle area: 25
Triangle sides: 3
Rectangle area: 24
Rectangle sides: 4
Assignment 4: Real-World Scenario
Solution:
php
Copy code
<?php
interface LibraryItem {
public function checkOut();
public function returnItem();
}
class Library implements LibraryItem {
protected $title;
protected $availability;
public function __construct($title) {
$this->title = $title;
$this->availability = true; // Available by default
}
public function checkOut() {
if ($this->availability) {
$this->availability = false;
return $this->title . " has been checked out.";
} else {
return $this->title . " is currently unavailable.";
}
}
public function returnItem() {
$this->availability = true;
return $this->title . " has been returned.";
}
}
class Book extends Library {
public function __construct($title) {
parent::__construct($title);
}
}
class Magazine extends Library {
public function __construct($title) {
parent::__construct($title);
}
}
class DVD extends Library {
public function __construct($title) {
parent::__construct($title);
}
}
// Instantiate objects
$book = new Book("The Great Gatsby");
$magazine = new Magazine("National Geographic");
$dvd = new DVD("Inception");
echo $book->checkOut() . "\n"; // Output: The Great Gatsby has been checked out.
echo $magazine->checkOut() . "\n"; // Output: National Geographic has been checked out.
echo $dvd->checkOut() . "\n"; // Output: Inception has been checked out.
echo $book->checkOut() . "\n"; // Output: The Great Gatsby is currently unavailable.
echo $book->returnItem() . "\n"; // Output: The Great Gatsby has been returned.
?>
Explanation:
- The LibraryItem interface defines checkOut() and returnItem() methods.
- The Library class implements this interface and manages the availability status of library items.
- The Book, Magazine, and DVD classes inherit from Library, reusing its functionality.
Expected Output:
csharp
Copy code
The Great Gatsby has been checked out.
National Geographic has been checked out.
Inception has been checked out.
The Great Gatsby is currently unavailable.
The Great Gatsby has been returned.
Assignment 5: Reflection
Solution:
Essay: Importance of Inheritance and Interfaces in OOP
Inheritance and interfaces are essential components of Object-Oriented Programming (OOP) that contribute significantly to software development. These concepts promote code reusability, flexibility, and maintainability, making it easier for developers to manage complex systems.
Inheritance allows classes to inherit properties and methods from other classes, reducing redundancy in code. For instance, in Assignment 1, we created a Person class, which was inherited by Student and Teacher classes. This structure enables us to define common behavior in a single place, making updates and changes more straightforward. If we need to add a new property to Person, we do it once, and all derived classes inherit that change.
Interfaces, on the other hand, define a contract that implementing classes must follow. This encourages a consistent approach to functionality across different classes. In Assignment 2, the Playable interface required both Music and Video classes to implement the play() and stop() methods. This guarantees that any class implementing Playable will provide these methods, leading to more predictable behavior in our application.
Combining inheritance and interfaces, as shown in Assignment 3, allows developers to create a robust architecture. Abstract classes can define default behavior, while interfaces ensure specific functionality. This dual approach fosters code organization and makes it easier to extend applications without breaking existing functionality.
In real-world applications, such as libraries or media management systems (Assignment 4), the use of inheritance and interfaces helps encapsulate behavior while providing the flexibility needed for diverse items. By modeling library items with a common interface, we can manage various types of media uniformly, simplifying the check-out and return processes.
In conclusion, inheritance and interfaces are vital tools in OOP, enabling developers to write cleaner, more efficient, and maintainable code. They promote best practices in software design, ensuring that systems are both flexible and robust, which is crucial for meeting the evolving needs of users and businesses.
These solutions illustrate fundamental concepts of OOP in PHP through practical examples. Each output confirms the successful implementation of inheritance and interfaces, demonstrating how these principles work in real-world applications.