Chapter 2 Flashcards
What is object oriented programming?
A programming technique that makes use of objects. Objects are self contained data structures that consist of properties, methods and events.
What is an object?
Objects are created from templates defined by classes.
What are an objects properties?
It is the data represented by an object.
Properties are class members that can be accessed like data fields but contain code like a method.
What are an objects methods?
Methods are an objects behaviour.
They are a block of code containing a series of statements.
Method defines the actions or operations of a class.
What is an event?
An event provides communication between objects
What is encapsulation?
The concept of hiding the complexity of a data structure.
In a class it prevents users from directly modifying attributes, and forces the user to use defined functions in order to modify them (called interfaces).
Encapsulation uses access modifiers to define the access level to data using public, private, protected etc.
What is a class?
It is the template from which individual objects are created, the object is also known as the instance of a class.
What is an access modifier?
It specifies what region of the code will have access to a field e.g private, public, protected.
How do you declare a method?
A method is defined by specifying the access level (private, public), the return type (double, int), the name of the method and an optional list of parameters eg public double GetArea ()
What does the void keyword mean when used on a method?
If a method does not return a value must use the void keyword.
How does a method return a value?
A method can return a value to the code calling it. If a method does return a value it must use the return keyword in the code, the return keyword will stop the code executing and return to the calling code.
What is a constructor?
Broadly speaking, a constructor is a method in the class which gets executed when its object is created.
It is a special class method that is executed when a new instance of a class is initialized.
It must have exactly the same name as the class and does not have a return type.
What is a default constructor?
Constructors that take no arguments are default constructors.
If a class is defined without a constructor a default constructor is created that does nothing.
What are the 2 accessors for a property?
Property has 2 accessors - get and set.
Get - used to return the property value.
Set - used to assign a new value to the property.
Leaving out the set means can’t set the value of the property (read-only).
Leaving out the get means can’t get the value of the property (write-only).
What are auto-implemented properties?
Auto-implemented properties simplify property declarations - introduced in C#.
public double Length {get; set}
public double Width {get; set}
Using this means that backing fields for properties are defined behind the scenes and are not accessible by code.
What is the this keyword?
This keyword is used to access members from within constructors, instance methods and accessors.
private double length; private double width; public Rectangle (double l, double w) { this.length = l; this.width = w; }
What is constructor overloading?
A class can have multiple constructors that take multiple arguments.
It is often useful to have more than one way to initialise an object, by using multiple constructors.
What is a method’s signature?
A method’s name, parameter list and order of the data types of the parameters.
What is a delegate?
Delegates are a special type used to encapsulate a method with a specific signature.
Delegates are defined using the delegate keyword.
What are events?
Events are a way for a class to notify other classes or objects when something of interest happens.
The class that sends information is publisher, class that receives is subscriber e.g. click event in GUI. Events aren’t limited to GUI.
What do events normally need?
Delegate that connects event with its handler method.
Class that contains event data - derived from EventArgs class.
What is a namespace?
Namespaces allow you to organise code and create unique class names.
Defined using the namespace keyword.
What are static members?
Static members belong to a class itself rather than individual object type.
Static member referenced using class name e.g. Rectangle.ShapeName.
Can’t use this keyword on static member only use this on instance objects.
What is a value type?
Value types directly store data within an allocated block of memory, dependent on the type e.g. declaring an int causes compiler to allocate 4 bytes of memory.
What is a reference type?
Reference types store a reference to a memory location e.g. when declare circle variable does not allocate memory to hold circle, allots a small area of memory that holds the address (reference) to another block of memory that holds the circle object.
Memory for circle object is only allocated when new keyword is used to create object.
What are structs?
Structs create user defined types that consist of small groups of related fields. Used to define simple types, if complex use a class.
Defined using keyword struct.
Structs are similar to classes in that they can have methods, properties etc.
They are value types as opposed to classes which are reference types.
Struct cannot inherit from another class or struct unlike a class.