PYTHON: Object-oriented programing

Spread the love

1. The core concepts of object-oriented programing

Object-oriented Programming (OOP: Object-Oriented Programming) is a programming idea that treats each object as the basic unit of the program, and each object has corresponding data and functions to process the data.

2.The three key feature of OOP design philosophy

  • Polymorphism: It allows you to implement multiple methods within the same clas that use the same name. However, there are three criteria:
    • It need to have a different number of paramters. For example, one method accepting 2 and another one accepting 3 parameters
    • The types of the parameters need to be different. For example, one method accepting the string, the another accepting a long
    • It need to expect the paramters in a different order. For example, one method accepts a String and a Decimal and another one accepts a Decimal and a String. This kind of overloading is highly not recommended because it makes the interface very difficult to understand.
  • Encapsulation: It is a practice that keeping fields within a class private, then providcing access to those fields via public methods. It is like a protective barrier that keeping data and code and function safe within the class itself.
  • Inheritance: It allows you have a new class sharing the same feature with an existing class. For example, we define a class “Human”, which may has properties like:’name’,’age’,’gender’. If we defineds another class named “Employee”, which inheritae from “Human” and sharing the same features as “Human”, with “name”,”age”,”gender”, but it could have some property human may all have, like “company”, “job position”.

3. Example

Let’s define three different objects.

  • Human:
    • It will have two property: name & age.
    • Overwrite the method __str__ to return with the name and age.
  • Student
    • It need to inherite from the Human class.
    • It requires a private property “score” which can be accessed from the public.
    • It will have a method called “my_info”, by which the console will print such info like “I am a student. I am 23 years old and my name is zed. My score is 89.”
  • Professor
    • It need to inherite from the Human class.
    • It requires a private property “subject” which can be accessed from the public.
    • It will have a method called “my_info”, by which the console will print such info like “I am a profeesor. I am 59 years old and my name is annie. I am teaching computer science.”

After defining the above three objects, please initialize the following objects:

A human named Lee is 24 years old.

A student named Zed is 23 years old.

A professor named Annie is 59 years old and is teaching computer science.

4. Source Code

class Person(object):     #Encapsulate the object, we can see the defination inside the class
    def __init__ (self,name,age):
        self.name=name
        self.age=age

    def __str__(self):
        return 'Name:%s Age:%s'%(self.name,self.age)

class Student(Person):                  #Class Student was inheriated from the class Person
    def __init__ (self,name,age,score):
        self.__score=score        # __  <----It means the property is pravite,which cannot be accessed out of class
        super().__init__(name,age)
    def get_score(self):          #utilized getter to access private property
        return self.__score

    def myInfo(self):
        print('I am a student. I am %s years old. My name is %s and my grade is %s.'%(self.age,self.name,self.__score))


class Professor(Person):     
    def __init__(self,name,age,subject):
        self.__subject=subject
        super().__init__(name,age)

    def get_subject(self):
        return self.__subject

    def myInfo(self):
        print('I am a professor. I am %s years old. My name is %s and I teach %s.'%(self.age,self.name,self.__subject))
    


Lee=Person('lee',24)     #initialize the object
print(Lee)               #after define the method '__str__',based on the defination, it will print out the object's name and age (detail can be checked from line 7)

Zed=Student('Zed',23,89)
print('Score:    %s'%Zed.get_score())
Zed.myInfo()

Annie=Professor('Annie',59,'CS')
print('Subject:  %s'%Annie.get_subject())
Annie.myInfo()          #myInfo()representing the polymorphism in OOD philosophy

5. Running Result

6.Notes:

  • The funcition defined under object class is called “method”
  • The child class cannot access the private property in parent’s class
  • If the method under parent class cannot meet your business logic, you are allowed to overwrite the method in child class.
Zeren
If you want to know more about me, please get on the about page. :)
Posts created 18

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top
error: Content is protected !!