Class is a wrapper that bundles attributes and methods of an object.
we can use UML class diagrams to understand and demonstrate a class of an object.
2. Encapsulation
Encapsulation is a fundamental concept in oop that involves bundling attributes and methods to an single unit called an object (this bundle is called a class ) and controll access to object’s components .
First we bundle the attributes and methods in a class and then we create an instance of a class .
To controll access we use three access modifiers .
2.1 Access Modifiers
Private
can be accessed only within the class
Helpful for helper methods that should not be exposed .
we use ” – ” to state the private modifier in UML Class diagrams .
When programming we use “private” keyword .
Public
can be accessed from outside the class
Used for attributes and methods that are meant to be a part of the class’s public interface.
we use ” + ” to state the private modifier in UML Class diagrams .
When programming we use “public” keyword .
Protected
can be accessed within the class and from child classes .
Used for attributes and methods that are meant to be a part of the class’s public interface .
we use ” # ” to state the private modifier in UML Class diagrams .
When programming we use “protected” keyword .
2.2 Why Use Access Modifiers ?
Data Hiding: You can prevent unwanted or harmful modifications to an object’s internal state by limiting access to specific sections of a class. This guarantees the validity and reliability of the object’s data.
Enhanced Security: By blocking unwanted access or change, access modifiers help in protecting the data inside an object.
Enhanced Security: By blocking unwanted access or change, access modifiers help in protecting the data inside an object.
Control Over Access: They offer control over the methods and data’s accessibility and modification. This makes it possible to manage the class more effectively and understand how it should be used.
Code Maintainability: It is easier to maintain and modify the code without affecting the sections that depend on the public interface of the class when it is apparent which parts are designed to be used privately and which are meant to be used publicly.
Flexibility: The class can modify its internal implementation without impacting the external code. This facilitates code optimization and upgrades.
2.3 Lets go through simple example !
ShoppingListItem and ShoppingList classes has a aggregation realationship between classes which means ShoppingListItem is a part ShoppingList class.
ShoppingList and the ShoppingListCommandPrompt has a composition realationship between the classes which means objects of ShoppingList class live and die with ShoppingListCommandPrompt class.
instances of class ShoppingList and ShoppingListCommandPrompt are created as attributes in MainScript.
The variables $itemID and $itemName are specified as private in the ShoppingListItem class. This indicates that the only ways for accessing and changing them are through the public methods the class provides..
The private properties $itemID and $itemName can be accessed and modified by the public getter and setter methods provided by the ShoppingListItem class.
The implementation of instantiating relevant objects using constructor .
The ShoppingList class provides the addItem(), removeItem(), getItems(), and getItemsAsString() methods for working with the private $items array. By controlling the addition, removal, and retrieval of items from the list, these approaches avoid having outsiders directly manipulate the $items array.
3. Abstraction
Abstraction is a concept that involves showing only the essential and relevant functions of a system, while hiding the complex implementation details. This frees users from having to understand the complex insides of an object in order to interact comfortably.
All shapes are required to follow by an agreement defined by the Shape interface.It abstract the idea of a shape by defining two methods calculateArea and calculatePerimeter that any shape must execute.
3.2 Implementing the Shape Interface
3.2.1 Circle Class
The Shape interface is implemented by the Circle class. Based on the circle’s radius, it provides implementations of the calculateArea and calculatePerimeter functions.
3.2.2 Rectangle Class
The Shape interface is implemented by the Rectangle class. Based on the rectangle’s height and width, it provides implementations of the calculateArea and calculatePerimeter functions.
3.2.3 Square Class
The Shape interface is implemented by the Square class. Based on the Square’s length, it provides implementations of the calculateArea and calculatePerimeter functions.
3.3 User Interaction
Through the Shape interface, the CalCommandPrompt class interacts with shapes, enabling users to calculate areas and perimeters without having to understand the details of how these calculations are carried out for various shapes.
4. Inheritance
A key concept in object-oriented programming (OOP) is inheritance, which lets a class adopt on attributes and methods from another class. A subclass is a class that inherits from the the base class. A natural hierarchy between classes is established by inheritance, which also encourages code reuse.
Superclass: The class from which another class inherits its methods and properties.
Subclass: The class that derives from another class is called a subclass.
The Shape class is an abstract class, meaning it cannot be instantiated directly.
The Rectangle class extends the Shape class, inheriting its abstract methods.
The Circle class also extends the Shape class, inheriting its abstract methods.
4.1 Benefits of inheritance
Code redundancy : Redundancy can be minimized by defining common methods and properties in a base class (like Shape) that are then used by derived classes (like Circle, Rectangle, Square).
Maintainability: Code maintenance becomes easier by the automated set of changes made to derived classes from the base class.
5. Polymorphism
This concept allows objects from various classes to be handled similarly to objects of the same base class. It offers a method for executing a single activity in several ways. Method overloading (compile-time polymorphism) and method overriding (runtime polymorphism) are two ways to accomplish polymorphism.
Superclass: The class from which another class inherits its methods and properties.
Subclass: The class that derives from another class is called a subclass.
5.1 Types of polymorphism
Runtime Polymorphism (dynamic)
Obtained through method overriding.
The actual type of the object determines at runtime which method should be called.
Compile-Time Polymorphism (static)
Obtained through method overloading.
The method signature is used at compile time to identify which method should be invoked..
A collection of Shape objects can be held by the ShapeCollection class, which can then polymorphically handle them.
Then, by using a common interface (Shape), polymorphism makes it possible to add and modify many shapes.
When you call the getName() method on an object of type Shape, the overridden method in the actual class of the object ( Circle, Rectangle, or Square) is executed.
This shows polymorphism, in which an object of a given type might behave differently in response to the same method call (getName()).
5.2 Key points of Overriding
Same Method Signature: The getName() method in each subclass has the same signature as set in the Shape abstract class.
Different Implementations: Each subclass provides a unique implementation of the getName() method, returning the name of the shape it represents.