1 / 39

Object Oriented Programming

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

boyce
Download Presentation

Object Oriented Programming

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Abstract Data Type (ADT) • A new type • Contains some form of data representation • Provides operations for manipulating the data

  3. 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

  4. 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

  5. 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. - 6 • 5 • 4 • 3 • 2 • 1 • Off getFlow isOn shift shutOff Example - Throttle Throttle Top 6 Hidden Position 4

  7. - 6 • 5 • 4 • 3 • 2 • 1 • Off shift shutOff Example - Throttle Throttle Top 6 Position 4 getFlow isOn

  8. 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

  9. Defining a New Class in Java public class <class-name> { <instance variables> <methods> }

  10. Throttle Class public class Throttle { private int top; private int position; <methods> } Name begins with capital letter Instance variables

  11. 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

  12. 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

  13. public Throttle(int size) { if (size <= 0) throw new IllegalArgumentException (“Size <= 0: “ + size); top = size; position = 0; }

  14. Throttle Constructor public Throttle( ) Parameters: none Precondition: none Postcondition Throttle initialized with 5 on positions; initially off Throws: IllegalArgumentException

  15. public Throttle( ) { top = 5; position = 0; }

  16. Methods • Accessor – gives information about an object without altering it • Modifier – changes the “status” of an object (generally by changing instance variables)

  17. Throttle Accessor getFlow( ) public double getFlow( ) Get the current flow of this Throttle Returns: The current flow rate

  18. public double getFlow( ) { return (double) position / (double) top; }

  19. 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)

  20. Throttle Accessor isOn( ) public boolean isOn( ) Check whether Throttle is on Returns If Throttle flow is above zero, returns true; otherwise returns false.

  21. public boolean isOn( ) { return (position > 0); } Question: why doesn’t the programmer just check the value of position directly?

  22. Throttle Modifier shutOff( ) public void shutOff( ) Turn off this Throttle Precondition: none Postcondition: the Throttle’s flow is shut off

  23. public void shutOff( ) { position = 0; }

  24. 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.

  25. 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; }

  26. Methods Activating (Calling) Methods public boolean isOn() { return (getFlow() > 0); } Could just reference position instance variable

  27. Creating and Using Objects Prior examples: int[] values = new int[20]; Scanner input = new Scanner(System.in);

  28. Creating and Using Objects Throttle control = new Throttle(100); } } Declares new variable of type Throttle Creates new Throttle object Initializes Throttle size to 100

  29. Creating and Using Objects Throttle control = new Throttle(); Initializes Throttle size to default value

  30. 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

  31. 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());

  32. 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

  33. Null References Throttle control; . . . control = new Throttle(100); } What is the state of control during this time?

  34. Null References Throttle control = null; . . . control = new Throttle(100);

  35. Null References Throttle control; . . . control = new Throttle(100); . . . control = null; // no longer needed

  36. 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

  37. 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

  38. (t1 == t2) ? TRUE Throttle t1; Throttle t2; t1 = new Throttle(100); t1.shift(25); t2 = t1; t1 t2 Throttle top position 100 25

  39. (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

More Related