21. What is a final class or method?
โข A final class can't be extended.
โข A final method can't be overridden.
Useful for security, immutability (e.g., String class in Java is final).
22. What is object cloning?
โข Creating an exact copy of an object.
โข In Java: use .clone() method from Cloneable interface.
โข Shallow vs Deep cloning:
โ Shallow copies references.
โ Deep copies full object graph.
23. What is a singleton class?
โข A class that allows only one instance.
โข Ensures shared resource (like a config manager or DB connection).
โข Common in design patterns.
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return instance;
}
}
24. What are access specifiers?
Control visibility of class members:
โข public โ accessible everywhere
โข private โ only inside the class
โข protected โ inside class subclasses
โข (default) โ same package
25. What is cohesion in OOP?
โข Degree to which class elements belong together.
โข High cohesion = focused responsibility โ better design.
26. What is coupling?
โข Dependency between classes.
โข Low coupling = better modularity, easier maintenance.
27. Difference between tight and loose coupling?
โข Tight coupling: classes are strongly dependent โ harder to modify/test.
โข Loose coupling: minimal dependency โ promotes reusability, flexibility.
28. What is composition vs aggregation?
โข Composition: "part-of" strong relationship โ child can't exist without parent.
Example: Engine in a Car
โข Aggregation: weak association โ child can exist independently.
Example: Student in a University
29. Difference between association, aggregation, and composition?
โข Association: General relationship
โข Aggregation: Whole-part, but loose
โข Composition: Whole-part, tightly bound
30. What is the open/closed principle?
โข From SOLID:
โSoftware entities should be open for extension, but closed for modification.โ
โข Means add new code via inheritance, not by changing existing logic.
๐ฌ Double Tap โฅ๏ธ for Part-3