2. Inheritance Flashcards
What is inheritance?
Inheritance is the act of creating new classes based on existing classes.
What does inheritance allow us to do?
It allows us to take all the same members that are used by a given set of classes and extract them into they’re own class.
What is an abstract class?
An abstract class is a class that contains all the business logic that a normal class would use, but it cannot be directly instantiated.
What is the purpose of an abstract class?
To have its functionality extended by classes that inherit it.
How is an abstract class defined?
By using the abstract
modifier on a class.
eg. abstract class Computer { … }
How does a class extend another?
By specifying its name after a column next to its own name.
eg. class Desktop : Computer { … }
What is the default access modifier for classes?
The default access modifier for classes is internal
.
What is the base
keyword?
base
is a keyword that’s used by child classes to refer to their parent class.
How does a child class override the property of its base class?
By defining a property that its base class already has, using the override
keyword.
eg. public override string Name { get; set; }
How does a child class define a new property with the same name as its base class?
By defining the same property and using the new
keyword.
eg. public new string Name { get; set; }
Can a child property/method override a “normal” property/method of its base class?
No. In order for a base class property or method to be overridden, it needs to be marked as virtual or abstract.
eg. public virtual string Name { … }
How does a child class access the properties of its base class?
By using the base
keyword.
eg. base.Name
What is the virtual
keyword?
It’s a modifier that indicates that the given property/method CAN be overridden by a child class.
eg. public virtual string Name;
What is the abstract
keyword?
It’s a modifier that indicates that the given property/method MUST be overridden by a child class.
eg. public abstract string Name;
How do you define a setter or a getter so that it can only be called from within the class?
By using the private
access modifier.
eg. { private get; /* and/or */ private set; }