Hiding and overriding

Topics: Language Specification
Feb 4, 2014 at 3:39 PM
Hi,

in the language specification only overriding is explained (see chapter 8.2.3). Hiding of class members is (for me not exactly) explained in chapter 3.8.1. But what are the differences? Can I say, only properties can be hidden by sub classes and only methods can be overriden by sub classes?

class A {
public a: number;
public test(): void {}
}

class B extends A {
// hiding property A.a
public a: number;
// overriding method A.test
public test(): void {}
}

Thank you!
Feb 4, 2014 at 7:32 PM
Edited Feb 5, 2014 at 12:27 AM
removing untrue statement so I don't look stupid

Declaring members of the same name and type in a subclass does nothing. You're declaring a member to be part of the shape of B and it's compatible with the shape of A so that's fine. What's useful is if the member in the subclass has a narrower type.
class Alpha {
    public a: number;
}

class Bravo extends Alpha {
    public b: number;
}

class Charlie {
    public member: Alpha;
}

class Delta extends Charlie {
    public member: Bravo;  // all Bravos are Alphas so all Deltas can still be treated as Charlies
}
Developer
Feb 4, 2014 at 10:59 PM
See section 7.1 Interfaces in the language specification for some definitions of hiding members.
An interface has the members specified in the ObjectType of its declaration and furthermore inherits all base type members that aren’t hidden by declarations in the interface:
• A property declaration hides a public base type property with the same name.
• A call signature declaration hides a base type call signature that is identical when return types are ignored.
• A construct signature declaration hides a base type construct signature that is identical when return types are ignored.
• A string index signature declaration hides a base type string index signature.
• A numeric index signature declaration hides a base type numeric index signature.
But there's no distinction like the C# new vs override keywords on derived method signatures.
Feb 5, 2014 at 10:59 AM
Hi danquirk,

thank you, but are the same hiding rules for interfaces applied to classes, as well?

Is it true that,
  • a property (or instance/static variable) of the sub class HIDES a property (or instance/static variable) of the super class with the same name?
  • a property (or instance/static variable) of the sub class OVERRIDES a property (or instance/static variable) of the super class with the same name AND the same kind (instance or static). Furthermore the type of the overriding property (or instance/static variable) has to be a sub type of the overridden property (or instance/static variable)?
    The two rules above are applied to methods, too.