Reputation: 121
I'm new to Type Script and oop, i try to use a child method from the parent class but the method is not recognised.
Here is my Child class :
import {Ouvrage} from "./Class_Ouvrage";
import {Emprunt} from "./Class_Emprunt";
import {Adherant} from "./Class_Adherant";
export class Volumes extends Ouvrage {
private _NombreExemplaire: number;
private _NombreExemplaireEmpruntes: number;
private _auteur: string;
public get NombreExemplaire(): number {
return this._NombreExemplaire;
}
set NombreExemplaireEmpruntes(value: number) {
this._NombreExemplaireEmpruntes = value;
}
get NombreExemplaireEmpruntes(): number {
return this._NombreExemplaireEmpruntes;
}
get auteur(): string {
return this._auteur;
}
constructor(Pnombreexemplaire: number, Pnombreexemplairemprunte: number, Pauteur: string, PID: number, PTitre: string, Pdate: Date) {
super(PID, PTitre, Pdate);
this._auteur = Pauteur;
this._NombreExemplaire = Pnombreexemplaire;
this._NombreExemplaireEmpruntes = Pnombreexemplairemprunte;
}
descriptionVolume(): string {
return "l'auteur de ce volume s'apelle : " + this.auteur + " , il reste " + this.NombreExemplaire + "disponible(s)" + " Le nombre de volumes déja emprunté, s'éleve a " + this.NombreExemplaireEmpruntes;
}
public NbExemplaireDispo() : number{
return this.NombreExemplaire - this.NombreExemplaireEmpruntes;
}
}
Here is the parent class, where i'd like to call the method :
import {Volumes} from "./Class_Volume";
import {Adherant} from "./Class_Adherant";
export class Ouvrage{
private _id : number;
private _titre : string;
private _date : Date;
get id(): number {
return this._id;
}
get titre(): string {
return this._titre;
}
get date(): Date {
return this._date;
}
constructor(PID : number, PTitre : string, Pdate : Date) {
this._id = PID;
this._titre = PTitre;
this._date = Pdate;
}
descriptionOuvrage() : string{
return "Cet ouvrage s'apelle " + this.titre + " et il est sorti le " + this.date+" ";
}
this.NbExemplaireDispo();
}
How do i make NbExemplaireDispo()
working ?
as i said i'm new to oop, it's not a good practice to do this kind of call ?
Thanks!
Upvotes: 0
Views: 1181
Reputation: 1074999
i try to use a child method from the parent class but the method is not recognised
...
as i said i'm new to oop, it's not a good practice to do this kind of call ?
Right. Parent classes shouldn't call methods defined by child classes. One reason for that is that the instance where you're making the call may not be an instance of the child class (it could be just an instance of the parent, or the instance of another child). Consider this simpler example:
class Parent {
parentMethod() {
this.childMethod(); // Won't compile in TypeScript
}
}
class Child extends Parent {
childMethod() {
// ...
}
}
const p = new Parent();
p.parentMethod(); // Would throw an error in JavaScript, because `parentMethod`
// tries to use `childMethod`, which doesn't exist -- it's a
// `Parent`, not a `Child`
And a similar issue exists if there's Child2
and Child2
doesn't have the method, and you have
const c2 = new Child2();
c2.parentMethod(); // Would throw in JavaScript
Those "would throw in JavaScript" are one of the reasons TypeScript is useful — it warns you of these things at compile-time instead of runtime.
If the parent should be making calls that the child may want to handle differently, you define the method on the parent, and override it on the child. For instance:
class Parent {
parentMethod() {
this.theMethod();
}
theMethod() {
// ... this might even not do anything ...
}
}
class Child extends Parent {
theMethod() {
// ... this can do something slightly different from `Parent`'s version
// Can access `Parent`'s version if desired via `super.theMethod()`.
}
}
const p = new Parent();
p.parentMethod(); //Works
You might want an abstract
method: That would mean you could never create instances of Parent
, and subclasses would have to implement the method:
class Parent {
parentMethod() {
this.theMethod();
}
abstract theMethod();
}
class Child extends Parent {
theMethod() {
// ... this can do something slightly different from `Parent`'s version
// Can access `Parent`'s version if desired via `super.theMethod()`.
}
}
const c = new Child();
c.theMethod(); //Works
const p = new Parent(); // Won't compile, because `Parent` is abstract
Upvotes: 1