Issue
Classes can add methods when they extend from a super class, but can they do the opposite and remove methods? The goal is for these removed classes to not be accessible at compile time. I'm essentially trying to get (new car()). to not autocomplete .drive() in an IDE.
class vehicle {
drive() {
...
}
}
class car extends vehicle {
//previous attempt
private drive() { // can't modify public/private
}
}
Is there a way at compile time (tsc) to prevent car from having the method drive? Can we either delete it or modify it's accessibility?
Solution
No. As noted in comments, this is indicative that your hierarchy is not correct. A derived class must always be substitutable for its base class.
You have to consider what happens when someone writes code like this
function doSomething(y: Vehicle) {
y.drive();
}
let x = new Car();
doSomething(x);
Answered By - Ryan Cavanaugh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.