Inheritance, Encapsulation and Polymorphism

Object-Oriented Programming in Python

Allie Hsu
Geek Culture

--

Photo by davisuko on Unsplash

Everything in python is object. Classes, functions and even simple data types, such as integer and float.

Object-Oriented Programming makes the program easy to understand as well as efficient. Use it wisely could let development faster and easier! OOP has several advantages, such as reusable, maintainable, easy to expansion, flexibility and secure, etc. For more detail about the benefits, please refer to this article from EDUCBA: Advantage of OOP.

This article will introduce the three main concepts in Object-Oriented Programming and how they work:

Inheritance, Encapsulation and Polymorphism

We will create an Employee class as an example. In this case, the Employee class has two attributes: name and age, as well as two methods: description and speak.

class Employee:
def __init__(self, name, age):
self.name = name
self.age = age
def description(self):
return '{} is {} years old.'.format(self.name, self.age)
def speak(self, sound):
return '{} says {}.'.format(self.name, sound)

--

--

Allie Hsu
Geek Culture

A tech enthusiast who is keen to develop new skills