Python Object-Oriented Programming: Polymorphism

# Parent Class
class Shape:
def __init__(self):
self.sides = 0
def getArea(self):
pass
class Rectangle(Shape):
def __init__(self, width=0, height=0):
self.width = width
self.height = height
self.sides = 1
def getArea(self):
return (self.width * self.height)
class Circle(Shape):
def __init__(self, radius=0):
self.radius = radius
def getArea(self):
return (self.radius * self.radius * 3.14)

Duck Typing

x = 5 # type of x is an integer
print (type(x))
x = "Medium" # type x is now string
print (type(x))
class Dog:
def Speak(self):
print("Woof Woof")
class Cat:
def Speak(self):
print("Meow meow")
class AnimalSound:
def Sound(self, animal):
animal.Speak()

Abstract Base Classes

Syntax

from abc import ABC, abstractmethodclass ParentClass(ABC):

@abstractmethod
def method(self)

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Start Making Video Games, NOW!

Julia vs. Python: Which is best for data science?

Let’s deploy an app with Heroku!

SOLID Principles

Flutter Internationalization With Easy Localization

Deploying AWS Infrastructure (EFS use case) using Terraform and it’s integration with Jenkins

Beer Bot 101: Making Twitter Better, One Beer Alert at a Time

WebSocket Programming with Java

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jack Dong

Jack Dong

More from Medium

Using python poetry with .pypirc

Introduction to python dictionaries

Python Functions: Anonymous Functions

Difficult Python Question — Coding A Find Four Game