|
Object Oriented Programming
- Object-Oriented Design
- recognize "objects" or "concepts" in the problem
- recognize properties/features/states (-> member variables or data members) and
behaviors/actions (-> member methods) of each object
- figure out the types for each variable
- Class composition
- data members (member variables)
- constructors
- accessor methods
- selectors: getters, get-methods
- mutators: setters, set-methods
- other methods
in ClassName.java file
public class ClassName {
// member variables/properties/features/states/fields
// in general, these SHOULD be declared private
// as discussed in lecture
// this is to promote encapsulation
// o bundling of data and methods that operate on the data in a single unit;
// o allow others to access our variables ONLY through the methods we provide
private .......
private .......
// constructors
//
// multiple constructors can exist in the same class
// as long as the parameter lists are different
// - in the # of parameters, types or order of the parameters
public ClassName()
{
}
public ClassName(....)
{
}
// selectors: get____ methods
// aka getters, get methods
// in general, you may provide a get method for each variable
public ______ get____() { .... }
public ______ get____() { .... }
// mutators: set___ methods
// aka setters, set methods, modifiers
// in general, for each variable you may provide a set method
// this mechanism allows a "controlled" way to modify values of
// the private variables
public void set____(________) { .... }
public void set____(________) { .... }
// other operators
}
- Access Modifiers (basic, for now)
- public: available to all
- private: available to none, only within class
- Constructor
- same name as class itself
- no return type
- intializes member data variables
- can have several constructors: constructor overloading
as long as the parameter list is different - different number, types, order of parameters, etc.
- can be called (invoked) only in two places
- after the new keyword -> instantiates/creates an instance (or object) of this class
- from the constructors of subclasses (child classes - inheritance later)
- can call other constructors by calling this(.....)
- Class vs. Object (or instance)
- class: cookiecutter, template, blueprint
- object: a cookie, an instance of a particular class
- analogy: Firefox is a browser program (class), when you double-click and run Firefox, there is a Firefox window that starts running (object/instance). You can run several Firefox windows (objects/instances).
- Identity vs. Equality
- identity: same object (== for testing)
- equality: equal contents or equal based on some or all properties (obj1.equals(obj2) for testing)
- Example: FishBowl with 2 Fish, Aquarium with many Fish
- what are the objects in this program?
- what are the necessary properties/features and behaviors of each of the objects?
- keyword: static
- static variables/methods belong to the class as a whole and are shared among all objects/instances of the class
- static method: if you don't access any member variables and only use the parameters in the method body, this method should be static. There is no reason for the objects of this class to have independent implementation of this method.
- static means class-level
- non-static variable: any variable that uniquely belongs to an object should be non-static.
- non-static means instance-level (instance methods, instance variables)
- keyword: this
- refers to the current (this) object
- this.myVar: always refers to member variable named myVar
- this(...): can be called at the top of another constructor to call an existing constructor.
|