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.
What is interface?
What is the main purpose of using interfaces in OOP design?
A type (construct) that only contains method/property signatures and no implementation. /
To define a contract for a shared behavior / to specify what a class can do without specifying how
What access modifier should be used for:
Data member / Method Members
Data member always private
Method Members usually public (private for methods for internal use)
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.
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]
What is the main purpose of using interfaces in OOP design?
To define a contract for behavior / to specify what a class can do without specifying how
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 is the base or parent class of any defined class in C#? (Of all defined classes)
Object class.
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.
Can you implement multiple interfaces on a class? If so how?
class ABD: Iinterface1,Iinterface2, Iinterface3
{ .. }
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
3 pointers to 1st object and 1 point to 2nd object
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 (shape) then the child (Square)
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()
public interface IShape { double GetArea(); }
public class Circle : IShape { public double Radius; }
Can you create an instance of Circle? Why?
No, it does not implement GetArea()
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.
class Animal { .. }
class SeaAnimal : Animal { .. }
class LandAnimal : Animal { .. }
class WildAnimal : LandAnimal { .. }
//What type of casting is used in the following? Explain if there is an error.
1. WildAnimal wAnimal = new WildAnimal();
2. Animal animal = wAnimal;
3. SeaAnimal sAnimal = new Animal();
4. LandAnimal lAnimal = animal;
5. SeaAnimal sAnimal2 = wAnimal as SeaAnimal;
1. No casting: pointer and instance have the same type
2. Upcasting: child instance assigned to parent
3. Error: Down-casting cannot create parent and assign to child (need as, is or explicit for the compiler not to give an error. But at runtime it will not cast as it is not a SeaAnimal)
4. Error: Down-casting (need as, is or explicit) parent pointer assigned to child pointer (will work if down casted properly)
5. Error: cannot cast (different branch - sibling)
What is polymorphism? Give an example where is can be observed.
A method with the same signature having different implementations in different classes. It can be observed when override methods (Inheritance), implementing (overriding) abstract methods (Inheritance) and when implementing and interface.