Open geekelo opened 5 months ago
Data Abstraction:
Definition: Data abstraction is the process of simplifying complex systems by presenting only the essential details and hiding the unnecessary complexity from the user. In programming, it involves exposing only the relevant aspects of an object and hiding the implementation details.
Usage:
Example in JavaScript (Without React):
// Simple JavaScript Example
class Shape {
constructor() {
if (this.constructor === Shape) {
throw new Error("Abstract classes can't be instantiated.");
}
}
getArea() {
throw new Error("Method 'getArea()' must be implemented in derived classes.");
}
}
class Circle extends Shape {
constructor(radius) {
super();
this.radius = radius;
}
getArea() {
return Math.PI * Math.pow(this.radius, 2);
}
}
const circle = new Circle(5);
console.log(circle.getArea()); // Accessing abstracted method without worrying about the implementation details.
In this example, Shape
is an abstract class with an abstract method getArea()
. The Circle
class extends Shape
and provides a concrete implementation for getArea()
. Users can interact with the abstracted getArea()
method without needing to understand how it's implemented in different shapes.
Example in React:
// Simple React Example
import React, { useState } from 'react';
const Counter = () => {
const [count, setCount] = useState(0);
const increment = () => {
setCount(count + 1);
};
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
};
// Usage:
// Users interact with the abstracted 'count' state without knowing the details of how state is managed.
ReactDOM.render(<Counter />, document.getElementById('root'));
In this React example, the Counter
component uses the useState
hook to manage the abstracted count
state. Users interact with the component by clicking the "Increment" button without needing to know the details of how state management is implemented internally.
Polymorphism:
Definition: Polymorphism, in simple terms, allows a single interface or method to be used for different types of data or objects. It enables flexibility in how different objects can respond to the same method or interface.
Usage:
Example in JavaScript:
// Polymorphism in JavaScript
class Animal {
makeSound() {
console.log('Some generic sound');
}
}
class Dog extends Animal {
makeSound() {
console.log('Woof!');
}
}
class Cat extends Animal {
makeSound() {
console.log('Meow!');
}
}
function animalSound(animal) {
animal.makeSound();
}
const genericAnimal = new Animal();
const myDog = new Dog();
const myCat = new Cat();
animalSound(genericAnimal); // Output: Some generic sound
animalSound(myDog); // Output: Woof!
animalSound(myCat); // Output: Meow!
In this example, Animal
is a base class with a makeSound
method. The Dog
and Cat
classes extend Animal
and provide their own implementations of makeSound
. The animalSound
function takes any object of type Animal
and calls its makeSound
method. This showcases polymorphism, as the same method (makeSound
) is used with different types of objects (Animal
, Dog
, Cat
), and each object responds in its own way.
Example in React:
// Polymorphism in React with Components
class Button extends React.Component {
render() {
return <button>{this.props.label}</button>;
}
}
class Link extends React.Component {
render() {
return <a href={this.props.url}>{this.props.label}</a>;
}
}
function renderElement(element) {
return element.render();
}
const buttonElement = new Button({ label: 'Click me!' });
const linkElement = new Link({ url: 'https://example.com', label: 'Visit website' });
console.log(renderElement(buttonElement)); // Renders a button
console.log(renderElement(linkElement)); // Renders a link
In this React example, the Button
and Link
components both have a render
method, and the renderElement
function can take any object with a render
method and render it. This demonstrates polymorphism in React, as different types of components can be rendered using a common interface (render
method).
Inheritance:
Definition: Inheritance is a mechanism in object-oriented programming where a new class (subclass or child class) inherits properties and behaviors from an existing class (superclass or parent class). It allows a class to reuse and extend the functionality of another class, promoting code reuse and hierarchy.
Usage:
Example in JavaScript:
// Simple JavaScript Example
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a sound`);
}
}
class Dog extends Animal {
constructor(name, breed) {
super(name);
this.breed = breed;
}
bark() {
console.log(`${this.name} barks loudly`);
}
}
const myDog = new Dog('Buddy', 'Golden Retriever');
myDog.speak(); // Output: Buddy makes a sound
myDog.bark(); // Output: Buddy barks loudly
In this example, Animal
is the superclass, and Dog
is the subclass that inherits from Animal
. The Dog
class inherits the name
attribute and the speak
method from the Animal
class, and it adds a new method bark
. Instances of Dog
can both speak and bark.
Example in React:
// Simple React Example
class AnimalComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
name: props.name,
};
}
render() {
return <p>{this.state.name} makes a sound</p>;
}
}
class DogComponent extends AnimalComponent {
bark() {
return <p>{this.state.name} barks loudly</p>;
}
}
const myDogComponent = <DogComponent name="Buddy" />;
In this React example, AnimalComponent
is the base component, and DogComponent
is a specialized component that inherits from AnimalComponent
. The DogComponent
adds a new method bark
to the existing functionality of AnimalComponent
. Instances of DogComponent
can render both the generic sound and the specific bark.
Encapsulation:
Definition: Encapsulation is the bundling of data and the methods that operate on that data within a single unit, known as a class in object-oriented programming. It restricts direct access to some of the object's components, promoting modularity and information hiding.
Usage:
Example:
In this example, the
Car
class encapsulates themake
attribute and a private attribute_model
. Direct access to_model
is restricted, and the full model information is accessed through thegetFullModel
method, demonstrating encapsulation.REACT
In this example, the Car class encapsulates the make attribute and a private attribute _model. Direct access to _model is restricted, and the full model information is accessed through the getFullModel method, demonstrating encapsulation.