Java Interview questions on OOP Concepts
Categories: OTHERS
Q.1. What is the difference between loose coupling and tight coupling?
Loose coupling allows components to interact with each other with minimal dependencies, while tight coupling creates strong dependencies between components.
Q.2. What is the difference between cohesion and coupling?
Cohesion refers to the degree to which elements within a module belong together, while coupling refers to the degree of interdependence between modules.
Q.3. What is Liskov Substitution principle? Can you explain with an example?
Liskov Substitution principle states that objects of a superclass should be replaceable with objects of its subclasses without affecting the correctness of the program.
For example, In a class hierarchy where "Shape" is the superclass and "Circle" and "Square" are the subclasses, every method that functions on Shape should also function correctly on Circle or Square.
Q.4. What is the difference between abstract class and interface in Java?
Abstract classes can have both abstract and concrete methods, while interfaces can only have abstract methods. Additionally, a class can implement multiple interfaces but can only extend one abstract class.
Q.5. What is the difference between composition, aggregation, and association?
Composition: Composition implies a strong ownership relationship where the lifetime of the contained object is dependent on the container.
Aggregation: Aggregation implies a weaker relationship where the contained object can exist independently of the container.
Association: Association implies a relationship between two classes without any ownership or lifecycle dependency.