JavaScript · Syntax · Beginner
OOP: Inheritance and Method Overriding
OOP problems in JavaScript on inheritance, super(), constructor, method overriding, toString(), custom class methods, and working with objects.
Quick topic start and explanations before exercises (exercises below):
toString() and custom methods
#Inheritance and method patterns
#Exercises:
Animal and Dog through inheritance.
#class Animal {
// your code here
}
class Dog extends Animal {
// your code here
}
Solution
class Animal {
constructor(name) {
this.name = name;
}
}
class Dog extends Animal {
toString() {
return `Dog: ${this.name}`;
}
}
Book with string representation.
#class Book {
// your code here
}
Solution
class Book {
constructor(title, pages) {
this.title = title;
this.pages = pages;
}
length() {
return this.pages;
}
toString() {
return `Book: ${this.title} (${this.pages} pages)`;
}
}
User and Admin.
#class User {
// your code here
}
class Admin extends User {
// your code here
}
Solution
class User {
constructor(name) {
this.name = name;
}
toString() {
return this.name;
}
}
class Admin extends User {
toString() {
return `Admin: ${this.name}`;
}
}
Box with items.
#class Box {
// your code here
}
Solution
class Box {
constructor(itemsCount) {
this.itemsCount = itemsCount;
}
add(count) {
if (count > 0) {
this.itemsCount += count;
}
}
remove(count) {
if (count > 0) {
this.itemsCount -= count;
if (this.itemsCount < 0) {
this.itemsCount = 0;
}
}
}
getCount() {
return this.itemsCount;
}
}
Message with spam flag.
#class Message {
// your code here
}
Solution
class Message {
constructor(text) {
this.text = text;
this.spam = null;
this.spamTriggers = ["buy", "expensive"];
}
isSpam() {
if (this.spam !== null) {
return this.spam;
}
for (let trigger of this.spamTriggers) {
if (this.text.toLowerCase().includes(trigger)) {
this.spam = true;
return true;
}
}
this.spam = false;
return false;
}
length() {
return this.text.length;
}
}
let m = new Message("Buying old TVs for a high price!!!");
console.log(m.isSpam());
console.log(m.length());
Employee and Manager.
#class Employee {
// your code here
}
class Manager extends Employee {
// your code here
}
Solution
class Employee {
constructor(name, salary) {
this.name = name;
this.salary = salary;
}
changeSalary(coef) {
this.salary = Math.round(this.salary * coef);
}
}
class Manager extends Employee {
toString() {
return `Manager ${this.name} earns ${this.salary}`;
}
}
let manager = new Manager("Bob", 1000);
manager.changeSalary(1.1);
console.log(manager.toString());
Playlist and CustomPlaylist.
#class Playlist {
// your code here
}
Solution
class Playlist {
constructor(songs) {
this.songs = songs;
}
add(song) {
if (!this.songs.includes(song)) {
this.songs.push(song);
}
}
remove(song) {
if (this.songs.includes(song)) {
this.songs.splice(this.songs.indexOf(song), 1);
}
}
reverse() {
this.songs.reverse();
}
length() {
return this.songs.length;
}
isEmpty() {
return this.songs.length === 0;
}
}
class CustomPlaylist extends Playlist {
add(song) {
this.songs.push(song);
}
}
Named CustomPlaylist.
#class CustomPlaylist extends Playlist {
constructor(songs, name) {
super(songs);
// your code here
}
add(song) {
this.songs.push(song);
}
}
Solution
class Playlist {
constructor(songs) {
this.songs = songs;
}
add(song) {
if (!this.songs.includes(song)) {
this.songs.push(song);
}
}
}
class CustomPlaylist extends Playlist {
constructor(songs, name) {
super(songs);
this.name = name;
}
add(song) {
this.songs.push(song);
}
toString() {
return `Playlist: ${this.name}`;
}
}
MagicBox with a random item.
#class Box {
constructor() {
this.items = [];
this.itemsCount = this.items.length;
}
add(item) {
this.items.push(item);
this.itemsCount = this.items.length;
}
remove(item) {
if (this.items.includes ? this.items.includes(item) : (item in this.items)) {
this.items.splice(this.items.indexOf(item), 1);
this.itemsCount = this.items.length;
} else {
console.log("The cat from the box says: That's not here, goodbye!");
}
}
length() {
return this.itemsCount;
}
}
let things = ["Hat", "Umbrella", "Mug"];
class MagicBox extends Box {
constructor() {
// call super() and add a random item
}
}
Solution
function choice(items) {
return items[Math.floor(Math.random() * items.length)];
}
class Box {
constructor() {
this.items = [];
this.itemsCount = this.items.length;
}
add(item) {
this.items.push(item);
this.itemsCount = this.items.length;
}
remove(item) {
if (this.items.includes(item)) {
this.items.splice(this.items.indexOf(item), 1);
this.itemsCount = this.items.length;
} else {
console.log("The cat from the box says: That's not here, goodbye!");
}
}
length() {
return this.itemsCount;
}
}
const things = ["Hat", "Umbrella", "Mug"];
class MagicBox extends Box {
constructor() {
super();
this.items.push(choice(things));
this.itemsCount = this.items.length;
}
}