390 likes | 414 Views
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
E N D
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) • A new type • Contains some form of data representation • Provides operations for manipulating the data
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 • 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 • 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
- 6 • 5 • 4 • 3 • 2 • 1 • Off getFlow isOn shift shutOff Example - Throttle Throttle Top 6 Hidden Position 4
- 6 • 5 • 4 • 3 • 2 • 1 • Off shift shutOff Example - Throttle Throttle Top 6 Position 4 getFlow isOn
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 public class <class-name> { <instance variables> <methods> }
Throttle Class public class Throttle { private int top; private int position; <methods> } Name begins with capital letter Instance variables
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 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
public Throttle(int size) { if (size <= 0) throw new IllegalArgumentException (“Size <= 0: “ + size); top = size; position = 0; }
Throttle Constructor public Throttle( ) Parameters: none Precondition: none Postcondition Throttle initialized with 5 on positions; initially off Throws: IllegalArgumentException
public Throttle( ) { top = 5; position = 0; }
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( ) public double getFlow( ) Get the current flow of this Throttle Returns: The current flow rate
public double getFlow( ) { return (double) position / (double) top; }
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( ) public boolean isOn( ) Check whether Throttle is on Returns If Throttle flow is above zero, returns true; otherwise returns false.
public boolean isOn( ) { return (position > 0); } Question: why doesn’t the programmer just check the value of position directly?
Throttle Modifier shutOff( ) public void shutOff( ) Turn off this Throttle Precondition: none Postcondition: the Throttle’s flow is shut off
public void shutOff( ) { position = 0; }
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.
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 public boolean isOn() { return (getFlow() > 0); } Could just reference position instance variable
Creating and Using Objects Prior examples: int[] values = new int[20]; Scanner input = new Scanner(System.in);
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 Objects Throttle control = new Throttle(); Initializes Throttle size to default value
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
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());
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 Throttle control; . . . control = new Throttle(100); } What is the state of control during this time?
Null References Throttle control = null; . . . control = new Throttle(100);
Null References Throttle control; . . . control = new Throttle(100); . . . control = null; // no longer needed
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 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) ? TRUE Throttle t1; Throttle t2; t1 = new Throttle(100); t1.shift(25); t2 = t1; t1 t2 Throttle top position 100 25
(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