Conteúdo do curso
JavaScript Course for Beginners: Fundamentals and Practice
Sobre a Aula

Basic Concepts of Object-Oriented Programming

Welcome to Module 6! In this journey, you will explore the fascinating universe of Object-Oriented Programming (OOP), a paradigm that allows you to organize and structure your programs more efficiently, modularly, and with greater reusability.

Get ready to learn the basic concepts of OOP and how to apply them in your projects, elevating your proficiency as a developer.

Object-oriented programming is like building a house with Lego pieces.

Each Lego piece is like a class, defining a type of object with its characteristics (attributes) and actions (methods).

For example, we can have a Car class with attributes such as color, model, and speed, and methods like accelerate and brake.

From these classes, we can create specific objects (instances), just as we can assemble different car models with Lego pieces.

Each object has its own characteristics and can perform the methods defined in the class.

An important concept is inheritance, which is like a family of related cars.

For example, we can have a SportsCar class that inherits characteristics from the Car class but also has specific features, such as the maximum speed.

Another concept is polymorphism, which is like different types of cars responding to the same action differently.

For instance, both a sports car and a family car can have an accelerate method, but each will have a specific behavior.

Abstraction is like creating a simplified representation of cars, focusing only on important details.

For example, we can create an abstract class Vehicle that defines common characteristics for all vehicles, such as the number of wheels, and let more specific classes, like Car and Motorcycle, detail their peculiarities.

Let’s put this explanation into code to enhance understanding:

// Defining the abstract class Vehicle
class Vehicle {
  constructor(numberOfWheels) {
    this.numberOfWheels = numberOfWheels;
  }

  accelerate() {
    console.log('Accelerating the vehicle.');
  }

  brake() {
    console.log('Braking the vehicle.');
  }
}

// Defining the Car class that inherits from Vehicle
class Car extends Vehicle {
  constructor(color, model, speed) {
    super(4); // Cars have four wheels
    this.color = color;
    this.model = model;
    this.speed = speed;
  }

  // Specific method for Car
  openDoor() {
    console.log('Opening the car door.');
  }
}

// Defining the SportsCar class that inherits from Car
class SportsCar extends Car {
  constructor(color, model, maxSpeed) {
    super(color, model, 0); // Initial speed is zero
    this.maxSpeed = maxSpeed;
  }

  // Overriding the accelerate method for a specific behavior
  accelerate() {
    console.log('Accelerating the sports car. Maximum speed: ' + this.maxSpeed);
  }
}

// Defining the Motorcycle class that inherits from Vehicle
class Motorcycle extends Vehicle {
  constructor(color, model) {
    super(2); // Motorcycles have two wheels
    this.color = color;
    this.model = model;
  }

  // Overriding the brake method for a specific behavior
  brake() {
    console.log('Braking the motorcycle. Be careful with balance!');
  }
}

// Creating instances and demonstrating the concepts
const sportsCar = new SportsCar('Red', 'Sports', 250);
const motorcycle = new Motorcycle('Black', 'Custom');

sportsCar.accelerate();
sportsCar.openDoor();

motorcycle.accelerate();
motorcycle.brake();

In this example, the Vehicle class is the abstract class containing common characteristics for all vehicles. The Car and Motorcycle classes inherit from Vehicle, demonstrating inheritance.

The SportsCar class inherits from Car and overrides the accelerate method, exemplifying polymorphism. The code also shows abstraction by focusing on essential characteristics for each class.

Challenge:

  • Create a JavaScript script that defines a Person class with properties name and age.
  • Create a present method that displays the person’s name and age.
  • Create a person object from the Person class and use the present method.

In summary, object-oriented programming allows you to organize and reuse code in a modular way, much like building different car models with Lego pieces.

Remember:

OOP is a fundamental paradigm for developing large-scale software.
Basic OOP concepts will guide you in creating more organized, efficient, and reusable programs.
Start with simple examples and practice creating classes and objects to master OOP.

In the next lesson, we’ll detail each of these concepts to reinforce your foundation. Are you ready? Let’s go!

Entrar na conversa
Rolar para cima