Python Classroom Series – 18/Jun/2021

Inheritance

  • There are three types of relationships b/w objects

    • association
    • composition
    • aggregation
  • There is one more kind of relationship b/w objects which can be referred as is a relationship. This is a relationship is formed by inheritance

  • Refer Here for the UML Diagrams

  • Consider a Rental Vehicle Application Preview

  • Code Example

class Vehicle:
    def start_hire(self):
        print("hire started")

    def stop_hire(self):
        print("hire finished.")

class Bike(Vehicle):
    pass

vehicle = Vehicle()
vehicle.start_hire()
vehicle.stop_hire()

pulsar_bike = Bike()
pulsar_bike.start_hire()
pulsar_bike.stop_hire()
  • Chess Pieces Preview

  • Lets try to design classes for Chess Game

    • Player
    • Chess Set
    • Pieces
      • Pawn
      • Rook
      • Knight
      • Bishop
      • Queen
      • King
    • Board
    • Position (64 Positions)
  • Pieces Class Diagram Preview

  • Complete Class Diagram Preview

  • Sample code for the classes

class Piece:
    """
    This represents a piece in the chess game
    """
    def __init__(self,color,board): 
        """
        Initializer for the piece
        """
        self._color = color
        self._board = board

    def move(self):
        """
        This method defines the movement of chess piece
        """
        print("No movement defined")


class Pawn(Piece):
    def move(self):
        print("Single positon forward or cross")

class Bishop(Piece):
    def move(self):
        print("Move Cross in any direction by any number of steps")


class Board:
    pass
my_chess_board = Board()

bb1 = Bishop(color='Black', board=my_chess_board)
bb2 = Bishop(color='Black', board=my_chess_board)
wb1 = Bishop(color='White', board=my_chess_board)
wb2 = Bishop(color='White', board=my_chess_board)
bb1.move()

bp1 = Pawn(color='Black', board=my_chess_board)
bp1.move()

Exercise

  • Create a Class Diagram for Bus Ticket Booking application
  • Here the following are the few classes
    • User
    • Bus
      • Sleeper
      • Seater
      • Sleeper and Seater
    • Bus Agency
    • Ticket
    • Route Preview

Leave a Comment

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

About learningthoughtsadmin