Exam 1 Study Guide Flashcards
What is a constructor?
A special method in Java that is used to initialize objects when they are created. It is automatically called when an object is instantiated using the ‘new’ keyword.
Example:
class Car {
Car() { // Constructor has the same name as the class
System.out.println(“Car object created!”);
}
}
What is a Parameterized Constructor?
A constructor that accepts arguments.
Example:
class Car{
private String name;
public Car(String name);
this.name = name;
Copy Constructor
A constructor that takes another object as the argument. Basically it copies.
What is a Singleton?
A creational design pattern that ensures that only one instance of its kind exists. It provides a single point of access to it.
- Restricts Multiple Instances
- Global Access Point
- Lazy or Eager Initialization
When is a Singleton useful?
-Database connections
-Logging systems
-Configuration settings
-Thread pools
Downsides of Singleton
- Tight Coupling
- Hidden Dependencies
- Difficult to debug
Benefits of Singleton
- Control Over Instance Creation
- Memory Efficiency
- Thread Safety
What are access modifiers in Java?
Access modifiers control the visibility and accessibility of classes, methods, and variables. They define which other classes can interact with a given field or method.
Scope of ‘public’ access modifier
Everywhere. Inside and outside the package
Scope of ‘Protected’ access modifier
Same package + subclasses
Scope of ‘private’ access modifier
Only within the same class
Scope of the default access modifier (no access modifier)
Only within the same package.
What is the purpose of the ‘final’ modifier?
The ‘final’ modifier restricts modifications to variables, methods, and classes
Example of ‘final’ modifier in variables
class Example {
public static void main(String[] args) {
final int number = 10;
number = 20; // ❌ ERROR: Cannot reassign a final variable
}
}
‘final’ modifier with methods
class Parent {
final void show() {
System.out.println(“This is a final method.”);
}
}
class Child extends Parent {
void show() { // ❌ ERROR: Cannot override a final method
System.out.println(“Overriding attempt!”);
}
}
‘final’ modifier with classes
final class Animal {
void makeSound() {
System.out.println(“Animal makes sound”);
}
}
class Dog extends Animal { // ❌ ERROR: Cannot extend a final class
}
What is the purpose of the ‘static’ modifier
‘static’ keyword is used to define class-level members, meaning they belong to the class itself rather than an instance. A static variable is shared across all instances of the class.
‘static’ with variables
‘static’ varibales are shared across all instances of the class:
class Example {
static int count = 0; // Shared among all instances
Example() { count++; // Increments for each new object } public static void main(String[] args) { Example obj1 = new Example(); Example obj2 = new Example(); System.out.println(Example.count); // Output: 2 } }
‘static’ with Methods
‘static’ method belongs to the class and can be called without creating an object.
class Utility {
static void greet() {
System.out.println(“Hello, World!”);
}
}
public class Test {
public static void main(String[] args) {
Utility.greet(); // ✅ No need to create an object
}
}
What is the strategy design pattern?
A behavioral design pattern that allows you to define a family of algorithms and select them at run time without modifying the client code.
Benefits of the strategy design pattern?
Helps achieve loose coupling and follows the open-closed principle (modify behavior without modifying existing code)
Also avoids many if-else or switch statements.
You want to switch algorithms at runtime.
Scenario when the Strategy design pattern would be useful?
An e-commerce application where users can pay using Credit Card, Paypal, or Bitcoin. The Strategy design pattern allows us to switch between these payment methods dynamically.
How does the Strategy Pattern Improve Code Flexibility and Maintainability?
*Encapsulation of Behavior - Logic is in separate strategy classes, making it easier to manage.
*Supports Open-Closed Principle (OCP) - New methods can be added without modifying existing code.
*Eliminates Complex Conditionals - No need for multiple if-else or switch statements
*Increases Reusability - Each strategy can be reused independently
*Enhances Testability - Individual strategies can be tested separately without affecting the main method
What job would the Strategy Pattern be good for?
When you need to perform the same job but with different approaches (or inputs). Instead of hardcoding multiple behaviors into a single class, you encapsulate them into separate strategy classes, making it easier to switch, extend and maintain different implementations dynamically.