CSCI 161 - Lecture 13 - Object-Oriented Programming (OOP) Basics
Instructor's Class Notes
- OOP Terms
- Object-Oriented Programming - A reasoning about a program as a set of objects rather than a set of actions.
"Reasoning" in this context
refers to many aspects of programming: design, construction, the view of the overall problem and its component solution, ...
- Procedural Programming - Is the oldest style of programming and what you have done so far CSCI-161. OOP is in stark contrast to procedural programming. Procedural programming is "verb noun" while OOP is "noun verb".
- Object - A programming entity that contains state (data) and behavior (methods).
- State - A set of values (internal data) stored in an object.
- Behavior - A set pf actions an object can perform, often reporting or modifying its internal state.
- Client (or Client Code) - Code that interacts with the class or objects of that class (aka calling class, calling code, or caller).
For example, your programs have been client code that use and call the Scanner class.
- Object State and Behavior
- Classes - Used already; something new:
- Your program had a class, the program class (same name as the file name).
- You have worked with several classes: Scanner, File, String, Integer, Double,...
- Can also define and use your own classes as blueprints for new types of objects in your program.
- Objects - A class in your program provides a blueprint for how objects created from that class will
act, behave, etc since each class defines the following about its objects:
- The state stored in each object.
- Internally in private variables called fields.
- The behavior each object can perform
- How to construct objects of that type
- Object Behavior: Methods - Several types of methods
- Instance Method - A method inside an object that operates on that object.
Examples: String class has .length() method; Scanner class has .hasNext() method.
- Implicit Parameter - The object itself is an implicit parameter to all Instance methods, meaning
that the state (data within) is always available within instance methods.
- Mutator - An instance method that modifies the object's internal state.
- Accessor - An instance method that provides information about the state of an object without
modifying it.
- Object Initialization: Constructors
- Constructor - The method of the same name as the Class that is used to "Construct" the
object from the class.
- A class can have many constructors, all having the same name as the class, but with
different parameters.
- A common use of a constructor is to allow the caller to specify initializing values
for state (field) variables of the class (as in supplying X and Y for the Point class).
// Without a constructor
Point p1 = new Point();
p1.x = 7;
p1.y = 2;
// With a constructor
Point p1 = new Point(7, 2);
- The default constructor is inserted by the JVM if no other constructors are
defined in your classes code.
- It is a good practice to always include a constructor, even if one that accepts
no parameters and initializes state (field) variables to specific, known values.
- Warning: - Be careful NOT to include void as the return data type of
a constructor. Constructors have no return type and including void, out of habit, will
indicate to Java that the method is "just anothe method" and not a constructor.
- this - The keyword this refers to the object derived from the class and is
a convenient way within constructors and methods to reference (and to be sure your program
references) class state variables, methods, etc.