0% found this document useful (0 votes)
21 views3 pages

OOP Concepts: Classes, Methods, Inheritance

Hdhdhsjsjsjhshsxbbshsjbbbzbsbzbxbzhzjsjdbbxbdbshs

Uploaded by

N.Akshay Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

OOP Concepts: Classes, Methods, Inheritance

Hdhdhsjsjsjhshsxbbshsjbbbzbsbzbxbzhzjsjdbbxbdbshs

Uploaded by

N.Akshay Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module 1: Introduction to OOPS, Methods

Classes and Objects, Constructors, Namespaces, Encapsulation, Abstraction, Creating a Class, The self variable,
Constructor - Types of Methods: Instance Method, Class Method, Static Method, Passing members of one class to
another class - Create a Parent Class, Create a Child Class, Add the __init__() Function, Use the super() Function, Add
Properties

### 1. **Classes and Objects, Constructors, Namespaces**

class Person:
# Constructor
def __init__(self, name, age):
[Link] = name # Namespace (instance variable)
[Link] = age

# Instance Method
def display(self):
print(f"Name: {[Link]}, Age: {[Link]}")

# Create an object of the class


person1 = Person("Pratik", 30)
[Link]()

### 2. **Encapsulation and Abstraction**

class Account:
def __init__(self, balance):
self.__balance = balance # Private variable (Encapsulation)

def deposit(self, amount):


if amount > 0:
self.__balance += amount

def withdraw(self, amount):


if amount <= self.__balance:
self.__balance -= amount
else:
print("Insufficient balance!")

def get_balance(self):
return self.__balance # Public method to access private variable

# Creating an Account object


acc = Account(1000)
[Link](500)
[Link](200)
print(f"Remaining Balance: {acc.get_balance()}")
### 3. **Creating a Class, The `self` Variable, Constructor - Types of Methods:
Instance, Class, Static Methods**

class Employee:
company_name = "TechCorp" # Class variable

def __init__(self, name, salary):


[Link] = name # Instance variable
[Link] = salary

# Instance Method
def display_details(self):
print(f"Employee: {[Link]}, Salary: {[Link]}")

# Class Method
@classmethod
def get_company_name(cls):
return cls.company_name

# Static Method
@staticmethod
def is_working_day(day):
return [Link]() not in ['saturday', 'sunday']

# Using the class


emp = Employee("Alice", 60000)
emp.display_details()
print(f"Company: {Employee.get_company_name()}")
print(f"Is Monday a working day? {Employee.is_working_day('Monday')}")
```

### 4. **Inheritance: Create a Parent Class, Create a Child Class, Add the `__init__()` and
`super()` Functions**

class Vehicle:
def __init__(self, brand, model):
[Link] = brand
[Link] = model

def display_info(self):
print(f"Brand: {[Link]}, Model: {[Link]}")

# Child class inheriting from Vehicle


class Car(Vehicle):
def __init__(self, brand, model, year):
super().__init__(brand, model) # Use of super()
[Link] = year

def display_car_info(self):
print(f"Car Info: {[Link]} {[Link]} - {[Link]}")
# Creating an object of the Child class
car1 = Car("Toyota", "Camry", 2021)
car1.display_car_info()

### 5. **Passing Members of One Class to Another**

class Engine:
def __init__(self, horsepower):
[Link] = horsepower

def display_engine_info(self):
print(f"Engine Horsepower: {[Link]} HP")

class Car:
def __init__(self, brand, model, engine):
[Link] = brand
[Link] = model
[Link] = engine # Passing object of Engine class

def display_car_info(self):
print(f"Car: {[Link]} {[Link]}")
[Link].display_engine_info()

# Create an Engine object


engine1 = Engine(150)

# Create a Car object, passing the Engine object as an argument


car1 = Car("Honda", "Civic", engine1)
car1.display_car_info()

You might also like