简体   繁体   中英

in c# oop how to explain why a base class can take a derived class instance

Well, since I'm typing this from mobile I can't type or copy paste the whole code hence I'm linking to the SO post --> What is the error in this code? Interview

If you do X a = new Y(); it compiles. An interviewer asked how is this possible? I know this will be possible if X was defined as abstract class, but that isn't the case either.

If Y inherits X, then Y is a kind of X and can be used wherever X can.

This will work whether X is abstract of not.

class Y either inherits class X, or X is an interface that Y inherits.

But if someone asked me that in a job interview, I'd tell them I don't want to work anywhere that developers are creating types with single character names.

To expand on Branko Dimitrijević's answer, this is possible because of the Liskov substitution principle .

In your comment, you said

the base class and derived class can have their own distinct and different method/property/field definitions. So logically that statement shouldn't compile...but it does...i don't know the rationale behind it

It's true that the base class and the derived class have their own member definitions, but they are not entirely distinct and different. Specifically, every member of the base class is also a member of the derived class. For example, you can always say

object o = anything;
Console.WriteLine(o.ToString());
Console.WriteLine(o.GetHashCode());
Console.WriteLine(o.GetType().Name);

You can do that because every object has ToString, GetHashCode, and GetType methods , because every object's runtime type inherits either directly or indirectly from object.

You can do the same with a base class other than object:

class X
{
    public string Exclamation { get { return "Inherit this!"; } }
}
class Y : X
{
    public string Question { get { return "What's up with that?"; } }
}

Then it's perfectly legal to say

X x = new Y();
Console.WriteLine(x.Exclamation);

But of course, you can't say

X x = new Y();
Console.WriteLine(x.Question);  //does not compile!

In order to use the Question property, you need to have a reference of type Y.

To recap: because every member of the base class is also a member of the derived class, you can use an instance of the derived class as if it were an instance of the base class. The derived class instance has all of the members of the base class.

You raised the subject of abstract classes. An abstract class cannot be instantiated, of course; you can only instantiate a class derived from the abstract class. That doesn't affect the substitution principle, however, except to imply that a variable whose type is an abstract class must refer to an instance of a derived type, while a variable whose type is a non-abstract (unsealed) class may refer to an instance of a derived type.

Lets Assume

class Animal
{

}

class Tiger:Animal
{

}

Animal fooAnimal = new Tiger()

This is possible because Tiger is a type of Animal .

C# supports what is called covariance and contravariance. Covariance is the ability to implicitly (without any extra code) covert a derived class, in this case Y, to a base class, in this case X. This is possible because Y extends X, meaning that Y has all the same members as X. This allows a variable of type X to be assigned to a derived class.

This is important to extending classes and overloading. If class X contains a method called Foo and a derived class X creates a method called Foo, the method from Y will be called even if it is cast to a type X. Here is an example:

public class X
{
    public void Foo()
    {
         Console.WriteLine("Something");
    }
}
public class Y : X // y derives from X
{
    public override void Foo()
    {
         Console.WriteLine("Class Y");
    }
}
public class Program
{
    static void Main()
    {
          X item = new Y(); // covariance.
          item.Foo();   // prints "Class Y"
    }
}

I hope this isn't condesending, I do not know your skill level. This article from the MSDN explains it a litte, http://msdn.microsoft.com/en-us/library/ee207183.aspx .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM