Chapter 9: Objects and Classes Flashcards
What is an object and what is its function?
An object represents something that exists in real life inside the program. The state of an object is defined by its data fields (A circle is defined by its radius). The behavior of the object is defined by methods.
What is a class and what is its function?
A class is a design plan for an object. A class can declare what data fields and methods an object has. An object is an instance of a class.
How do classes and objects connect?
An object is an instance of a class. A class is a definition and an object is one example that fits inside that definition. The relationship is analogous. A class would be an apple-pie recipe, while each individual apple pie is one object.
How do you use a constructor(s)?
A constructor is what is used to create (construct haha) a new instance of an object. They are designed to initialize, such as initializing the data fields. They must have the same name as the class, have no return type, and use the “new” operator.
What is the difference between instance and static?
An instance refers to a single object within a class that can have a unique value. A static is a variable shared by all objects within a class and can’t access instance members. A final static is a variable that can’t be changed such as the value of PI (3.14….)
What is a reference type and how does it relate to classes?
A class is a reference type. A variable of the class type can reference an instance (object) of the class.
What is an object reference variable?
Strictly speaking, an object reference variable and an object are different, but most of the time the distinction can be ignored. Therefore, it is fine, for simplicity, to say that myCircle is a Circle object rather than use the long-winded description that myCircle is a variable that contains a reference to a Circle object.
How do you access an Object’s Data and Methods?
Using the dot(.) operator
objectRefVar.dataField = myCricle.radius
objectRefVar.method(arguments) = myCricle.getArea()
What is the null value?
If a data field of a reference type does not reference any object, the data field holds a special Java value, null. null is a literal just like true and false. While true and false are Boolean literals, null is a literal for a reference type
What is data field encapsulation?
Declaring data fields private in order to protect those data fields from bugs and accidental changes.
What is data field encapsulation?
Declaring data fields private in order to protect those data fields from bugs and accidental changes.
SIDE NOTE:
From now on, all data fields should be declared private, and all constructors and methods should be defined public, unless specified otherwise.
What is an immutable object and class?
An instance (object) of a class that once created, can’t be changed. For a class to be immutable it must have all private data fields, no mutator methods, and No accessor methods can return a reference to a data field that is mutable.