1 of 29

Introduction to �Object Oriented Programming�

Gayathri Namasivayam

Introduction to OOP

2 of 29

Topics

  • Why use OOP?
  • Building blocks of OOP
    • Classes
    • Objects
  • What is OOP?
  • OOP concepts
    • Abstraction
    • Encapsulation
    • Inheritance
    • Polymorphism
  • Advantages vs Disadvantages
  • Conclusion

Introduction to OOP

3 of 29

Topics

  • Why use OOP?
  • Building blocks of OOP
    • Classes
    • Objects
  • What is OOP?
  • OOP concepts
    • Abstraction
    • Encapsulation
    • Inheritance
    • Polymorphism
  • Advantages vs Disadvantages
  • Conclusion

Introduction to OOP

4 of 29

Why use OOP?

  • Object Oriented Programming (OOP) is one of the most widely used programming paradigm

  • Why is it extensively used?
    • Well suited for building trivial and complex applications
    • Allows re-use of code thereby increasing productivity
    • New features can be easily built into the existing code
    • Reduced production cost and maintenance cost

  • Common programming languages used for OOP include C++, Java, and C#

Introduction to OOP

5 of 29

Topics

  • Why use OOP?
  • Building blocks of OOP
    • Classes
    • Objects
  • What is OOP?
  • OOP concepts
    • Abstraction
    • Encapsulation
    • Inheritance
    • Polymorphism
  • Advantages vs Disadvantages
  • Conclusion

Introduction to OOP

6 of 29

Building Blocks of OOP: Objects & Classes

  • Object: models a
    • Real world object (ex. computer, book, box)
    • Concept (ex. meeting, interview)
    • Process (ex. sorting a stack of papers or comparing two computers to measure their performance)

  • Class: prototype or blueprint from which objects are created

Introduction to OOP

7 of 29

Building Blocks of OOP: Objects & Classes

  • Class has
    • Set of attributes or properties that describes every object
    • Set of behavior or actions that every object can perform

  • Object has
    • Set of data (value for each of its attribute)
    • Set of actions that it can perform
    • An identity

  • Every object belongs to a class

Introduction to OOP

8 of 29

Real World Example of Objects & Classes

Introduction to OOP

Object: FordCar1

Behavior

Start, Accelerate, Reverse, Stop

Attributes

Color: Yellow

Type: Coupe

Model: Mustang

Cylinder: 6

Attributes

Color, Type, Model, Cylinder

Behavior

Start, Accelerate, Reverse, Stop

Class: FordCar

Behavior

Start, Accelerate, Reverse, Stop

Attributes

Color: Orange

Type: Coupe

Model: Focus

Cylinder: 4

Object: FordCar2

9 of 29

Another Real World Example..

Introduction to OOP

Attributes

Name, Height, Age

Behavior

Speak, Listen, Eat, Run, Walk

Class: Person

Object: Person1

Attributes

Name: Ann

Height: 5’ 4”

Age: 21

Behavior

Speak, Listen, Eat, Run, Walk

Attributes

Name: Adam

Height: 5’ 9”

Age: 24

Behavior

Speak, Listen, Eat, Run, Walk

Object: Person2

10 of 29

Class

  • A class is a set of variables (to represent its attributes) and functions (to describe its behavior) that act on its variables

Introduction to OOP

11 of 29

Class ShippingBox

Introduction to OOP

int shipping_cost() {

return cost_per_pound*weight;

}

sender_name : string

receiver_name : string

cost_per_pound : int

weight : int

shipping_cost() : int

12 of 29

Object

  • Object is an instance of a class that holds data (values) in its variables. Data can be accessed by its functions

Introduction to OOP

13 of 29

Objects of ShippingBox class

Introduction to OOP

sender_name = Jim

receiver_name = John

cost_per_pound = 5

weight = 10

shipping_cost()

Object BoxB

Object BoxA

sender_name = Julie

receiver_name = Jill

cost_per_pound = 2

weight = 5

shipping_cost()

Class ShippingBox

14 of 29

Topics

  • Why use OOP?
  • Building blocks of OOP
    • Classes
    • Objects
  • What is OOP?
  • OOP concepts
    • Abstraction
    • Encapsulation
    • Inheritance
    • Polymorphism
  • Advantages vs Disadvantages
  • Conclusion

Introduction to OOP

15 of 29

What is OOP?

  • Paradigm for problem solving by interaction among objects
  • It follows a natural way of solving problems

    • Ex. Ann wants to start her car

(1) Ann walks to her car

(2) Ann sends a message to the car to start by turning on the ignition

(3)The car starts

Introduction to OOP

16 of 29

Problem Solving in OOP

Problem: Ann wants to start her car

Introduction to OOP

Name = Ann

Age = 21

Speak()

Run()

Walk()

Object Ann

Color = Yellow

Type = Coupe

Model = Mustang

Cylinder = 6

Start()

Accelerate()

Stop()

Object Ann’s car

message

17 of 29

Topics

  • Why use OOP?
  • Building blocks of OOP
    • Classes
    • Objects
  • What is OOP?
  • OOP concepts
    • Abstraction
    • Encapsulation
    • Inheritance
    • Polymorphism
  • Advantages vs Disadvantages
  • Conclusion

Introduction to OOP

18 of 29

Abstraction

Introduction to OOP

  • Extracting essential properties and behavior of an entity
  • Class represents such an abstraction and is commonly referred to as an abstract data type

Ex. In an application that computes the shipping cost of a box, we extract its properties: cost_per_pound, weight and its behavior: shipping_cost()

19 of 29

Abstraction

Introduction to OOP

sender_name : string

receiver_name : string

cost_per_pound : int

weight : int

shipping_cost() : int

Class

Shipping Box

Attributes

Sender’s name,

Receiver’s name,

Cost of shipping per pound,

Weight

Behavior

Calculate shipping cost

Abstraction

20 of 29

Topics

  • Why use OOP?
  • Building blocks of OOP
    • Classes
    • Objects
  • What is OOP?
  • OOP concepts
    • Abstraction
    • Encapsulation
    • Inheritance
    • Polymorphism
  • Advantages vs Disadvantages
  • Conclusion

Introduction to OOP

21 of 29

Encapsulation

Introduction to OOP

sender_name

receiver_name

cost_per_pound

weight

shipping_cost()

  • Mechanism by which we combine data and the functions that manipulate the data into one unit
  • Objects & Classes enforce encapsulation

22 of 29

Topics

  • Why use OOP?
  • Building blocks of OOP
    • Classes
    • Objects
  • What is OOP?
  • OOP concepts
    • Abstraction
    • Encapsulation
    • Inheritance
    • Polymorphism
  • Advantages vs Disadvantages
  • Conclusion

Introduction to OOP

23 of 29

Inheritance

  • Create new classes (derived classes) from existing classes (base classes)
  • The derived class inherits the variables and functions of the base class and adds additional ones!
  • Provides the ability to re-use existing code

Introduction to OOP

24 of 29

Inheritance Example

BankAccount CheckingAccount SavingsAccount

Introduction to OOP

customer_name : string

account_type : string

balance : int

insufficient_funds_fee : int

deposit() : int

withdrawal() : int

process_deposit() : int

customer_name : string

account_type : string

balance : int

deposit() : int

withdrawal() : int

customer_name : string

account_type : string

balance : int

interest_rate : int

deposit() : int

withdrawal() : int

calculate_interest() : int

25 of 29

Inheritance Example

Introduction to OOP

interest_rate : int

calculate_interest() : int

insufficient_funds_fee : int

process_deposit() : int

CheckingAccount

SavingsAccount

customer_name : string

account_type : string

balance : int

deposit() : int

withdrawal() : int

BankAccount

26 of 29

Topics

  • Why use OOP?
  • Building blocks of OOP
    • Classes
    • Objects
  • What is OOP?
  • OOP concepts
    • Abstraction
    • Encapsulation
    • Inheritance
    • Polymorphism*
  • Advantages vs Disadvantages
  • Conclusion

(* To be covered in the next class)

Introduction to OOP

27 of 29

Disadvantages of OOP

  • Initial extra effort needed in accurately modeling the classes and sub-classes for a problem
  • Suited for modeling certain real world problems as opposed to some others

Introduction to OOP

28 of 29

Conclusion

  • Classes & Objects
  • Concepts in OOP
    • Abstraction
    • Encapsulation
    • Inheritance
  • Advantages & Disadvantages
  • Next class
    • Be prepared for an in-class activity (based on topics covered today)!
    • Polymorphism in OOP!

Introduction to OOP

29 of 29

Thank you!

Introduction to OOP