These are the four pillars you must know — and be able to give JavaScript / React examples for.
Definition: Hiding implementation details and exposing only what's necessary through a public API.
In JavaScript: Achieved with closures, classes, or private fields (#property).
In React: Component state is "encapsulated" — it's not directly accessible from outside the component.
Example:
function Counter() {
const [count, setCount] = React.useState(0); // Encapsulated inside the component
return <button onClick={() => setCount(count + 1)}>{count}</button>;
}Definition: Hiding complex logic behind a simpler interface.
In JavaScript: Use functions, classes, or modules to expose only necessary methods.
In React: Custom hooks abstract reusable logic.
Example:
function useCounter(initialValue = 0) {
const [count, setCount] = React.useState(initialValue);
const increment = () => setCount((c) => c + 1);
return { count, increment };
}Now you can use useCounter() without caring how it's implemented.
Definition: Reusing code by creating child classes from parent classes.
In JavaScript: Achieved with class and extends.
In React:
- Class components can extend
React.ComponentorReact.PureComponent. - Modern React prefers composition over inheritance, but you should still know this.
class MyComponent extends React.Component {
render() {
return <h1>Hello {this.props.name}</h1>;
}
}Definition: Same interface, different implementation.
In JavaScript: Method overriding or implementing the same method in different classes.
In React: Passing different props or components to achieve different behavior.
function Button({ as: Component = "button", children, ...props }) {
return <Component {...props}>{children}</Component>;
}
// Usage:
<Button>Click Me</Button>
<Button as="a" href="#">Link Button</Button>Same Button interface, but different rendering behavior.
Since React uses JS, you should understand:
classsyntax and constructorthiskeyword binding issues in class components- Prototypes and prototype chain
- Static methods (
static myMethod() {}) for utility functionality
Example:
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a noise.`);
}
}
class Dog extends Animal {
speak() {
console.log(`${this.name} barks.`);
}
}Common questions you might get:
- Explain the four pillars of OOP with examples in React.
- Difference between composition and inheritance — why React prefers composition.
- How would you use OOP in a large-scale React project?
- How does encapsulation apply to state in React?
- When would you choose a class component over a functional component?
- State & props = Encapsulation
- Custom hooks / HOCs = Abstraction
- Class components = Inheritance example (via React.Component)
- Props-driven rendering = Polymorphism