Object-Oriented Programming

Download Report

Transcript Object-Oriented Programming

Programming Languages
and Paradigms
Object-Oriented Programming
Object-Oriented Programming

Objects, classes, fields, methods
Encapsulation
Inheritance
Polymorphism and dynamic binding

Program units: Classes



OOP
Slide 2
Object
Definition: a thing that has identity, state,
and behavior



identity: a distinguished instance of a class
state: collection of values for its variables
behavior: capability to execute methods
* variables and methods are defined in a class
OOP
Slide 3
Examples of Objects




on (true or false)

behavior


LightBulb

state/attributes


switch on
switch off
check if on
state/attributes

behavior


Car



state/attributes


behavior

BankAccount
balance


deposit
withdraw
check balance
# of liters of gas in tank
total # of km run so far
efficiency (km/liter)

drive
load gas
change efficiency
check gas
check odometer reading
Note

each object is an “instance”
of that “class” of object

each instance has its own
values for its attributes

e.g., different accounts can
have different balances
OOP
Slide 4
Class
Definition: a collection of data (fields/
variables) and methods that operate on
that data



data/methods define the contents/capabilities
of the instances (objects) of the class
a class can be viewed as a factory for objects
a class defines a recipe for its objects
OOP
Slide 5
Instantiation



Object creation
Memory is allocated for the object’s fields
as defined in the class
Initialization is specified through a
constructor

a special method invoked when objects are
created
OOP
Slide 6
A Class with a Constructor



Constructor: special
method that handles
initialization
Java Example:
BankAccount
A constructor is invoked
during object
construction:
BankAccount b;
b = new BankAccount();
b.deposit( 100.00 );
Constructor call
Method call
public class BankAccount
{
private double balance;
BankAccount.java
public BankAccount()
{
balance = 0;
}
public double getBalance()
{
return balance;
}
public void deposit( double amount )
{
balance = balance + amount;
}
…
}
OOP
Slide 7
Encapsulation


A key OO concept: “Information Hiding”
Key points



The user of an object should have access only
to those methods (or data) that are essential
Unnecessary implementation details should be
hidden from the user
In Java/C++, use classes and access modifiers
(public, private, protected)
OOP
Slide 8
Inheritance

Inheritance:


Subclass relationship



programming language feature that allows for the
implicit definition of variables/methods for a class
through an existing class
B is a subclass of A
B inherits all definitions (variables/methods) in A
Superclass variables, subclass objects

Polymorphism and dynamic binding
OOP
Slide 9
Reuse



Inheritance encourages software reuse
Existing code need not be rewritten
Successful reuse occurs only through
careful planning and design

when defining classes, anticipate future
modifications and extensions
OOP
Slide 10
Polymorphism

“Many forms”


Example:


allow several definitions under a single method name
“move” means something for a person object but
means something else for a car object
Dynamic binding:

capability of an implementation to distinguish
between the different forms during run-time
OOP
Slide 11
OOP in Java and C++






Program Structure and Execution
Encapsulation and Inheritance
Objects and Variables
Methods
Pointers
Constructors
OOP
Slide 12
Program Structure


Class definition similar in Java and C++
Java: two types of programs



application (with main() function)
applet (typically embedded in a web page)
C++


a program is (still) a collection of functions
that may use objects and classes
main() function serves as driver
OOP
Slide 13
Program Execution

Java: Virtual Machine (VM)




programs: both compiled and interpreted
compiler produces .class from .java
VM loads .class file(s) as needed
C++: compiled, linked, and loaded



modules separately compiled
linked to produce executable
static vs dynamic libraries
OOP
Slide 14
Encapsulation

Enforced through access keywords




In Java


public: for interface
private: to make implementation inaccessible
protected: access for subclasses only
each member is prefixed with a keyword
In C++

public, private, and protected sections
OOP
Slide 15
Inheritance

Feature that allows a class to be defined based
on another class


Java and C++ difference



methods and attributes are inherited
Java: public class A extends B { … }
C++: class A: public B { … }
(different types of inheritance)
Multiple inheritance possible in C++, not in Java

But in Java, one may implement several interfaces
OOP
Slide 16
Objects and Identity

Questions:



How/when are objects created?
What is the relationship between a variable
and an object?
Difference between Java and C++


distinction between primitive (built-in) type
variables and variables for objects
reference relationship between variable and
actual object
OOP
Slide 17
Variables for Built-in Types

Variables for built-in types (C++ and Java)
int x;
…
x = 5;
X
X
5
OOP
Slide 18
Reference Variables
(in Java)

Reference type variables
X
BankAccount x;
…
BankAccount Object
X
100
x = new BankAccount(100.00);
OOP
Slide 19
Variables That “hold” Objects
(in C++)

Declaration of an object variable allocates
space for the object
X
BankAccount x(100.00);
100
OOP
Slide 20
Methods



A method describes a specific behavior applicable
to objects of a class
A method defines a sequence of instructions (or
statements) to be carried out when that method
is called
A method is called or invoked on an object of the
class

Carried out through the dot operator
( e.g., x.deposit( 1000.00 ); )
OOP
Slide 21
Pointers (in C++)

Variables can be explicitly declared as
pointers to objects
X
BankAccount *x;
X
…
x = new BankAccount(100.00);
BankAccount Object
100
OOP
Slide 22
Disposing of
Allocated Memory

In Java, garbage collection is automatic



Memory allocated objects are reclaimed when
no variables refer to them
Need to set reference variables to null when
the object is no longer needed
In C++, object destruction is the
programmer’s responsibility using the
delete keyword
OOP
Slide 23
delete in C++

There should be a delete for every new




SomeClass *x = new SomeClass(…);
// … use object pointed to by x
delete x; // done using object
Memory leak



Occurs when you forget to delete
Wasted memory
Can this occur in Java?
OOP
Slide 24
Object Construction

Constructor


Constructor without parameters



place where you include code that initializes the
object
“default” constructior
no additional info required
Constructor with parameters


with parameters that specify initial values or sizes
Example: public BankAccount( double initBal )
{ balance = initBal; }
OOP
Slide 25
Constructors in Java and C++

In Java,



a constructor is invoked only through the new
keyword
recall that all object variables are references
In C++,


a constructor is called upon variable declaration, or
explicitly through new with pointers, or in other
situations
other types of constructors
OOP
Slide 26
Next…






More advanced OOP features
in Java and C++
Arrays
Destructors
Operator overloading
Static vs Dynamic Binding
Many others
OOP
Slide 27