Skip to content

Instantly share code, notes, and snippets.

@carefree-ladka
Created February 25, 2025 07:58
Show Gist options
  • Save carefree-ladka/7802b523bd4e2548ea25fc6c774e992d to your computer and use it in GitHub Desktop.
Save carefree-ladka/7802b523bd4e2548ea25fc6c774e992d to your computer and use it in GitHub Desktop.
Topic : OOPS in Python
# OOPS (Inheritance, Polymorphism, Encapsulation, Abstraction)
"""
Access Modifiers:
public : We can anything from anywhere
protected : Only inside the class & subclass (_)
private : Only inside the class (__)
"""
# Encapsulation
# setter & getter (built in keywords)
# class Person:
# def __init__(self): # Constructor
# self.__employees = list(10)
# def person(self, emp): # setter
# self.__employees.append(emp)
# def person(self): # getter
# return self.__employees
# emp1 = Person() # Object 1 # Intanstiation
# employess = [
# {"name": "Vivek", "age": 20},
# {"name": "Amit", "age": 25},
# {"name": "John", "age": 28},
# ]
# emp1.person = employess
# print(emp1.person) # getter
class Animal: # Base class (Parent class)
def __init__(self, color, type="Dog"): # Keep default parameter in the end
self.type = type
def sleep(self):
return "sleeps"
def poop(self):
return "poops"
class Snake(Animal): # subclass Or child class
def __init__(
self,
type,
color,
name="Viper",
):
super().__init__(type, color)
self.name = name
def sleep(self):
print(self.name, super().sleep())
def poop(self):
print(self.name, super().poop())
snake = Snake("snake", "grey", "Cobra")
snake.sleep()
snake.poop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment