What are the 3 main characteristics of an object?
State (attributes), Behavior (methods), Identity.
What is a class in Java?
A blueprint for creating objects, defining fields and methods.
What is the relationship between a class and an object?
An object is an instance of a class.
Give two attributes and one method of a Circle class.
Attributes: radius, x; Method: area()
What are instance variables?
Fields that store object-specific data.
Why are methods used in a class?
To define actions/behaviors that objects can perform.
What is a constructor in Java?
A special method to initialize an object, named after the class.
How do you instantiate an object in Java?
ClassName obj = new ClassName();
What is the role of the new keyword?
Allocates memory and calls the constructor.
How do you call a method on an object?
objectName.methodName()
What is garbage collection in Java?
Automatic removal of unreferenced objects from memory.
What does the private and public keyword do in Java?
What is encapsulation in Java?
The practice of keeping fields private and accessing them through public methods.
Why is encapsulation important?
t hides internal details, protects data, and allows control over how data is accessed or modified.
What is a getter method?
A public method that returns the value of a private field.
What is a setter method?
A public method that updates the value of a private field.
Write a simple getter for a private name field.
public String getName() {
return name;
}
What are the benefits of using getters and setters?
They provide controlled access, validation, and flexibility to change implementation.