Archive for the ‘4) Inheritance’ Category
Inheritance
May 16, 2013
Leave a comment
4. What is Inheritance (Is-a Relationship)?
- The mechanism of deriving a new class from existing class is called inheritance.
- The inheritance allows subclasses to inherit all properties (variables and methods) of their parent class.
- When a “Is-A” relationship exists between two classes we use Inheritance.
- Reusability is achieved by INHERITANCE
- PHP classes can be reused by extending a class. Extending an existing class is nothing but reusing properties of the existing classes.
- The class whose properties are extended is known as super or base or parent class.
- The class which extends the properties of super class is known as sub or derived or child class.
- The keyword extends is used by the sub class to inherit the features of super class.
4.1. Is-a Relationship
- When one object is a specialized version of another object, there is an “is a” relationship between them. For example, a grasshopper is an insect. Here are a few other examples of the “is a” relationship:
- A poodle is a dog.
- A car is a vehicle.
- A flower is a plant.
- A rectangle is a shape.
- A football player is an athlete.
- When an “is a” relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special.
- In object-oriented programming, inheritance is used to create an “is a” relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.
4.2. Basic Types of inheritance:
- Single inheritance
- Multilevel inheritance
- Multiple inheritance
- Hierarchical inheritance
- Single inheritance
Categories: 4) Inheritance