I'm working through Jeff Fritz's c# tutorial videos, and there is some code like this that uses an abstract class:
public abstract class Shape {}
public class Rectangle : Shape {}
public class Square : Rectangle {
public string SquareOnlyMethod() { return "I am a square"; }
}
public static void Main()
{
Square s = new Square();
Console.WriteLine(s.GetType()); // Square
Console.WriteLine(s is Shape); // True
Console.WriteLine(s is Rectangle); // True
Console.WriteLine(s is Square); // True
Console.WriteLine(s.SquareOnlyMethod()); // I am a square
Shape r = new Square();
Console.WriteLine(r.GetType()); // Square
Console.WriteLine(r is Shape); // True
Console.WriteLine(r is Rectangle); // True
Console.WriteLine(r is Square); // True
Console.WriteLine(r.SquareOnlyMethod()); // 'Shape' does not contain a definition for 'SquareOnlyMethod' and no extension method 'SquareOnlyMethod' accepting a first argument of type 'Shape' could be found
}
Can somebody please explain the following?
- What is actually created when we do
Shape r = new Square();
? Is it aShape
or aSquare
? - Why does
GetType
returnSquare
but then the method cannot be found that is within theSquare
class?
Jeff says (if I understand correctly) that, "'Shape` is created with the footprint of Square", but then moves on.