H5-Class Hierarchies Flashcards

1
Q

What exception type can you use when a parameter is not in the expected rainge? e.g. string that is null, or empty.

A

ArgumentOutOfRangeException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you inherit from a parent class?

A

public class child : parent{

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Does a child class inherit the constructor of the parent?

A

No, so if the parent has a constructor parentconstructer(parameter1, parameter2) then the child could still use new child() as a constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you call the constructor of the parent?

A

public Employee(string firstName, string lastName,
string departmentName)
: base(firstName, lastName)
{
do something with departmentName
rest of parameters go to constructor for parent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What get’s executed first? parent or child constructor?

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What happens when the parent has only 1 constructor with parameters? What does the child have to do?

A
If both the parent and child class have constructors, the child class’s constructor must invoke
one of the parent class’s constructors
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what happens if the parent has 1 constructor that has parameters and the child is ChildConstrutor()
{
..
}

A

.Net will implicitly look for the parent construtor with no parameters. As this is not there an complition error will occure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What will happen when the child class does not have a constructor and the parent only has a construtor with parameters?

A

It will still work, this is the only situation where the child does not call the base() parent implicenty.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can you call a other constructor in the same class?

A

MyConstructor(String age, String name) : this (name)
{
store agem name was already processed by the call to this(name)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When calling a other constructor in the same class? who get’s executed first?

A

The constructor called with this(…) same as the base(…) call.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a best practice when you want to use 2 different constructors in the same class?

A

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){ ...}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly