0% found this document useful (0 votes)
5 views3 pages

PG 6

The document outlines the four main principles of Object-Oriented Programming (OOP): encapsulation, inheritance, polymorphism, and abstraction. It explains encapsulation in PHP, how to implement it in a user profile class, and compares inheritance and polymorphism. Additionally, it evaluates the pros and cons of abstract classes versus interfaces and provides an example of a class hierarchy demonstrating inheritance and polymorphism.

Uploaded by

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

PG 6

The document outlines the four main principles of Object-Oriented Programming (OOP): encapsulation, inheritance, polymorphism, and abstraction. It explains encapsulation in PHP, how to implement it in a user profile class, and compares inheritance and polymorphism. Additionally, it evaluates the pros and cons of abstract classes versus interfaces and provides an example of a class hierarchy demonstrating inheritance and polymorphism.

Uploaded by

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

Question: What are the four main principles of Object-Oriented Programming (OOP)?

Suggested Solution: The four main principles of OOP are encapsulation, inheritance,
polymorphism, and abstraction.

2. Understanding

Question: Can you explain how encapsulation works in PHP and why it is important?
Suggested Solution: Encapsulation in PHP involves bundling data (properties) and methods
within a class while restricting direct access to some of its components. It is important because it
helps protect the integrity of the data by preventing unintended interference and modification,
promoting better code organization.

3. Applying

Question: How would you implement encapsulation in a PHP class that represents a user
profile?
Suggested Solution: You can create a UserProfile class with private properties for user data
(e.g., username, password) and public methods for setting and getting those properties. This way,
you control how the properties can be accessed or modified.

php
Copy code
class UserProfile {
private $username;
private $password;

public function setUsername($username) {


$this->username = $username;
}

public function getUsername() {


return $this->username;
}

public function setPassword($password) {


// Here, you could add password validation
$this->password = $password;
}

public function getPassword() {


return $this->password;
}
}

4. Analyzing
Question: Compare and contrast inheritance and polymorphism in PHP.
Suggested Solution: Inheritance allows a class to inherit properties and methods from another
class, enabling code reuse and establishing a hierarchical relationship. Polymorphism, on the
other hand, allows methods to perform different functions based on the object that is calling
them. While inheritance is about "is-a" relationships, polymorphism allows for "one interface,
multiple implementations."

5. Evaluating

Question: Evaluate the advantages and disadvantages of using abstract classes versus interfaces
in PHP.
Suggested Solution:

 Advantages of Abstract Classes:


o Can provide default behavior.
o Can contain properties.
 Disadvantages of Abstract Classes:
o A class can only inherit from one abstract class.
 Advantages of Interfaces:
o Allows for multiple implementations.
o Provides a contract that all implementing classes must follow.
 Disadvantages of Interfaces:
o Cannot provide any default behavior or state.

6. Creating

Question: Design a simple class hierarchy in PHP that illustrates the use of inheritance, and
create an instance that utilizes polymorphism.
Suggested Solution: You could create a Shape class with an area() method, and subclasses like
Circle and Square that implement the area() method differently. For example:

php
Copy code
abstract class Shape {
abstract public function area();
}

class Circle extends Shape {


private $radius;

public function __construct($radius) {


$this->radius = $radius;
}

public function area() {


return pi() * ($this->radius ** 2);
}
}

class Square extends Shape {


private $side;

public function __construct($side) {


$this->side = $side;
}

public function area() {


return $this->side ** 2;
}
}

// Polymorphism in action
$shapes = [new Circle(5), new Square(4)];

foreach ($shapes as $shape) {


echo $shape->area(); // Outputs area of each shape
}

You might also like