Tamil Nadu HSE Second Year Computer Science study materials
Tamil Nadu HSE Second Year Computer Science study resources

Tamil Nadu HSE Second Year Computer Science: Object-Oriented Programming in Python

Tamil Nadu HSE Second Year Computer Science · Work through your exam, one topic at a time.

All guide sections

Object-Oriented Programming in Python

What this domain tests

Object-oriented programming in Python: classes as blueprints, objects as instances, encapsulation, inheritance, polymorphism and abstraction. The exam asks you to write small classes, trace output, and explain why OOP organises large programs better than a loose set of functions.

Core rules

  • class Name: defines a class; the constructor init(self, ...) runs automatically when an object is created.
  • self is the reference to the current instance and must be the first parameter of instance methods. Access attributes as self.name.
  • Class attributes are shared; instance attributes are created in init and belong to one object.
  • Inheritance: class Child(Parent): gives the child the parent's methods; super().init() calls the parent constructor. Python supports multiple inheritance.
  • Polymorphism: different classes can define the same method name; overriding replaces a parent method. Python does not support compile-time method overloading by argument count.
  • A double underscore prefix such as __balance triggers name mangling and gives a form of data hiding.

Worked example

class Account: def init(self, bal): self.bal = bal def deposit(self, amt): self.bal += amt return self.bal a = Account(500) print(a.deposit(250)) The constructor stores 500 in that instance. deposit adds 250 to self.bal and returns 750, so the output is 750.

Traps that cost marks

Forgetting self in a method definition or call; writing init with one underscore; assuming init must be called manually; treating a mutable class attribute as per-object data; expecting overloading by argument count; confusing overriding with overloading.