Object-Oriented Programming | Individual Assignment Mattu University — ECE Dept.
MATTU UNIVERSITY
College of Engineering and Technology
Department of Electrical and Computer Engineering
Individual Assignment
Introduction to Object-Oriented Programming
1. Introduction to Object-Oriented Programming (OOP)
At its core, Object-Oriented Programming — or OOP — is a way of thinking about and organizing
code. Instead of writing a long list of instructions for the computer to follow from top to bottom, OOP
encourages us to think in terms of 'things' (objects) that have their own properties and can perform
their own actions. It's actually quite close to how we see the real world.
Take a dog, for example. A dog has attributes — a name, a breed, a color — and it can do things,
like bark or fetch. In OOP, we'd model that dog as an object. The blueprint that describes what
every dog looks like and what every dog can do is called a class. So a class is the template, and an
object is the actual thing created from that template.
OOP became popular because it makes large programs much easier to manage. When your code
is organized around objects that each handle their own data and behavior, things like fixing bugs,
adding new features, or reusing code in another project become far less painful.
The four principles that OOP is built on are:
• Encapsulation — keeping an object's data bundled together with the methods that work on
it, and hiding the internal details from the outside world.
• Abstraction — showing only what's necessary and keeping the complicated stuff hidden
underneath.
• Inheritance — letting a new class 'borrow' the properties and behaviors of an existing one,
so we don't have to rewrite the same code twice.
• Polymorphism — allowing different objects to respond to the same method call in their own
unique way.
2. OOP vs. Procedure-Oriented Programming (POP)
Before OOP came along, most programs were written using Procedure-Oriented Programming
(POP). In POP, you basically write a sequence of instructions — called procedures or functions —
and the program moves through them step by step. The focus is entirely on what the program does
(the logic), not on what it's working with (the data).
Page 1 of 6
Object-Oriented Programming | Individual Assignment Mattu University — ECE Dept.
OOP flipped that around. Instead of asking 'what steps do I need to follow?', OOP asks 'what things
exist in this problem, and how do they interact?' Both approaches work, but they suit different kinds
of problems. OOP tends to shine when building larger, more complex systems where managing and
reusing code matters a lot.
Here's a side-by-side look at how the two compare:
Aspect OOP POP
Basic Unit Object (data + behavior) Function/Procedure
Focus Data and objects are primary Functions/logic are primary
Data Access Hidden inside objects Data is shared globally
(encapsulation)
Reusability High — via inheritance & Limited — code duplication
polymorphism common
Problem Approach Bottom-up design Top-down design
Security High — data hidden from Low — global data accessible
external access anywhere
Examples Java, Python, C++ C, Pascal, FORTRAN
3. Core OOP Concepts
3.1 Data Abstraction
Abstraction is probably the most philosophical of the four concepts, but it's also one of the most
practical. The idea is simple: show only what the user needs to see, and hide the messy details
underneath.
Think about using a TV remote. You press the volume button and the TV gets louder — but you
have no idea what's happening inside the circuit board, and honestly, you don't need to. That's
abstraction in action. In OOP, we use abstract classes and interfaces to define what an object
should do without spelling out exactly how it does it.
• It reduces complexity — the user of a class doesn't need to understand its internals.
• It improves security — sensitive implementation details stay hidden.
• It makes the code easier to maintain, since changes to the hidden part don't affect the
visible interface.
3.2 Polymorphism
Polymorphism literally means 'many forms,' and that's exactly what it is — one name, many
behaviors. It lets us write code that works on different types of objects through the same interface,
which makes programs much more flexible and easier to extend.
It comes in two flavors:
Page 2 of 6
Object-Oriented Programming | Individual Assignment Mattu University — ECE Dept.
• Compile-time (Static) Polymorphism — this happens through method overloading, where
you have multiple methods with the same name but different parameters. The compiler
figures out which one to call.
• Run-time (Dynamic) Polymorphism — this happens through method overriding, where a
subclass rewrites a method from its parent class. The right version is chosen when the
program is actually running.
A simple example: imagine a makeSound() method in an Animal class. If you call it on a Horse
object, it might print 'Neigh.' Call it on a Cat, and you get 'Meow.' Same method name, completely
different behavior — that's polymorphism.
3.3 Inheritance
Inheritance is one of the most useful features in OOP, and once you see it in action, it's hard to
imagine coding without it. The idea is that a new class (called a subclass or child class) can inherit
the properties and behaviors of an existing class (the superclass or parent class). This saves a
huge amount of repetitive code.
The main types of inheritance are:
• Single Inheritance — one child class inherits from one parent class. The most
straightforward type.
• Multilevel Inheritance — class C inherits from class B, which itself inherits from class A. Like
a family tree with multiple generations.
• Hierarchical Inheritance — multiple subclasses all inherit from the same single parent class.
Why is this so useful?
• You write common code once in the parent class, and all child classes automatically get it.
• You can extend an existing class with new features without touching the original code.
• It naturally models real-world relationships — a Horse IS an Animal, and a Cat IS an Animal.
3.4 Encapsulation
Encapsulation is about keeping things tidy and protected. The idea is to bundle an object's data (its
attributes) and the methods that work on that data into a single unit — the class — and then control
who gets to access what.
In practice, this usually means making class variables private (so nothing outside the class can
touch them directly) and providing public getter and setter methods for controlled access. It's a bit
like a bank vault — the money is locked inside, but there's a proper process for depositing and
withdrawing.
• Protects data from accidental or unauthorized changes.
• Each class becomes self-contained and easier to test independently.
• If you need to change how data is stored internally, you only have to update the class itself
— everything else stays the same.
Page 3 of 6
Object-Oriented Programming | Individual Assignment Mattu University — ECE Dept.
4. Implementation: Inheritance Using Animal, Horse, and Cat
To put everything together, let's look at a practical Java example that demonstrates inheritance.
The Animal class acts as our superclass — it holds the shared properties that both horses and cats
have in common (name, food, number of legs, and whether they have a tail). It also has an eat()
method, since all animals eat.
The Horse and Cat classes then extend Animal using the extends keyword. Each subclass calls
super() in its constructor to pass its own specific values up to the parent, so the Animal constructor
can store them. This way, we write the storage logic only once — in Animal — and both subclasses
benefit automatically.
Java Source Code:
// Super class
class Animal {
String name;
String food;
int numberOfLegs;
boolean hasTail;
// Super class constructor
Animal(String name, String food, int numberOfLegs, boolean hasTail) {
[Link] = name;
[Link] = food;
[Link] = numberOfLegs;
[Link] = hasTail;
}
// Super class method
void eat() {
[Link](name + " eats " + food);
}
}
// Sub class Horse
class Horse extends Animal {
Horse() {
super("Horse", "Grass & Hay", 4, true);
}
void display() {
[Link]("--- Horse ---");
[Link]("Name : " + name);
[Link]("Food : " + food);
[Link]("Number of legs: " + numberOfLegs);
[Link]("Has tail : " + hasTail);
eat();
}
}
// Sub class Cat
class Cat extends Animal {
Cat() {
super("Cat", "Meat & Fish", 4, true);
}
Page 4 of 6
Object-Oriented Programming | Individual Assignment Mattu University — ECE Dept.
void display() {
[Link]("--- Cat ---");
[Link]("Name : " + name);
[Link]("Food : " + food);
[Link]("Number of legs: " + numberOfLegs);
[Link]("Has tail : " + hasTail);
eat();
}
}
// Main class
public class Main {
public static void main(String[] args) {
Horse horse = new Horse();
[Link]();
Cat cat = new Cat();
[Link]();
}
}
Program Output:
--- Horse ---
Name : Horse
Food : Grass & Hay
Number of legs: 4
Has tail : true
Horse eats Grass & Hay
--- Cat ---
Name : Cat
Food : Meat & Fish
Number of legs: 4
Has tail : true
Cat eats Meat & Fish
Breaking it down:
• Animal is the superclass. Its constructor takes in name, food, numberOfLegs, and hasTail —
the four things every animal in this program shares.
• The eat() method lives in Animal because every animal eats. Both subclasses inherit it for
free, without writing it again.
• Horse calls super("Horse", "Grass & Hay", 4, true) to send its specific values up to the
Animal constructor. Cat does the same with its own values.
• Both subclasses have a display() method that prints all the inherited fields and then calls
eat() — showing that inherited methods work exactly as if they were defined in the subclass
itself.
• This is the real power of inheritance: common logic lives in one place, and subclasses only
add what makes them unique.
Page 5 of 6
Object-Oriented Programming | Individual Assignment Mattu University — ECE Dept.
End of Assignment
Page 6 of 6