object oriented programming n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Object Oriented Programming PowerPoint Presentation
Download Presentation
Object Oriented Programming

Loading in 2 Seconds...

play fullscreen
1 / 39

Object Oriented Programming - PowerPoint PPT Presentation


  • 143 Views
  • Uploaded on

Object Oriented Programming. Chapter 2 introduces Object Oriented Programming. OOP is a relatively new approach to programming which supports the creation of new data types and operations to manipulate those types. This presentation introduces OOP. Data Structures and Other Objects

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Object Oriented Programming' - boyce


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
object oriented programming
Object OrientedProgramming
  • Chapter 2 introduces Object Oriented Programming.
  • OOP is a relatively new approach to programming which supports the creation of new data types and operations to manipulate those types.
  • This presentation introduces OOP.

Data Structures

and Other Objects

Using Java

abstract data type adt
Abstract Data Type (ADT)
  • A new type
  • Contains some form of data representation
  • Provides operations for manipulating the data
object oriented programming1
Object-Oriented Programming
  • A methodology for how programs are written
  • Extension of ADT concept to include inheritance capability
  • An abstraction of the

real world

animal

mammal

canine

dog

Lakota

key principles of adts and oop
Key Principles of ADTs and OOP
  • Encapsulation
    • A way of organizing all the components of an object into one entity
    • E.g. class libraries
  • Information Hiding
    • A way of separating the descriptive specification for an object from its implementation
    • E.g. primitive data types such as double
object
Object
  • Defines a new type of data
  • Includes data members (usually hidden) called instance variables
  • Includes member methods that define the permissible operations on the data members
  • Includes constructors – special methods that provide initializations for instance variables
example throttle

- 6

  • 5
  • 4
  • 3
  • 2
  • 1
  • Off

getFlow

isOn

shift

shutOff

Example - Throttle

Throttle

Top

6

Hidden

Position

4

example throttle1

- 6

  • 5
  • 4
  • 3
  • 2
  • 1
  • Off

shift

shutOff

Example - Throttle

Throttle

Top

6

Position

4

getFlow

isOn

java class
Java Class
  • In Java (and some other programming languages), objects are built using the mechanism called a class
  • A class encapsulates all of the components of the object, including instance variables, constructor(s), and method definitions
defining a new class in java
Defining a New Class in Java

public class <class-name>

{

<instance variables>

<methods>

}

throttle class
Throttle Class

public class Throttle

{

private int top;

private int position;

<methods>

}

Name begins with capital letter

Instance variables

constructor methods
Constructor Methods
  • Responsible for initializing instance variables
  • Can have more than one, as long as they have different signatures
  • Can have no constructor – default constructor
  • A method with no return type
throttle constructor
Throttle Constructor

public Throttle(int size)

Parameters: size, the number of on positions

Precondition

Size > 0

Postcondition

Throttle initialized with specified # of on positions; initially off

Throws: IllegalArgumentException

slide13
public Throttle(int size)

{

if (size <= 0)

throw new IllegalArgumentException (“Size <= 0: “ + size);

top = size;

position = 0;

}

throttle constructor1
Throttle Constructor

public Throttle( )

Parameters: none

Precondition: none

Postcondition

Throttle initialized with 5 on positions; initially off

Throws: IllegalArgumentException

slide15
public Throttle( )

{

top = 5;

position = 0;

}

methods
Methods
  • Accessor – gives information about an object without altering it
  • Modifier – changes the “status” of an object (generally by changing instance variables)
throttle accessor getflow
Throttle Accessor getFlow( )

public double getFlow( )

Get the current flow of this Throttle

Returns:

The current flow rate

slide18
public double getFlow( )

{

return (double) position / (double) top;

}

reasons for accessor methods
Reasons for Accessor Methods
  • Programmer using Throttle doesn’t need to worry how its implemented
  • Could later change Throttle implementation without affecting existing user programs
  • Method can be thoroughly tested
  • Information hiding keeps programmers from using instance variables in unintended ways (e.g. setting position to negative)
throttle accessor ison
Throttle Accessor isOn( )

public boolean isOn( )

Check whether Throttle is on

Returns

If Throttle flow is above zero, returns true; otherwise returns false.

slide21
public boolean isOn( )

{

return (position > 0);

}

Question: why doesn’t the programmer just check the value of position directly?

throttle modifier shutoff
Throttle Modifier shutOff( )

public void shutOff( )

Turn off this Throttle

Precondition: none

Postcondition: the Throttle’s flow is shut off

slide23
public void shutOff( )

{

position = 0;

}

throttle modifier shift
Throttle Modifier shift( )

public void shift( int amount)

Move Throttle position up or down

Parameters:

amount – the amount to move the position up or down

Postconditions:

Throttle’s position has been moved by amount. If result is more than top position, then position set to top. If result is less than zero position, then position set to zero.

slide25

Why isn’t this:

(position + amount > top)?

public void shift(int amount)

{

if (amount > top - position)

position = top;

else if (position + amount < 0)

position = 0;

else

position += amount;

}

methods activating calling methods
Methods Activating (Calling) Methods

public boolean isOn()

{

return (getFlow() > 0);

}

Could just reference position instance variable

creating and using objects
Creating and Using Objects

Prior examples:

int[] values = new int[20];

Scanner input = new Scanner(System.in);

creating and using objects1
Creating and Using Objects

Throttle control = new Throttle(100);

}

}

Declares new variable of type Throttle

Creates new Throttle object

Initializes Throttle size to 100

creating and using objects2
Creating and Using Objects

Throttle control = new Throttle();

Initializes Throttle size to default value

creating and using objects3

Object name

Period operator

Method name

Method parameters

Creating and Using Objects

Throttle control = new Throttle();

.

.

.

control.shift(3);

Activates shift( ) method for object control

slide31
final int SIZE = 8;

final int SPOT = 3;

Throttle small = new Throttle(SIZE);

small.shift(SPOT);

System.out.print(“Small throttle position = “);

System.out.println(SPOT + “ out of “ + SIZE + “.”);

System.out.println(“The flow is now: “ + small.getFlow());

slide32

tiny

huge

Throttle

Throttle

top

position

4

top

position

10000

0

0

Throttle tiny = new Throttle(4);

Throttle huge = new Throttle(10000);

Each object has its own copy of the instance variables

null references
Null References

Throttle control;

.

.

.

control = new Throttle(100);

}

What is the state of control during this time?

null references1
Null References

Throttle control = null;

.

.

.

control = new Throttle(100);

null references2
Null References

Throttle control;

.

.

.

control = new Throttle(100);

.

.

.

control = null; // no longer needed

assignments with reference variables
Assignments with Reference Variables

Throttle t1;

Throttle t2;

t1 = new Throttle(100);

t1.shift(25);

t2 = t1;

t1

t2

t2.shift(-5);

?

?

Throttle

top

position

100

25

20

assignments with reference variables1
Assignments with Reference Variables

Throttle t1;

Throttle t2;

t1 = new Throttle(100);

t1.shift(25);

t2 = new Throttle(100);

t2.shift(25);

t1

t2

Throttle

Throttle

top

position

100

top

position

100

25

25

t1 t2
(t1 == t2) ?

TRUE

Throttle t1;

Throttle t2;

t1 = new Throttle(100);

t1.shift(25);

t2 = t1;

t1

t2

Throttle

top

position

100

25

t1 t21
(t1 == t2) ?

FALSE

Throttle t1;

Throttle t2;

t1 = new Throttle(100);

t1.shift(25);

t2 = new Throttle(100);

t2.shift(25);

t1

t2

Throttle

Throttle

top

position

100

top

position

100

25

25