CSC 142/Chapter 8
From charlesreid1
Chapter 8: Classes
Sections:
8.1 Object Oriented Programming
8.2 Object state/behavior
8.3 Object initialization: constructors
8.4 Encapsulation
8.5 Case study: Stock class
Chapters 1-7 focused on procedural programming. Now, we'll dive into classes, core concept in Java.
Start with terminology - like prior chapter covering objects, lots of terminology.
Discuss abstraction and encapsulation.
Discuss ideas for design of new classes, and programs to use them.
Section 8.1: Object Oriented Programming
Definitions:
- Object oriented programming
- Object
- State
- Behavior
- Client
- Driver
Material
Core concept:
- state and behavior
- state: values (internal data) stored by oejct
- behavior: set of actions
- Ultimately, it is iall just data - numbers, strings, other objects, but methods are also like data - a set of instructions, a recipe
Driver program:
- The program that sets things in motion (confusing because this is also an object in Java, but think of it as the non-object object
- Objects != programs: objects == components of programs
Encapsulation
- Represneting details
- Taking care of details
- Bank analogy again - bank acct object defines some data, like name and addr, balance, acct no., some can be changed, some cannot
Classes:
- Classes: template, definition, blueprint
- Objects: actual implementation, actual instance
Class:
- object
- object
- object
house blueprint
- house
- house
- house
Point objects: (x,y)
- Provied by awt
- has methods defined
- Usage of an object, understanding how to call its methods
- History reference: DOS (cmd line) --> Mac (GUI)
- Verb noun --> noun verb
Section 8.2: State and Behavior
Definitions
Definitions:
- Mutator
- Accessor
Material
Working through construction of Point class
Components:
- Fields (data stored in each object)
- Methods (behavior)
- Constructor (init code)
- Encapsulation (data protection)
Object state: fields
- x and y are two Point fields
- Separate init pair x and y for each instance (blueprint)
- Creation of client code to create 2 points, compute distance
Object behavior: methods
- Behavior --> instance methods
- Translating point: example of abstraction
- Just translate the point - we don't care how you do it!
- Alt is pkublic static method, lots of detail exposed, not optimal
- IF we change point class to be 3D, now we have to change the class, and change stuff that is outside the class
Implicit parameter:
- Code for an instance method has an implied knowledge of the object it operates on (ITSELF)
- Implicit parameter - the object that is referenced during an instance method call
- Python: we have to say self.x, self.y
- Java we say this.x, this.y, but we can also just say x, y
Methods for accessing changing data:
- Objects, Java in general, obsessed with concept of data protection
- Strange fixation, at beginning
- Let's explore this.
- Bank acct example: code needs to protect data, provide accessors to appropriate code, not unfettered access to all data
- Healthcare record system in Java: served to large hospital network, access control REALLY important
To deal with data protections:
- All data is private by default
- Accessor: getParam() method
- Mutator: setParam() method
- Le the object change the data itself, provide a high-level method for accessing that functionality
toString() method:
- Strings are the central object in Java
- Concatenation works with all objects and primitive types
- Printing objects by default prints address in memory
- Can replace this default behavior by overriding toString() method
Section 8.3: Object Init and Constructors
Definitions
Definitions:
- this
Material
Objects are collections of data
How do we initialize that data?
Don't always want user to initialize (e.g., initialize a new bank account with 1E16 dollars).
Flags
| CSC 142 - Intro to Programming I Computer Science 142 - Intro to Programming I, South Seattle College.
Chapter 1: Intro to Java CSC 142/Chapter 1 Chapter 2: Primitive Data and Definite Loops CSC 142/Chapter 2 Chapter 3: Parameters and Objects CSC 142/Chapter 3 Chapter 4: Conditional Execution CSC 142/Chapter 4 Chapter 5: Program Logic and Indefinite Loops CSC 142/Chapter 5 Chapter 6: File Processing CSC 142/Chapter 6 Chapter 7: Arrays CSC 142/Chapter 7 Chapter 8: Classes CSC 142/Chapter 8
Puzzles: Puzzles
Category:Teaching · Category:CSC 142 · Category:CSC Related: CSC 143 Flags · Template:CSC142Flag · e |