0% found this document useful (0 votes)
2 views

OOP_Interview_Problems

The document discusses the application of object-oriented programming principles such as polymorphism, composition, inheritance, encapsulation, method overloading, method overriding, and abstraction in software design. It emphasizes how these principles can improve flexibility, maintainability, and security in systems like billing and service-based architectures. Key takeaways include using polymorphism for customer-specific discount logic, favoring composition for flexibility, and applying abstraction in API design to hide implementation details.

Uploaded by

pbecic
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

OOP_Interview_Problems

The document discusses the application of object-oriented programming principles such as polymorphism, composition, inheritance, encapsulation, method overloading, method overriding, and abstraction in software design. It emphasizes how these principles can improve flexibility, maintainability, and security in systems like billing and service-based architectures. Key takeaways include using polymorphism for customer-specific discount logic, favoring composition for flexibility, and applying abstraction in API design to hide implementation details.

Uploaded by

pbecic
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

OOP

How would you apply polymorphism to improve flexibility in a billing system


that supports multiple customer types?
 Polymorphism allows different classes to define their own behavior through a shared
interface.
 Use case: A billing system needs to apply different discount logic based on customer
type (e.g., Regular, VIP, Corporate).
 Implementation approach:
 Define a common interface or abstract class, e.g., Customer with a method
calculateDiscount().
 Each customer type implements its own discount calculation.
 The billing logic operates on the Customer abstraction, not specific subclasses.
 Benefits:
 Improved code flexibility — new customer types can be introduced without modifying
the billing logic.
 Enhances testability and reuse of common code.

What is the difference between composition and inheritance, and when would
you favor one over the other?
 Inheritance models 'is-a' relationships, while composition models 'has-a' relationships.
 Inheritance is tight coupling — changes in base class affect all subclasses.
 Composition is more flexible and promotes loose coupling.
 Preferred scenarios:
 Use composition when behaviors can be shared or changed at runtime (e.g., Strategy
pattern).
 Use inheritance for shared structure or when subclass truly is a specialized type of the
superclass.
 Example:
 Instead of class Car extends Vehicle, use Car has Engine, and inject different engine
types as needed.
 Modern design favors composition over inheritance for flexibility and adherence to
SOLID principles.

How would you explain encapsulation and its practical benefits in a service-
based architecture?
 Encapsulation is the principle of hiding internal details and exposing only what’s
necessary via a defined interface.
 In service-based architecture, encapsulation helps enforce boundaries between
components.
 Benefits:
 Reduces the chance of unintended usage of internal logic or data.
 Facilitates refactoring — changes in internal implementation don't affect consumers.
 Improves security and robustness by validating input/output through public interfaces.
 Example:
 A UserService only exposes methods like createUser() or findUserById(), but hides
internal database queries or caching strategies.

What is the difference between method overloading and method overriding in


Java?
 Overloading means defining multiple methods with the same name but different
parameters within the same class.
 Overriding means redefining a method of a superclass in its subclass with the same
method signature.
 Differences:
 Overloading is resolved at compile-time (static polymorphism).
 Overriding is resolved at runtime (dynamic polymorphism).
 Examples:
 Overloading: void print(int value) and void print(String value).
 Overriding: subclass redefines toString() method from Object class.
 Overriding enables polymorphism, allowing different behaviors for subclasses through
base class references.

How would you apply abstraction when designing APIs for external clients?
 Abstraction hides implementation details and shows only essential operations.
 When designing APIs:
 Expose only business-relevant operations (e.g., /createOrder, /getCustomerProfile).
 Hide database schema, internal IDs, or intermediate processing logic.
 Techniques to implement abstraction:
 Use DTOs to separate internal models from external representation.
 Apply versioning to control and evolve exposed interfaces without breaking clients.
 Benefits:
 Improved security, decoupling, and maintainability.
 Allows internal evolution without impacting consumers.

You might also like