- A class is the blueprint; an object is one instance built from it.
🎯 How this can come up · 1 question
A class is best described as:
Correct: A blueprint for creating objects.
- Attributes (instance variables) hold an object’s state; methods are its behaviour.
- Constructor: same name as the class, NO return type (not even void), runs when you call new.
🎯 How this can come up · 1 question
Which is true of a constructor?
Correct: It has the same name as the class and no return type.
- If you write no constructor, Java supplies a no-argument default constructor.
- Constructor overloading: several constructors with different parameter lists.
- Make attributes private (data hiding) and expose them through public methods.
- Data hiding lets setters VALIDATE values (e.g., reject a negative age).
🎯 How this can come up · 1 question
Why should instance variables be private?
So other classes can only change them through methods, which can validate the values (data validation) and hide the implementation; the class can then change internally without breaking its users.
- Encapsulation: hide the details, show only the interface (like a car’s pedals).
- Recipe: private instance variables + public accessors (getters) and mutators (setters).
- this = the current object. Use it when a parameter has the same name as a field: this.name = name; and to call another constructor: this(...).
🎯 How this can come up · 1 question
In a constructor with parameter name, which assigns the field?
Correct: this.name = name;.
- static = one shared copy for the whole class; final = constant.
- A static variable (e.g., count of Students) is shared by all objects; static final = constant.
🎯 How this can come up · 1 question
A counter of how many Student objects have been created should be:
Correct: A static variable.
- Static methods belong to the class, not an object (Math.max). They can’t use this or instance variables directly.
🎯 How this can come up · 1 question
A static method can directly access instance variables.
False. It has no object (no this).
- toString() returns a String describing the object; println(obj) calls it automatically.
- Object.equals compares REFERENCES; override it to compare contents (e.g., same ID).
- Copy constructor: Student(Student s) builds a separate object with the same values.
- Composition = has-a: a class holds an object of another class (Student has a Date).
🎯 How this can come up · 1 question
A Student class with a Date birthDate field is an example of:
Correct: Composition (has-a).
No slides match this filter.