Wa0002.
Wa0002.
Advanced Programming
Vivek Kumar
Computer Science and Engineering
IIIT Delhi
vivekk@iiitd.ac.in
About me
• Bio
– http://vivkumar.github.io/
• Lab
– http://hipec.iiitd.edu.in/
Why Object Oriented Programming?
3
© Vivek Kumar
What is OOP?
5
© Vivek Kumar
Advantages of OOP (1/3)
• Code reuse and recycling
– Objects can easily be reused
6
© Vivek Kumar
Advantages of OOP (2/3)
• Design benefits
– Extensive planning phase results better design and
lesser flaws
Car Engine
engine cylinders
Drive() Start()
7
© Vivek Kumar
Advantages of OOP (3/3)
• Software maintenance
– Easy to incorporate changes in legacy code (e.g.,
supporting a new hardware)
• Simplicity
8
© Vivek Kumar
Key Features of OOP
• Abstraction
• Encapsulation
• Method overloading
• Inheritance
• Method overriding
• Polymorphism
9
© Vivek Kumar
Abstraction
(Abstraction)
10
© Vivek Kumar
Encapsulation
Ohh.. I wish I
can change the
max speed of
this car
Internal details of
the car hidden from
the user
(Encapsulation)
11
© Vivek Kumar
Summary: Encapsulation v/s Abstraction
• An encapsulated object can be thought of as
a black box -- its inner workings are hidden
from the client
• The client invokes the interface methods of
the object, which manages the instance data
Client Methods
Abstraction lets you focus on what
the object does instead of how it
does, while encapsulation means
hiding the internal details of how Data
an object works
12
© 2004 Pearson Addison-Wesley. All rights reserved
Class
WhyWithout Encapsulation
Encapsulation?
MyClass
int data
String name void doSomething()
int getSomething()
OtherClass thing
No encapsulation
Class
WhyWithout Encapsulation
Encapsulation?
MyClass
int data
String name void doSomething()
int getSomething()
OtherClass thing
No encapsulation
Class Supporting Encapsulation
Why Encapsulation?
MyClass
int data
String name void doSomething()
int getSomething()
OtherClass thing
/* interface methods */
Encapsulation
Visibility Modifier
public private
Variables
Violate Enforce
encapsulation encapsulation
Support other
Methods
Provide services
methods in the
to clients
class
16
© 2004 Pearson Addison-Wesley. All rights reserved
Accessors and Mutators
• Because instance data is private, a class usually
provides services to access and modify data values
• An accessor method returns the current value of a
variable
• A mutator method changes the value of a variable
• The names of accessor and mutator methods take the
form getX and setX, respectively, where X is the
name of the value
• They are sometimes called “getters” and “setters”
18
© 2004 Pearson Addison-Wesley. All rights reserved
Procedural v/s OOP
19
A Sample Problem
• Write a method that will throw 2 Dice with
varying number of sides, a specified amount
of times, and reports how many times we got
a snake eyes (both dice showing 1)
return count;
} 21
© 2004 Pearson Addison-Wesley. All rights reserved
OOP Approach
• In OOP, we first focus on the main actors,
not how things are done.
• The main actors here are Dice objects. We
need to define a Dice class that captures
the state and behavior of a Dice.
• We can then instantiate as many dice
objects as we need for any particular
programs
22
© 2004 Pearson Addison-Wesley. All rights reserved
Classes (Recap)
• A class can contain data declarations and
method declarations
int size, weight;
Data declarations
char category;
Method declarations
23
© 2004 Pearson Addison-Wesley. All rights reserved
Dice Class
int faceValue;
Data declarations
int numFace;
roll()
………
24
© 2004 Pearson Addison-Wesley. All rights reserved
public class Dice {
private final int numFaces; //maximum face value
private int faceValue; //current value showing on the dice
return count;
}
© 2004 Pearson Addison-Wesley. All rights reserved 27
Instance Data
• We can depict the two Dice objects from
the RollingDice program as follows:
dice1 faceValue 5
numFaces 6
dice2 faceValue 2
numFaces 9
35