1. Encapsulation Flashcards

1
Q

What does encapsulation mean?

A

Encapsulation means that a group of related properties, methods, and/or other members are treated as a single unit or object.

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

How do you instantiate a class in C#?

A

You instantiate a class by using the new keyword.

eg. var computer = new Computer();

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

How do you find out the type of an object in C#?

A

By using the GetType method.

eg. computer.GetType();

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

How do you determine whether a given object is the instance of a given class?

A

By using the is keyword.

eg. computer is Computer; // true

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

What is a field?

A

A field is a variable that is defined inside a particular class.

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

What is the default access modifier for members and what does that mean in terms of access?

A

It’s private. By default, they are accessible from only within the class that they’re defined in.

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

What does the internal access modifier do for a class’s members?

A

It allows access to them from only the classes of the same namespace or the same type as the class that they’re attached to.

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

What does the protected access modifier do for members?

A

It allows access to them only from within the classes that inherit the class that they’re defined in.

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

What does the private access modifier do for members?

A

It allows access to them from only within the class that they’re defined in.

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

What does the public access modifier do for members?

A

It allows access to them from anywhere where the class that they’re defined in is accessible.

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

What makes a property more favourable over a field?

A

Properties are better for protecting the internal data of an object.

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

How are properties defined?

A

They’re defined similarly to members but with a getter and/or setter.

eg. public static Name { get; set; }

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

How do you set the default value of a field?

A

By assigning it a value as part of its definition using the assignment operator.

eg. private static _Name = “Some Value”;

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

What are getters and setters useful for?

A

They are useful for encapsulating the logic behind the scenes for how we’re reading and writing data for our classes.

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

What’s the return type of a constructor?

A

Void — it doesn’t return anything.

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

What is a constructor?

A

A constructor is a special method that is used to initialize an object.

17
Q

How do you define custom read/write logic for properties?

A

Inside their getters and/or setters by using curly brackets.

eg. 

public static Name
{
get { /…custom logic/ }
// and/or
set { /…custom logic/ }
}
~~~
~~~