Unit 2: Object Orientated Programming Flashcards
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?
- private: It restricts access to a variable or method to within the same class only.
- public: It allows access to a method or variable from any other class.
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.