When an object no longer has any reference variables referring to it, what happens to it?
The garbage collector makes the memory it occupies available for new objects.
Name 2 important features when creating a static class. (Mention 1 more for a bonus)
1. Class is marked static and all members are static.
2. Cannot initialize instances of a static class.
3. Classes cannot inherit from a static class.
Class Student is inheriting class Person and wants change a behavior (a method).
Given that C# is used, the method to be changed must be marked as
_______ in the base-class and
_______ in the derived-class.
virtual in the base-class
override in the derived-class
Classes can extend or derive from how many class(es) in C# .NET? Which OOP pillar does this represent.
Only 1: a class can inherit from one class only.
However, you may have multiple layers of Inheritance.
Examine the following section of code:
ChocolateBox Alice = new ChocolateBox(10);
ChocolateBox Ben = Alice;
ChocolateBox Carmen = new ChocolateBox(30);
ChocolateBox David = Carmen;
ChocolateBox Eddy = new ChocolateBox(50);
How many objects (total) are created? After the last statement has executed, how many objects are now accessible (Do not count garbage collected objects)?
How many pointers created
3 Created and 3 accessible
5 pointers.
What can an instance method member access in a class ?
Instance methods and instance data members.
(All members that are not marked static)
Static methods and static data members.
(All members that are marked static)
A protected access modifier for an instance member indicates that the member can be accessed in:
- Inside the same class
- In any class that inherits from the class containing the protected member.
What is the base or parent class of any defined class in C#? (Of all defined classes)
Object class.
A class default constructor refers to?
Do you need to create one when create a class?
A constructor without parameters
No need to create one, the compiler will create one for you.
class Food{
public static GetCount(){ .... }
}
class Cookie: Food{ ... }
Which of the following is the correct way to access GetCount() method? Explain.
Food.GetCount();
Cookie.GetCount();
Both are correct. Static method can be accessed via both parent or child class name.
What does this UML class diagram represent?
Teacher show UML diagram
Class 3 inherits from Class 2
And Class 2 inherits from Class 1
You have encountered a class that is marked as abstract. What can you infer about that class?
What does it mean to be an abstract class?
1. You cannot create instances of that class.
2. Class mainly created for Inheritance purposes.
3. Class may include a method without implementation, but a derived-class must implement.
Examine the following section of code:
ChocolateBox section1 = new ChocolateBox(10);
ChocolateBox section2 = section1;
ChocolateBox section3 = section1;
ChocolateBox section5 = new ChocolateBox(20);
section1 = null;
How many objects (total) are created? After the last statement has executed, how many objects are now accessible (Do not count garbage collected objects)?
2 created and 2 accessible
static class Calc
{
public static Sum(x,y) { .... }
}
Create instance of Calc and use the Sum method to calculate the sum of 10 & 20
cannot ..
Calc.Sum(10,20)
class Shape {
public Shape (string x)
{ .... }
}
class Square: Shape {
public Square(string y): base(y)
{ .... }
}
Which constructor will be called first when you create a Square object?
The parent then the child
You have learned two types of class relations:
The “Has-A” relationship refers to _____, which means______ and
The “Is-A” relationship refers to _____, which means______
Composition: which means an object of a class is a data member of another class.
Inheritance: a derived-class (child) inherits all the members of a base-class (parent) [Code reuse]
Examine the following section of code:
ChocolateBox Alice = new ChocolateBox(10);
Alice = new ChocolateBox(20);
How many objects (total) are created? After the last statement has executed, how many objects are now accessible (Do not count garbage collected objects)?
2 created and 1 accessible
A class has a private static data member: _xyz.
A programmer created a public instance getter: GetXYZ() for that static data member.
Can GetXYZ() access the data member and get the value of this static data member in Main. Explain
Yes, but only by creating an instance of the class.
The getter should be marked as static to access it by class name.
class Shape {
public Shape (string x){ .... }
}
class Square : Shape {
public Square(string y){ .... }
}
Can you create an object of type Square? Why?
No, you need to call the base constructor by using the base keyword in the Square class.
abstract class Shape {
public abstract double GetArea();
}
class Square : Shape {
public double GetSide(){ ...}
}
Can you create an object of type Square? Why?
No, compiler error.
Square must implement abstract method GetArea()