0% found this document useful (0 votes)
0 views

Code Samples- Python OOP

The document provides code samples demonstrating the use of classes and objects in Python, including class attributes, methods, inheritance, and polymorphism. It showcases examples of a Bike class, a Room class with area calculation, an Animal class with a Dog subclass, and multiple classes (Car, Boat, Plane) that implement a common method 'move'. Each example illustrates how to create objects, assign values, and call methods in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Code Samples- Python OOP

The document provides code samples demonstrating the use of classes and objects in Python, including class attributes, methods, inheritance, and polymorphism. It showcases examples of a Bike class, a Room class with area calculation, an Animal class with a Dog subclass, and multiple classes (Car, Boat, Plane) that implement a common method 'move'. Each example illustrates how to create objects, assign values, and call methods in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Code Samples:

1. Example of a Class and Object

# A. Using Class Attributes

# define a class
class Bike:
name = ""
gear = 0

# create object of class


bike1 = Bike()

# access attributes and assign new values


bike1.gear = 11
bike1.name = "Mountain Bike"

print(f"Name: {bike1.name}, Gears: {bike1.gear} ")

# B. Using Class Method

# create a class
class Room:
length = 0.0
breadth = 0.0

# method to calculate area


def calculate_area(self):
print("Area of Room =", self.length * self.breadth)

# create object of Room class


study_room = Room()

# assign values to all the properties


study_room.length = 42.5
study_room.breadth = 30.8

# access method inside class


study_room.calculate_area()

1. Example of inheritance

class Animal:
# attribute and method of the parent class
name = ""

def eat(self):
print("I can eat")

# inherit from Animal


class Dog(Animal):

# new method in subclass


def display(self):
# access name attribute of superclass using self
print("My name is ", self.name)

# create an object of the subclass


labrador = Dog()

# access superclass attribute and method


labrador.name = "Rohu"
labrador.eat()

# call subclass method


labrador.display()

# Polymorphism
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model

def move(self):
print("Drive!")

class Boat:
def __init__(self, brand, model):
self.brand = brand
self.model = model

def move(self):
print("Sail!")

class Plane:
def __init__(self, brand, model):
self.brand = brand
self.model = model

def move(self):
print("Fly!")

car1 = Car("Ford", "Mustang") #Create a Car class


boat1 = Boat("Ibiza", "Touring 20") #Create a Boat class
plane1 = Plane("Boeing", "747") #Create a Plane class

for x in (car1, boat1, plane1):


x.move()

You might also like