TypeScript Language Specification v1.5
TypeScript Language Specification v1.5
TypeScript Language Specification v1.5
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Private and protected accessibility is enforced only at compile-time and serves as no more than an<br />
indication of intent. Since JavaScript provides no mechanism to create private and protected properties on<br />
an object, it is not possible to enforce the private and protected modifiers in dynamic code at run-time.<br />
For example, private and protected accessibility can be defeated by changing an object's static type to<br />
Any and accessing the member dynamically.<br />
The following example demonstrates private and protected accessibility:<br />
class A {<br />
private x: number;<br />
protected y: number;<br />
static f(a: A, b: B) {<br />
a.x = 1; // Ok<br />
b.x = 1; // Ok<br />
a.y = 1; // Ok<br />
b.y = 1; // Ok<br />
}<br />
}<br />
class B extends A {<br />
static f(a: A, b: B) {<br />
a.x = 1; // Error, x only accessible within A<br />
b.x = 1; // Error, x only accessible within A<br />
a.y = 1; // Error, y must be accessed through instance of B<br />
b.y = 1; // Ok<br />
}<br />
}<br />
In class 'A', the accesses to 'x' are permitted because 'x' is declared in 'A', and the accesses to 'y' are<br />
permitted because both take place through an instance of 'A' or a type derived from 'A'. In class 'B', access<br />
to 'x' is not permitted, and the first access to 'y' is an error because it takes place through an instance of<br />
'A', which is not derived from the enclosing class 'B'.<br />
8.2.3 Inheritance and Overriding<br />
A derived class inherits all members from its base class it doesn't override. Inheritance means that a<br />
derived class implicitly contains all non-overridden members of the base class. Only public and protected<br />
property members can be overridden.<br />
A property member in a derived class is said to override a property member in a base class when the<br />
derived class property member has the same name and kind (instance or static) as the base class property<br />
member. The type of an overriding property member must be assignable (section 3.10.4) to the type of<br />
the overridden property member, or otherwise a compile-time error occurs.<br />
Base class instance member functions can be overridden by derived class instance member functions, but<br />
not by other kinds of members.<br />
113