CodeGym /Courses /Frontend SELF EN /Practical OOP Examples

Practical OOP Examples

Frontend SELF EN
Level 40 , Lesson 5
Available

12.1 User Management in a Web Application

Object-oriented programming (OOP) in JavaScript lets you create complex, scalable, and easily maintainable apps. Let's look at some practical examples of using OOP in JavaScript, including classes, inheritance, encapsulation, and polymorphism.

Imagine we're developing a user management web application. We can create classes for different user types, like User, Admin, and Guest, using inheritance to create specialized classes.

Explanation:

  • The User class contains methods login and logout
  • The Admin class inherits from User and adds the method deleteUser
  • The Guest class inherits from User and adds the method requestAccess
  • Instances of the Admin and Guest classes use their own class methods and those of the base class

Code:

JavaScript
    
      class User {
        constructor(username, email) {
          this.username = username;
          this.email = email;
        }

        login() {
          console.log(`${this.username} logged in.`);
        }

        logout() {
          console.log(`${this.username} logged out.`);
        }
      }

      class Admin extends User {
        constructor(username, email) {
          super(username, email);
          this.role = 'admin';
        }

        deleteUser(user) {
          console.log(`${this.username} deleted user ${user.username}.`);
        }
      }

      class Guest extends User {
        constructor(username, email) {
          super(username, email);
          this.role = 'guest';
        }

        requestAccess() {
          console.log(`${this.username} requested access.`);
        }
      }

      const admin = new Admin('adminUser', 'admin@example.com');
      const guest = new Guest('guestUser', 'guest@example.com');

      admin.login(); // "adminUser logged in."
      admin.deleteUser(guest); // "adminUser deleted user guestUser."
      admin.logout(); // "adminUser logged out."

      guest.login(); // "guestUser logged in."
      guest.requestAccess(); // "guestUser requested access."
      guest.logout(); // "guestUser logged out."
    
  

12.2 Product Management in an Online Store

In this example, we'll create classes to represent different types of products in an online store, like Product, Electronics, and Clothing. We'll also implement polymorphism for the calculateDiscount method.

Explanation:

  • The Product class contains a calculateDiscount method, which calculates a default 10% discount
  • The Electronics class overrides the calculateDiscount method, providing a 20% discount for electronics
  • The Clothing class overrides the calculateDiscount method, providing a 15% discount for clothing
  • Instances of the Electronics and Clothing classes use their own calculateDiscount methods to calculate discounts

Code:

JavaScript
    
      class Product {
        constructor(name, price) {
          this.name = name;
          this.price = price;
        }

        calculateDiscount() {
          return this.price * 0.1; // Default 10% discount
        }

        display() {
          console.log(`${this.name} - $${this.price.toFixed(2)}`);
        }
      }

      class Electronics extends Product {
        constructor(name, price, brand) {
          super(name, price);
          this.brand = brand;
        }

        calculateDiscount() {
          return this.price * 0.2; // 20% discount for electronics
        }
      }

      class Clothing extends Product {
        constructor(name, price, size) {
          super(name, price);
          this.size = size;
        }

        calculateDiscount() {
          return this.price * 0.15; // 15% discount for clothing
        }
      }

      const laptop = new Electronics('Laptop', 1000, 'BrandX');
      const tshirt = new Clothing('T-Shirt', 20, 'M');

      laptop.display(); // "Laptop - $1000.00"
      console.log(`Discount: $${laptop.calculateDiscount().toFixed(2)}`); // "Discount: $200.00"

      tshirt.display(); // "T-Shirt - $20.00"
      console.log(`Discount: $${tshirt.calculateDiscount().toFixed(2)}`); // "Discount: $3.00"
    
  

12.3 Library Management

In this example, we'll create classes for managing a library, including Book, Magazine, and Library classes. We'll also implement methods for adding and removing items from the library.

Explanation:

  • The Book and Magazine classes inherit from the LibraryItem class and override the display method
  • The Library class manages a collection of library items, providing addItem, removeItem, and displayItems methods
  • Instances of the Book and Magazine classes are added and removed from the library, and their information is displayed using the display method

Code:

JavaScript
    
      class LibraryItem {
        constructor(title, year) {
          this.title = title;
          this.year = year;
        }

        display() {
          console.log(`${this.title} (${this.year})`);
        }
      }

      class Book extends LibraryItem {
        constructor(title, year, author) {
          super(title, year);
          this.author = author;
        }

        display() {
          console.log(`${this.title} by ${this.author} (${this.year})`);
        }
      }

      class Magazine extends LibraryItem {
        constructor(title, year, issueNumber) {
          super(title, year);
          this.issueNumber = issueNumber;
        }

        display() {
          console.log(`${this.title} - Issue ${this.issueNumber} (${this.year})`);
        }
      }

      class Library {
        constructor() {
          this.items = [];
        }

        addItem(item) {
          this.items.push(item);
          console.log(`Added: ${item.title}`);
        }

        removeItem(title) {
          this.items = this.items.filter(item => item.title !== title);
          console.log(`Removed: ${title}`);
        }

        displayItems() {
          this.items.forEach(item => item.display());
        }
      }

      const library = new Library();

      const book = new Book('JavaScript: The Good Parts', 2008, 'Douglas Crockford');
      const magazine = new Magazine('JavaScript Weekly', 2021, 450);

      library.addItem(book); // "Added: JavaScript: The Good Parts"
      library.addItem(magazine); // "Added: JavaScript Weekly"

      library.displayItems();
      // "JavaScript: The Good Parts by Douglas Crockford (2008)"
      // "JavaScript Weekly - Issue 450 (2021)"

      library.removeItem('JavaScript Weekly'); // "Removed: JavaScript Weekly"
      library.displayItems(); // "JavaScript: The Good Parts by Douglas Crockford (2008)"
    
  
1
Task
Frontend SELF EN, level 40, lesson 5
Locked
User Classes
User Classes
1
Task
Frontend SELF EN, level 40, lesson 5
Locked
Library Classes
Library Classes
1
Survey/quiz
Inheritance and Polymorphism, level 40, lesson 5
Unavailable
Inheritance and Polymorphism
Inheritance and Polymorphism
Comments (1)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Mirko Mirxas Level 2
26 February 2026
Just copy and paste the coding example in the lecture into the challenge, then you are done :) Another point is that the quiz is quite low quality. Poorly worded questions, no coding questions, etc