Senior
Suggest a correct inheritance model for Button and Input classes instead of the current one where Input extends Button.
sobes.tech AI
Answer from AI
Inheriting Input from Button is not logical, as Input and Button are different elements with different semantics and behaviors.
It is more correct to define a common base class, for example, UIControl or FormElement, which contains common properties and methods for all form elements. Then, Button and Input inherit from it as separate classes.
Example in TypeScript:
class UIControl {
constructor(public id: string) {}
render() {
// common rendering method
}
}
class Button extends UIControl {
click() {
// click logic
}
}
class Input extends UIControl {
value: string = '';
setValue(val: string) {
this.value = val;
}
}
This approach reflects the real hierarchy and helps avoid incorrect relationships between classes.