Blog/Java/OOP Concepts

OOP Concepts

10 essential oop concepts interview questions

1. Encapsulation: bundling data and methods, hide internal details. 2. Inheritance: subclass inherits from superclass, code reuse. 3. Polymorphism: objects behave differently based on type. 4. Abstraction: hide complexity, expose only necessary interface. Together they promote code organization, reusability, maintainability.
Encapsulation: hiding internal implementation details, exposing through public interface. Use private fields, public getter/setter methods. Protects data integrity, enables validation. Prevents unauthorized access. Improves maintainability (can change internal without affecting users).
Inheritance: child class inherits parent class fields and methods. Promotes code reuse. Enables polymorphism. Establishes hierarchy. Single inheritance in Java (one parent). Method overriding: child provides custom implementation. Use extends keyword.
Is-a (inheritance): child IS-A type of parent. Example: Dog IS-A Animal. Use extends. Has-a (composition): object HAS-A component. Example: Car HAS-A Engine. Use field. Composition is more flexible and preferred (favor composition over inheritance).
Method overloading: same method name, different parameters (type or count). Example: add(int, int) and add(double, double). Compile-time polymorphism. Improves readability. Must differ in parameter signature (not just return type).
Method overriding: child class provides custom implementation of parent method. Runtime polymorphism. Use @Override annotation. Must have same signature. Enables subclass-specific behavior while maintaining parent interface.
Super: reference to parent class. super.method() calls parent method. super() calls parent constructor. Useful for accessing overridden methods, calling parent implementation, initializing parent state.
Final: cannot be changed (for variables, methods, classes). Static: belongs to class not instance, shared among all instances. Java has no const keyword (use final). Static variables initialized once, final variables initialized once but can be final instance variables.
Design patterns: reusable solutions to common problems. Creational: Singleton, Factory, Builder. Structural: Adapter, Decorator, Facade. Behavioral: Observer, Strategy, State. Patterns improve code organization, communication, reusability. Study Gang of Four patterns.
S: Single Responsibility (one reason to change). O: Open/Closed (extend, don't modify). L: Liskov Substitution (subtype substitutable). I: Interface Segregation (small interfaces). D: Dependency Inversion (depend on abstractions). SOLID promotes maintainable, flexible code.