Object-Oriented Programming is a paradigm that organises code around objects - entities that combine data and behaviour.
Classes and Objects
An object is a specific instance of a class - a real entity created from the blueprint.
// CLASS - the blueprint
class Dog:
// Attributes (data)
private name
private breed
private age
// Constructor - creates new objects
public procedure new(givenName, givenBreed, givenAge):
name = givenName
breed = givenBreed
age = givenAge
end procedure
// Methods (behaviour)
public procedure bark():
print(name + " says: Woof!")
end procedure
public function getAge():
return age
end function
end class
// OBJECTS - instances of the class
myDog = new Dog("Rex", "German Shepherd", 3)
yourDog = new Dog("Bella", "Labrador", 5)
// Using the objects
myDog.bark() // Output: Rex says: Woof!
print(yourDog.getAge()) // Output: 5
Encapsulation
• Private attributes/methods: Only accessible within the class
• Public attributes/methods: Accessible from outside the class
Why Encapsulate?
• Protects data from invalid modifications
• Hides implementation details
• Makes code easier to maintain
• Provides controlled access through methods (getters/setters)
class BankAccount:
private balance // Cannot be accessed directly from outside
public procedure deposit(amount):
if amount > 0 then
balance = balance + amount
end if
end procedure
public function getBalance():
return balance
end function
end class
// Usage
account = new BankAccount()
// account.balance = 1000000 // ERROR - balance is private!
account.deposit(500) // OK - uses public method
print(account.getBalance()) // OK - uses public method
Inheritance
// SUPERCLASS (parent)
class Animal:
private name
private age
// Constructor
public procedure new(givenName, givenAge):
name = givenName
age = givenAge
end procedure
public function getName():
return name
end function
public procedure makeSound():
print(getName() + " makes a sound")
end procedure
end class
// SUBCLASS (child) - inherits from Animal
class Dog inherits Animal:
private breed
// Dog has an extra attribute, so it has its own constructor
public procedure new(givenName, givenAge, givenBreed):
super.new(givenName, givenAge) // Run the constructor of Animal
breed = givenBreed
end procedure
// Override the parent method
public procedure makeSound():
print(getName() + " barks: Woof!")
end procedure
// A method only Dog has
public procedure fetch():
print(getName() + " fetches the ball")
end procedure
end class
// SUBCLASS (child) - uses the constructor it inherits from Animal
class Cat inherits Animal:
public procedure makeSound():
print(getName() + " meows: Meow!")
end procedure
end class
// Usage
myDog = new Dog("Rex", 3, "German Shepherd")
myDog.makeSound() // Output: Rex barks: Woof!
myDog.fetch() // Output: Rex fetches the ball
myCat = new Cat("Whiskers", 5)
myCat.makeSound() // Output: Whiskers meows: Meow!
// myDog.name = "Max" // ERROR - name is private to Animal
Polymorphism
// Both Dog and Cat inherit from Animal
animals = [new Dog("Rex", 3, "German Shepherd"), new Cat("Whiskers", 5), new Dog("Bella", 2, "Labrador")]
// Polymorphism - same method call, different behaviour
for each animal in animals do
animal.makeSound()
end for
// Output:
// Rex barks: Woof!
// Whiskers meows: Meow!
// Bella barks: Woof!
// The code doesn't need to know the specific type -
// each object responds appropriately
OOP Principles Summary
• Description: Bundle data and methods, hide internals
• Benefit: Data protection, maintainability
Inheritance
• Description: Child classes inherit from parent
• Benefit: Code reuse, hierarchy
Polymorphism
• Description: Same interface, different implementations
• Benefit: Flexibility, extensibility
Abstraction
• Description: Hide complex details, show only essentials
• Benefit: Simplicity, focus
• OOP: Data and methods together in objects
• Procedural: Data and procedures separate
• OOP better for: Large projects, code reuse, modelling real-world entities
• Procedural better for: Small scripts, simple programs, learning