K
K
Konstantin2018-07-16 00:50:34
typescript
Konstantin, 2018-07-16 00:50:34

Why can't access parent class method?

export interface IProfileStaff {
  getName(): string;
}

export interface IProfilePupil {
  getName(): string;
  getOrganization(): void;

}

export class Organization { 

  protected getOrganization() { 
    return 'Organization name';
  }
}

export class ProfileBase extends Organization  { }

export class StaffProfile extends ProfileBase implements IProfileStaff {

  public getName() { 
    return '';
  }

}

export class PupilProfile extends ProfileBase implements IProfilePupil {
    public getName() { 
    return '';
  }
}

let a = new StaffProfile();
let b = new PupilProfile();
let arr: ProfileBase[] = [];

arr.push(a);
arr.push(b);

Why can't I access the getOrganization method of the Organization class
a.getOrganization(); // Здесь

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2018-07-16
@Junart1

Why can't I access the getOrganization method of the Organization class

Because it is protected: available to the class itself and its descendants. But not publicly. You can, for example, add
public getOrganization() { 
    return super.getOrganization();
  }
the ProfileBase class. Then the original will still be hidden in the Organization class, but available for calling from ProfileBase and ProfileBase descendants.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question