Inheritance

It is a concept of accessing the features of one class from another class. If we inherit the class features into another class, we can access both class properties. We can extends the features of a class by using ‘extends’ keyword.

  • It supports the concept of hierarchical classification.
  • Inheritance has three types, single, multiple and multilevel Inheritance.
  • PHP supports only single inheritance, where only one class can be derived from single parent class.
  • We can simulate multiple inheritance by using interfaces.

Example 1

<?php  
class a  
    {  
        function fun1()  
        {  
            echo "protutorials";  
        }  
    }  
    class b extends a  
    {  
        function fun2()  
        {  
            echo "SSSIT";  
        }  
    }  
    $obj= new b();  
    $obj->fun1();  
?>  

Output:

protutorials

Example 2

<?php  
    class demo  
    {  
        public function display()  
        {  
            echo "example of inheritance  ";  
        }     
    }  
    class demo1 extends demo  
    {  
        public function view()  
        {  
            echo "in php";  
        }     
    }  
    $obj= new demo1();  
    $obj->display();  
    $obj->view();  
?>

Output:

example of inheritance in php

PHP functions

1. get_class: By using this, we can get the class name of an object.

Example 1

<?php  
    class cls1  
    {  
          
    }  
    $obj=new cls1();  
    echo get_class($obj);  
?>  

Output:

cls1

2. get_class_vars: It is used to get all variables of a class as Array elements.

Example 2

<?php  
    class cls1  
    {  
        var $x=100;  
        var $y=200;  
    }  
    print_r(get_class_vars("cls1"));  
?>  

Output:

Array([x] => 100[y]=>200)

3. get_class_methods: To get the all methods of a class as an array.

Example 3

<?php  
    class cls1  
    {  
        function fun1()  
        {  
        }  
        function fun2()  
        {  
        }  
    }  
    print_r(get_class_methods("cls1"));  
?>  

Output:

Array([0] => fun1[1] =>fun2 )

4. get_declare_classes: To get the all declare classes in current script along with predefined classes.

Example 4

<?php  
    class cls1  
    {  
      
    }  
    print_r(get_declared_classes());  
?>  

Output:

Some Helpful Functions in PHP to get the Information About Class and Object

5. get_object_vars: To get all variables of an object as an array.

Example 5

<?php  
    class cls1  
    {  
        var $x=100;  
        var $y=200;  
    }  
    $obj= new cls1();  
    print_r(get_object_vars($obj));  
?>  

Output:

Array([x] => 100[y]=>200)

6. class_exists: To check whether the specified class is existed or not.

Example 6

<?php  
    class cls1  
    {  
          
    }  
    echo class_exists("cls1");  
?>  

Output:

1

7. is_subclass_of: By using this function we can check whether the 1st class is subclass of 2nd class or not.

Example 7

<?php  
    class cls1  
    {  
          
    }  
    class cls2 extends cls1  
    {  
    }  
    echo is_subclass_of("cls2","cls1");  
?>  

Output:

1

8. method_exists: By using this function we can check whether the class method is existed or not.

Example 8

<?php  
    class cls1  
    {  
        function fun1()  
        {  
        }  
    }  
    echo method_exists("cls1","fun1");  
?>  

Output:

1

Abstract Class

An abstract class is a mix between an interface and a class. It can be define functionality as well as interface.

  • Classes extending an abstract class must implement all of the abstract methods defined in the abstract class.
  • An abstract class is declared the same way as classes with the addition of the ‘abstract’ keyword.

SYNTAX:

abstract class MyAbstract  
{  
    //Methods  
}  
//And is attached to a class using the extends keyword.  
class Myclass extends MyAbstract  
{  
    //class methods  
}  

Example 1

<?php  
abstract class a  
{  
abstract public function dis1();  
abstract public function dis2();  
}  
class b extends a  
{  
public function dis1()  
    {  
        echo "tutorialspro";  
    }  
    public function dis2()  
    {  
        echo "tech";     
    }  
}  
$obj = new b();  
$obj->dis1();  
$obj->dis2();  
?>  

Output:

tutorialsprotech

Example 2

<?php  
abstract class Animal  
{  
    public $name;  
    public $age;  
public function Describe()  
        {  
                return $this->name . ", " . $this->age . " years old";      
        }  
abstract public function Greet();  
    }  
class Dog extends Animal  
{  
public function Greet()  
        {  
                return "Woof!";      
        }  
      
        public function Describe()  
        {  
                return parent::Describe() . ", and I'm a dog!";      
        }  
}  
$animal = new Dog();  
$animal->name = "Bob";  
$animal->age = 7;  
echo $animal->Describe();  
echo $animal->Greet();  
?>  

Output:

Bob,7 years old,and I’m a dog!woof!

PHP With OOPS Concept

Object-oriented programming is a programming model organized around Object rather than the actions and data rather than logic.

Class:

A class is an entity that determines how an object will behave and what the object will contain. In other words, it is a blueprint or a set of instruction to build a specific type of object.

In PHP, declare a class using the class keyword, followed by the name of the class and a set of curly braces ({}).

This is the blueprint of the construction work that is class, and the houses and apartments made by this blueprint are the objects.

Syntax to Create Class in PHP

  1.   
<?php  
class MyClass  
    {  
        // Class properties and methods go here  
    }  
?>

Important note:

In PHP, to see the contents of the class, use var_dump(). The var_dump() function is used to display the structured information (type and value) about one or more variables.

Syntax:

  1. var_dump($obj);  

Object:

A class defines an individual instance of the data structure. We define a class once and then make many objects that belong to it. Objects are also known as an instance.

An object is something that can perform a set of related activities.

Syntax:

  

<?php  
class MyClass  
{  
        // Class properties and methods go here  
}  
$obj = new MyClass;  
var_dump($obj);  
?>

Example of class and object:

<?php  
class demo  
{  
        private $a= "hello tutorialspro";  
        public function display()  
        {  
        echo $this->a;  
        }  
}  
$obj = new demo();  
    $obj->display();  
?>  

Output:

hello tutorialspro

Example 2: Use of var_dump($obj);

  

<?php  
class demo  
{  
        private $a= "hello javatpoint";  
        public function display()  
        {  
        echo $this->a;  
        }  
}  
$obj = new demo();  
    $obj->display();  
    var_dump($obj);  
?>

Output:

hello tutorialspro

object(demo)[1]

private ‘a’ => string ‘hello tutorialspro’ (length=18)

Python Object Oriented Programming

In this tutorial, you’ll learn about Object-Oriented Programming (OOP) in Python and its fundamental concept with the help of examples.

Object Oriented Programming

Python is a multi-paradigm programming language. It supports different programming approaches.

One of the popular approaches to solve a programming problem is by creating objects. This is known as Object-Oriented Programming (OOP).

An object has two characteristics:

  • attributes
  • behavior

Let’s take an example:

A parrot is can be an object,as it has the following properties:

  • name, age, color as attributes
  • singing, dancing as behavior

The concept of OOP in Python focuses on creating reusable code. This concept is also known as DRY (Don’t Repeat Yourself).

In Python, the concept of OOP follows some basic principles:


Class

A class is a blueprint for the object.

We can think of class as a sketch of a parrot with labels. It contains all the details about the name, colors, size etc. Based on these descriptions, we can study about the parrot. Here, a parrot is an object.

The example for class of parrot can be :

class Parrot:
    pass

Here, we use the class keyword to define an empty class Parrot. From class, we construct instances. An instance is a specific object created from a particular class.


Object

An object (instance) is an instantiation of a class. When class is defined, only the description for the object is defined. Therefore, no memory or storage is allocated.

The example for object of parrot class can be:

obj = Parrot()

Here, obj is an object of class Parrot.

Suppose we have details of parrots. Now, we are going to show how to build the class and objects of parrots.

Example 1: Creating Class and Object in Python

class Parrot:

    # class attribute
    species = "bird"

    # instance attribute
    def __init__(self, name, age):
        self.name = name
        self.age = age

# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)

# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))

# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))

Output

Blu is a bird
Woo is also a bird
Blu is 10 years old
Woo is 15 years old

In the above program, we created a class with the name Parrot. Then, we define attributes. The attributes are a characteristic of an object.

These attributes are defined inside the __init__ method of the class. It is the initializer method that is first run as soon as the object is created.

Then, we create instances of the Parrot class. Here, blu and woo are references (value) to our new objects.

We can access the class attribute using __class__.species. Class attributes are the same for all instances of a class. Similarly, we access the instance attributes using blu.name and blu.age. However, instance attributes are different for every instance of a class.


Methods

Methods are functions defined inside the body of a class. They are used to define the behaviors of an object.

Example 2 : Creating Methods in Python

class Parrot:
    
    # instance attributes
    def __init__(self, name, age):
        self.name = name
        self.age = age
    
    # instance method
    def sing(self, song):
        return "{} sings {}".format(self.name, song)

    def dance(self):
        return "{} is now dancing".format(self.name)

# instantiate the object
blu = Parrot("Blu", 10)

# call our instance methods
print(blu.sing("'Happy'"))
print(blu.dance())

Output

Blu sings 'Happy'
Blu is now dancing

In the above program, we define two methods i.e sing() and dance(). These are called instance methods because they are called on an instance object i.e blu.


Inheritance

Inheritance is a way of creating a new class for using details of an existing class without modifying it. The newly formed class is a derived class (or child class). Similarly, the existing class is a base class (or parent class).

Example 3: Use of Inheritance in Python

# parent class
class Bird:
    
    def __init__(self):
        print("Bird is ready")

    def whoisThis(self):
        print("Bird")

    def swim(self):
        print("Swim faster")

# child class
class Penguin(Bird):

    def __init__(self):
        # call super() function
        super().__init__()
        print("Penguin is ready")

    def whoisThis(self):
        print("Penguin")

    def run(self):
        print("Run faster")

peggy = Penguin()
peggy.whoisThis()
peggy.swim()
peggy.run()

Output

Bird is ready
Penguin is ready
Penguin
Swim faster
Run faster

In the above program, we created two classes i.e. Bird (parent class) and Penguin (child class). The child class inherits the functions of parent class. We can see this from the swim() method.

Again, the child class modified the behavior of the parent class. We can see this from the whoisThis() method. Furthermore, we extend the functions of the parent class, by creating a new run() method.

Additionally, we use the super() function inside the __init__() method. This allows us to run the __init__() method of the parent class inside the child class.


Encapsulation

Using OOP in Python, we can restrict access to methods and variables. This prevents data from direct modification which is called encapsulation. In Python, we denote private attributes using underscore as the prefix i.e single _ or double __.

Example 4: Data Encapsulation in Python

class Computer:

    def __init__(self):
        self.__maxprice = 900

    def sell(self):
        print("Selling Price: {}".format(self.__maxprice))

    def setMaxPrice(self, price):
        self.__maxprice = price

c = Computer()
c.sell()

# change the price
c.__maxprice = 1000
c.sell()

# using setter function
c.setMaxPrice(1000)
c.sell()

Output

Selling Price: 900
Selling Price: 900
Selling Price: 1000

In the above program, we defined a Computer class.

We used __init__() method to store the maximum selling price of Computer. We tried to modify the price. However, we can’t change it because Python treats the __maxprice as private attributes.

As shown, to change the value, we have to use a setter function i.e setMaxPrice() which takes price as a parameter.


Polymorphism

Polymorphism is an ability (in OOP) to use a common interface for multiple forms (data types).

Suppose, we need to color a shape, there are multiple shape options (rectangle, square, circle). However we could use the same method to color any shape. This concept is called Polymorphism.

Example 5: Using Polymorphism in Python

class Parrot:

    def fly(self):
        print("Parrot can fly")
    
    def swim(self):
        print("Parrot can't swim")

class Penguin:

    def fly(self):
        print("Penguin can't fly")
    
    def swim(self):
        print("Penguin can swim")

# common interface
def flying_test(bird):
    bird.fly()

#instantiate objects
blu = Parrot()
peggy = Penguin()

# passing the object
flying_test(blu)
flying_test(peggy)

Output

Parrot can fly
Penguin can't fly

In the above program, we defined two classes Parrot and Penguin. Each of them have a common fly() method. However, their functions are different.

To use polymorphism, we created a common interface i.e flying_test() function that takes any object and calls the object’s fly() method. Thus, when we passed the blu and peggy objects in the flying_test() function, it ran effectively.