Z
Z
zlodiak2020-04-26 20:52:58
typescript
zlodiak, 2020-04-26 20:52:58

Why is the abstract method spelled wrong?

There is an abstract class which contains an abstract method (without implementation). There is an ordinary class that inherited an abstract class and defined an implementation of the abstract method:

abstract class Figure {
    getArea(): void{
        console.log("Not Implemented")
    }
    abstract getColor(): void {}
}
class Rectangle extends Figure{
  getColor() {
    console.log('is red');
  }
}

// const f = new Figure(); // error
const r = new Rectangle();
console.log(r)
r.getArea();
r.getColor();


DEMO

The problem is that the compiler underlines it abstract getColorwith a red line and displays the following error text:
Method 'getColor' cannot have an implementation because it is marked abstract
.

Tell me, what's wrong? Is this a vscode glitch or have I incorrectly inherited from an abstract class?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aarifkhamdi, 2020-04-26
@zlodiak

You have an implementation for getColor. take away

abstract class Figure {
    getArea(): void{
        console.log("Not Implemented")
    }
    abstract getColor(): void // {} <<<<<<<<<<<<<<
}
class Rectangle extends Figure{
  getColor() {
    console.log('is red');
  }
}

// const f = new Figure(); // error
const r = new Rectangle();
console.log(r)
r.getArea();
r.getColor();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question