H5-Class Hierarchies Flashcards
What exception type can you use when a parameter is not in the expected rainge? e.g. string that is null, or empty.
ArgumentOutOfRangeException
How do you inherit from a parent class?
public class child : parent{
Does a child class inherit the constructor of the parent?
No, so if the parent has a constructor parentconstructer(parameter1, parameter2) then the child could still use new child() as a constructor.
How do you call the constructor of the parent?
public Employee(string firstName, string lastName,
string departmentName)
: base(firstName, lastName)
{
do something with departmentName
rest of parameters go to constructor for parent.
What get’s executed first? parent or child constructor?
When a constructor uses the base keyword to invoke a base class constructor, the base class’s constructor executes before the body of the child class’s constructor executes.
What happens when the parent has only 1 constructor with parameters? What does the child have to do?
If both the parent and child class have constructors, the child class’s constructor must invoke one of the parent class’s constructors
what happens if the parent has 1 constructor that has parameters and the child is ChildConstrutor()
{
..
}
.Net will implicitly look for the parent construtor with no parameters. As this is not there an complition error will occure.
What will happen when the child class does not have a constructor and the parent only has a construtor with parameters?
It will still work, this is the only situation where the child does not call the base() parent implicenty.
How can you call a other constructor in the same class?
MyConstructor(String age, String name) : this (name)
{
store agem name was already processed by the call to this(name)
}
When calling a other constructor in the same class? who get’s executed first?
The constructor called with this(…) same as the base(…) call.
What is a best practice when you want to use 2 different constructors in the same class?
Put the actual code in methodes and call the methodes not the constructors.
public Customer(string email) {StoreEmail(email);}
public Customer(Address address){ StoreAddress(address);}
public Customer(string email, Address address) { StoreEmail(email); StoreAddress(address);}
// Store the email address. private void StoreEmail(string email){ ...}
// Store the postal address. private void StoreAddress(Address address){ ...}