OOP - Abstraction, Inheritance, Encapsulation, Polymorphism

Posted under » Methodology on 01 November 2012

This ability to hide from view the inner workings of a class is one of the four most important characteristics of OOP. APIE or :

Abstraction: Focus on the essentials. Show only the required things to the outside world while hiding the details. Human beings can talk, walk, hear, and eat but the details (legs) are hidden. Remember DRY : Dont Repeat Yourself.
Polymorphism: This means giving the same name to methods and properties that play similar roles in different classes. Polymorphism extends encapsulation by hiding the details of how individual methods work by using a common name.
Inheritance: New classes can be derived from existing ones, automatically inheriting all the methods and properties of the parent or superclass. The new class or subclass can not only add new properties and methods of its own, it can override those of its parent (superclass). Another DRY principle.
Encapsulation: This hides the details from the end user and prevents direct access to key values. Blackboxing.

Its not that we want to hide our own code, but its about reducing dependencies.

Eg. of inheritance.

<¿php
    // parent class
class Human {
    // public property name
        public $name;
        
    public function walk() {
        echo $this->name. " is walking...
"; } public function see() { echo $this->name. " is seeing...
"; } } // child class class Male extends Human { // No code in child } // child class class Female extends Human { // No code in child } $male = new Male(); $male->name = "Adam"; $female = new Female(); $female->name = "Eve"; // calling Human class methods $female->walk(); $male->see(); ?>

 

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data