Python Classroom Series – 24/Aug/2021

Object Oriented Programming

  • Composition: Composition is the act of collecting serveral objects together to create a new one.
    • Lets take Car as an example
    • Car has a engine Preview
  • Aggregation is almost exactly like composition, The difference is aggregate objects can exist independetly
  • To differentiate aggregation and compositon is to think about lifespan of the object. If it is composition objects control when the related objects are created and destroyed
  • Refer Here for the sample done around composition
  • Exercise (Waiting till 4:50)
  • Create a class called as Address with necessary data members or attributes
  • Now Create a Student class with Address composed in it and also a faculty class with Adress composed in it.
  • Also try to create the UML Representation Preview
class Address:
    def __init__(self, street, city, pincode) -> None:
        self.street = street
        self.city = city
        self.pincode = pincode

class Student:
    def __init__(self,name, email, street, city, pincode) -> None:
        self.name = name
        self.email = email
        self.address = Address(street,city, pincode)

class Faculty:
    def __init__(self,name, email, street, city, pincode) -> None:
        self.name = name
        self.email = email
        self.address = Address(street,city, pincode)
        

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin