1. Encapsulation Flashcards
What does encapsulation mean?
Encapsulation means that a group of related properties, methods, and/or other members are treated as a single unit or object.
How do you instantiate a class in C#?
You instantiate a class by using the new
keyword.
eg. var computer = new Computer();
How do you find out the type of an object in C#?
By using the GetType
method.
eg. computer.GetType();
How do you determine whether a given object is the instance of a given class?
By using the is
keyword.
eg. computer is Computer; // true
What is a field?
A field is a variable that is defined inside a particular class.
What is the default access modifier for members and what does that mean in terms of access?
It’s private
. By default, they are accessible from only within the class that they’re defined in.
What does the internal
access modifier do for a class’s members?
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.
What does the protected
access modifier do for members?
It allows access to them only from within the classes that inherit the class that they’re defined in.
What does the private
access modifier do for members?
It allows access to them from only within the class that they’re defined in.
What does the public
access modifier do for members?
It allows access to them from anywhere where the class that they’re defined in is accessible.
What makes a property more favourable over a field?
Properties are better for protecting the internal data of an object.
How are properties defined?
They’re defined similarly to members but with a getter and/or setter.
eg. public static Name { get; set; }
How do you set the default value of a field?
By assigning it a value as part of its definition using the assignment operator.
eg. private static _Name = “Some Value”;
What are getters and setters useful for?
They are useful for encapsulating the logic behind the scenes for how we’re reading and writing data for our classes.
What’s the return type of a constructor?
Void — it doesn’t return anything.