INHERITANCE AND POLYMORPHISM.docx
Document Details
Uploaded by IntricateEcoArt8118
Universidad de Dagupan
Tags
Full Transcript
**INHERITANCE AND POLYMORPHISM** **WHAT IS INHERITANCE?** Inheritance is one of the four pillars of Object-Oriented Programming (OOPs). Inheritance is the phenomenon by which a child class can inherit all the properties and characteristics of the parent class. Inheritance is one in which a new cl...
**INHERITANCE AND POLYMORPHISM** **WHAT IS INHERITANCE?** Inheritance is one of the four pillars of Object-Oriented Programming (OOPs). Inheritance is the phenomenon by which a child class can inherit all the properties and characteristics of the parent class. Inheritance is one in which a new class is created that inherits the properties of the already exist class. It supports the concept of code reusability and reduces the length of the code in object-oriented programming. **SYNTAX:** **TYPES OF INHERITANCE** Inheritance in object-oriented programming (OOP) can take several forms, depending on how classes are related to each other. Here\'s an overview of the main types of inheritance: - Single Inheritance - Multiple Inheritance - Multilevel Inheritance - Hierarchical Inheritance - Hybrid inheritance **DERIVED** It specifies the name of the derived or the child class. The child class, also known as a subclass, inherits its salient features or characteristics from its parent class. There can be one or over one derived class that inherits from the same base class depending on what type of inheritance is performed. There are three visibility modes supported in PHP: Public, Private, and Protected, which specify how the derived class will inherit what. **BASE** It specifies the name of the base or the parent class from which the child class is inheriting its properties. A base class is also known as a parent class because all the classes that are termed as derived classes or child classes inherit their properties from the base class. There can be one or more base classes in a program, depending upon the type of inheritance. **SINGLE INHERITANCE** Single inheritance is the most basic and simple inheritance type. As the name suggests, in a single inheritance, there is only one base class and one sub or derived class. It directly inherits the subclass from the base class. **EXAMPLE 1:** **SYNTAX:** ![](media/image2.png) **OUTPUT:** **EXAMPLE 2:** **SYNTAX:** ![](media/image4.png) **MULTILEVEL INHERITANCE** Multilevel inheritance can also be explained by a family tree. There is one base class. This base class can be inherited by. These subclasses (not every subclass necessarily) act as base class and are further inherited by other subclasses. This is just like a family having descendants over generations. **EXAMPLE 1:** **SYNTAX:** **OUTPUT:** ![](media/image6.png) **EXAMPLE 2:** **SYNTAX:** **HIERARCHICAL INHERITANCE** Hierarchical inheritance shows a tree-like structure. Many derived classes are directly inherited from a base class. In this type of inheritance, more than one derived class shares the same parent class. ![](media/image8.png) **EXAMPLE 1:** **SYNTAX:** **OUTPUT:** ![](media/image10.png) **EXAMPLE 2:** **SYNTAX:** **OVERRIDING:** **METHOD** It can be observed when a derived class and base class both contain a function having the same name. Both the functions should have the same number of arguments. The derived class inherits the member functions and data members from its base class. So to override a certain functionality, you perform function overriding. **EXAMPLE 1:** **THIS IS A DERIVED CLASS:** ![](media/image12.png) **EXAMPLE 2:** **THE FINAL KEYWORD** In PHP, the final keyword is used to prevent class inheritance or method overriding. You can apply final to a class or a method, depending on what you want to restrict. **FINAL CLASSES** The final keyword with a class, serves the purpose of preventing inheritance. If a class is declared final, using the keyword \"final\", then inheriting that class will cause the program to throw an error. This can be useful in a case when you don\'t want a class and its members to be accessed anywhere else in the program. **SYNTAX:** ![](media/image14.png) **OUTPUT:** **FINAL METHODS** The final keyword when used with a method, serves the purpose of preventing method overriding. If a method has been declared final, using the \"final\" keyword, then another function with the same name and same parameters cannot be defined in a derived class. Calling such a function will cause the program to throw an error. **SYNTAX 1:** ![](media/image16.png) **SYNTAX 1 OUTPUT:** **SYNTAX 2:** ![](media/image18.png) **SYNTAX 2 OUTPUT:** **MULTIPLE INHERITANCE** **in PHP Using Traits or Interfaces** **Multiple inheritance** is a type of inheritance in which one class can inherit the properties from more than one parent class. In this type of inheritance, there is one derived class, that is derived from multiple base classes. This is one of the most important and useful concepts provided in the object-oriented paradigm and serves the purpose wherein you want one class to inherit different types of properties (which are in different classes). ![](media/image20.png) **TRAITS** **MULTIPLE INHERITANCE (VIA TRAITS)** Traits are used to reduce complexity of a program by writing consistent functionalities that can be used by other classes in the program. The traits can be used to implement the concept of multiple inheritance in PHP. The traits can be used to implement the concept of multiple inheritance in PHP. **SYNTAX 1:** **SYNTAX 2:** ![](media/image22.png) **SYNTAX 2 OUTPUT:** I AM A BIRD, A PET, AND A PARROT. **INTERFACES** Interfaces are used when there are objects that share the same functionality, but you don\'t want the objects to know about each other. In other words, an interface provides contracts for the objects, without knowing anything else about one another. The following program illustrates the usage of interfaces to implement multiple inheritance: **SYNTAX:** **EXAMPLE:** **SYNTAX:** ![](media/image24.png) **OUTPUT:** I AM A BIRD, A PET, AND A PARROT. **IMPORTANCE OF INHERITANCE IN PHP** Inheritance in itself is one of the most important concepts introduced in object-oriented programming. Following are some of the key points highlighting the importance of inheritance in PHP: - **Code reusability:** Inheritance strongly supports code reusability. - **Data hiding:** Data hiding is one of the most important aspects of inheritance. - **Extensibility:** Inheritance provides a way to write extensible programs. - **Overriding:** The concept of method overriding can be easily achieved in PHP. - **Reduced complexity:** The complexity of code is also reduced as common properties can be simply inherited. **WHAT IS POLYMORPHISM** **Polymorphism** is a Greek word that literally means many forms. In object-oriented programming, polymorphism is closely related to [inheritance]. **Polymorphism** is a core principle of object-oriented programming (OOP) where objects of different classes can be treated as objects of a common superclass. It allows methods to perform different actions based on the object they are called upon, enhancing code flexibility and reusability. **PHP POLYMORPHISM:** **USING AN ABSTRACT CLASS** In PHP, polymorphism can be achieved using abstract classes. An abstract class serves as a base class that cannot be instantiated directly and can contain abstract methods that must be implemented by derived (child) classes. First, define an abstract class named **Person** that has an abstract method called **greet()**. Second, define the **English**, **German**, and **French** classes that extend the **Person** class. The **greet()** method of each class returns a different greeting message. **SYNTAX:** ![](media/image26.png) Third, define a function called **greeting()** that accepts an array of **Person** objects and calls the **greet()** method of each object: Fourth, define an array of objects of the **English**, **German**, and **French** classes and pass it to the **greeting()** function: ![](media/image28.png) Due to the specific implementation of the **greet()** method in each class, these objects return different greeting messages. **OUTPUT:** The following class diagram illustrates the relationships between the classes: ![](media/image30.png) Later, if you want to create a new class that extends the **Person** class, you don\'t need to change the **greeting()** function. For example, you can define a new class called **American** that extends the **Person** class: And use the object of the American class in the **greeting()** function as follows: ![](media/image32.png) **OUTPUT:** **POLYMORPHISM:** **USING AN INTERFACE** Polymorphism in PHP can also be achieved using interfaces. An interface in PHP defines a contract that classes must follow, specifying a set of methods that must be implemented by any class that implements the interface. First, define an interface called **Greeting** that has the **greet()** method. The **greet()** method returns a greeting message: ![](media/image34.png) Second, define the **English**, **German**, and **French** classes that implement the **Greeting** interface: Third, define the **greeting()** function that accepts an array of objects that implements the **Greeting** interface: ![](media/image36.png) Finally, define an array of the **Greeting** objects and pass it to the **greeting()** function: **OUTPUT:** ![](media/image38.png) The following class diagram shows the relationships between the interface and classes: