INHERITANCE
• System.Object class is the root class of all the classes. All classes implicitly derive from the System.Object class
• A derived class automatically contains all fields from the base class. These field require initialization when an object is created
• Therefore the constructor for a derived class must call the constructor for its base class
• If base class constructor is not explicitly called, compiler does that automatically
• It is not possible to cast a child class object to another child class object even if they derive from same parent class, shown as under:
class Token // Base class
class IdentifierToken:Token //Child class1
class KeywordToken: Token //Child class2
IdentifierToken it = new IdentifierToken();
KeywordToken kt = it; // error - type conversion invalid
• Although converting to an object higher up in the hierarchy is allowed, hence following is legal
IdentifierToken it = new IdentifierToken();
Token t = it; // this is ok, as it is a child class object, deriving from Token
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment