You are on page 1of 22

Introduction to Object Oriented

Programming

CS F213: Object Oriented Programming


Instructor: Dr. Rahul Thakur
Once Upon a Time…
• Large and heavy computers
– Used for industrial and scientific applications
• Programs were written in Binary and manually
fed to the computers for processing
• Assembly language: Simplifying the
programmers job
• High level language

Object Oriented Programming (CS F213)


The Modern Programming
• The C programming language
– Structured
– Efficient
– High-level  Statements written in simple English
– Replaced Assembly code
• The trade-offs
– Ease-of-use vs. Power
– Rigidity vs. Extensibility

Object Oriented Programming (CS F213)


The Next Step
• C became the dominant programming language
– And still widely used today
• Do we need of other Programming Languages?
– Yes! But Why?
– The answer is “Complexity”

Object Oriented Programming (CS F213)


Example: BITS Admission Process
• Steps involved
– Fill the application form
– Appear for BITSAT
– Registration at security gate
– Hostel allotment
– Admission
– Documentation
– Mess allotment…..
• Bad approach  All steps in just one function
• Better approach  One function for each step

Object Oriented Programming (CS F213)


Function Oriented Programming
• Also called Procedural Programming
• The primary focus is on Functions
• Secondary focus in on Data
• Suitable for simple programs
– Split bigger tasks into smaller one
• As program gets bigger and bigger
– Unmanageable by programmers
Object Oriented Programming (CS F213)
Object-Oriented Programming
• Heart of Object Oriented Programming
– Objects
– Classes
• Entire world is a collection of objects
• Objects interact with other objects
• Objects perform predefined tasks

Object Oriented Programming (CS F213)


Example
• Pet management system,
specially for dogs*
• Needs to store information
– breed, age, size, color
• Real life thing  Software
Entity
• How to design such a *https://www.guru99.com/java-oops-class-objects.html

software
Object Oriented Programming (CS F213)
Spot The Difference

Object Oriented Programming (CS F213)


Common Characteristics

©Image Copyright - Guru99 2018 Object Oriented Programming (CS F213)


Common Actions

Object Oriented Programming (CS F213)


Class and Objects

• Class is a logical construct, object is physical reality


Object Oriented Programming (CS F213)
Object Oriented Programming (OOP)
• OOP language provides us mechanism to
implement object oriented model
– Encapsulation
– Inheritance The OOP Principles
– Polymorphism
• And how all these three work together
– Robust and scalable
Object Oriented Programming (CS F213)
Encapsulation
• Encapsulation
– Binds together the code and data it manipulates
– Safety from outside interference and misuse
• Act like a wrapper
– Access to code and data inside a wrapper is via
well defined interfaces

Object Oriented Programming (CS F213)


Encapsulation
• Example: A Car
– Combine all the component under the hood
• Engine, transmission, cooling system, music system…
– Well defined interfaces
• Steering, bakes, accelerating, locking, playing music…
– Safety
• Can’t control car’s movement via music system buttons

Object Oriented Programming (CS F213)


Encapsulation
• In OOP, basis of encapsulation is Class
– Defines the structure (Data)
– Defines the behavior (Code)
• All objects of a class contain this structure and
behavior
• Object  Instance of a Class

Object Oriented Programming (CS F213)


Encapsulation and Classes
• Class contains
Member/Instance Variables
– Data
Members of a Class
– Code Member Methods

• Class encapsulate complexity


• Encapsulation vs. Abstraction

Object Oriented Programming (CS F213)


Inheritance
• One object acquire properties of another object
Breed
Breed Size
Breed Size
Size Age
Age Color
Age Color
Color Weight
Weight Owner
Eat() Eat() Eat()
Sleep() Sleep() Sleep()
Sit() Sit() Sit()
Run() Run() Run()
Jump()

• Support hierarchical classification


Object Oriented Programming (CS F213)
Inheritance
• Class hierarchy Animals

– Superclass
4 Legged 2 Legged
– Subclass
• Subclass inherits all the
Dogs
properties of superclass Cats

• Advantages Class
Labrador
– Code reuse Pug Hierarchy
– Close interaction with Encapsulation
Object Oriented Programming (CS F213)
Polymorphism
• “Many Forms”
• One interface for general class of actions
• Example: Dog’s sense of smell is polymorphic
– Smells a cat  Bark and run after it
– Smells its food  Wags the tail and run for the bowl
• Sense of smell works in both situation
– Difference: Type of data (smell) being operated upon
by dog’s nose
Object Oriented Programming (CS F213)
Polymorphism
• Polymorphism in the programming world
• Example: Calculate the area
– For Circle: Radius r
– For Rectangle: Length l, Width w
– For Sector: Radius r, Angle θ
• A single function to calculate all the above
– Differentiate: Data Type and Number

Object Oriented Programming (CS F213)


Next Class
• Introduction to Java and its features
• Sample Java programs
• Compilation and execution
• Lexical Issues
• Control Statements (if and for)

Object Oriented Programming (CS F213)