Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Constructor Inheritance | Inheritance
Object-Oriented Programming in JavaScript
course content

Зміст курсу

Object-Oriented Programming in JavaScript

Object-Oriented Programming in JavaScript

1. Classes & Objects
2. Encapsulation
3. Static
4. Inheritance

bookConstructor Inheritance

If there is a constructor in the parent class, the daughter class will inherit it. Here you create square, but use a Rectangle constructor, so you should pass both height and width parameters.

12
rectangle = new Rectangle(2, 5); square = new Square(4, 4);
copy

This is a bit inconvenient, so let's create constructor for Square class, that will use Rectangle constructor. To do that in daughter class, you should use super() function to call the parent constructor first.

12345678910111213141516171819
class Rectangle { height; width; constructor(height, width) { this.height = height; this.width = width; console.log('Rectangle created'); } } class Square extends Rectangle{ constructor(size){ super(size, size); // init other attributes if it is required console.log('Square created'); } } square = new Square(5);
copy

This way, first you call Rectangle's constructor(height, width), and after define other attributes initialization. Then Square`s constructor is called. This way, first you create parent's object, and after that daughter's one.

Завдання

To the class Square, add attribute color. Then create constructor with two parameters: side length and color. In its body, call Rectangle constructor to initialize width and length, and then initialize color.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 2
toggle bottom row

bookConstructor Inheritance

If there is a constructor in the parent class, the daughter class will inherit it. Here you create square, but use a Rectangle constructor, so you should pass both height and width parameters.

12
rectangle = new Rectangle(2, 5); square = new Square(4, 4);
copy

This is a bit inconvenient, so let's create constructor for Square class, that will use Rectangle constructor. To do that in daughter class, you should use super() function to call the parent constructor first.

12345678910111213141516171819
class Rectangle { height; width; constructor(height, width) { this.height = height; this.width = width; console.log('Rectangle created'); } } class Square extends Rectangle{ constructor(size){ super(size, size); // init other attributes if it is required console.log('Square created'); } } square = new Square(5);
copy

This way, first you call Rectangle's constructor(height, width), and after define other attributes initialization. Then Square`s constructor is called. This way, first you create parent's object, and after that daughter's one.

Завдання

To the class Square, add attribute color. Then create constructor with two parameters: side length and color. In its body, call Rectangle constructor to initialize width and length, and then initialize color.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 2
toggle bottom row

bookConstructor Inheritance

If there is a constructor in the parent class, the daughter class will inherit it. Here you create square, but use a Rectangle constructor, so you should pass both height and width parameters.

12
rectangle = new Rectangle(2, 5); square = new Square(4, 4);
copy

This is a bit inconvenient, so let's create constructor for Square class, that will use Rectangle constructor. To do that in daughter class, you should use super() function to call the parent constructor first.

12345678910111213141516171819
class Rectangle { height; width; constructor(height, width) { this.height = height; this.width = width; console.log('Rectangle created'); } } class Square extends Rectangle{ constructor(size){ super(size, size); // init other attributes if it is required console.log('Square created'); } } square = new Square(5);
copy

This way, first you call Rectangle's constructor(height, width), and after define other attributes initialization. Then Square`s constructor is called. This way, first you create parent's object, and after that daughter's one.

Завдання

To the class Square, add attribute color. Then create constructor with two parameters: side length and color. In its body, call Rectangle constructor to initialize width and length, and then initialize color.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

If there is a constructor in the parent class, the daughter class will inherit it. Here you create square, but use a Rectangle constructor, so you should pass both height and width parameters.

12
rectangle = new Rectangle(2, 5); square = new Square(4, 4);
copy

This is a bit inconvenient, so let's create constructor for Square class, that will use Rectangle constructor. To do that in daughter class, you should use super() function to call the parent constructor first.

12345678910111213141516171819
class Rectangle { height; width; constructor(height, width) { this.height = height; this.width = width; console.log('Rectangle created'); } } class Square extends Rectangle{ constructor(size){ super(size, size); // init other attributes if it is required console.log('Square created'); } } square = new Square(5);
copy

This way, first you call Rectangle's constructor(height, width), and after define other attributes initialization. Then Square`s constructor is called. This way, first you create parent's object, and after that daughter's one.

Завдання

To the class Square, add attribute color. Then create constructor with two parameters: side length and color. In its body, call Rectangle constructor to initialize width and length, and then initialize color.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 4. Розділ 2
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
some-alt