3

I have one base class Base and a class Derived1 which is derived from Base class and another derived class Derived2 which is dervied from derived1.

Below i have mentioned few cases of object creation ( following by Multilevel inheritance of class ). Can someone help me in understanding those cases in which object creation is not possible and why it is not possible in C# ?

Base b1 = new Base() //Possible 
Base b1 = new derived1() // Possible 
Derived1 d1 = new Base() // Not Possible 
Derived1 d1 = new Derived1() // Possible 
Derived2 d2 = new Derived1() // ---- 
Derived1 d1 = new Derived2() // ---- 
Derived2 d2 = new Derived2() // Possible
Derived2 d2 = new Base() // ---- 
Base b1 = new Derived2() // ---- 
awesoon
  • 32,469
  • 11
  • 74
  • 99
Sagar
  • 272
  • 1
  • 4
  • 13
  • 1
    This is referred to as [Liskov's Substitution Principle](https://en.wikipedia.org/wiki/Liskov_substitution_principle). – Erik Philips Feb 28 '16 at 06:17

3 Answers3

5

Here is a super easy way:

public class A { }
public class B : A { }
public class C : B { }

So it's as simple as reversing the definitions:

A < B < C

(I'm using the greater than sign here, because B is everything A is and more. C is everything B and A are... and more.)

So A can support A, B and C. And B can support B and C. Lastly C can only support C.

Valid:

A z = new A();
A y = new B();  
A x = new C();
B w = new B();
B v = new C();
C u = new C();

Any other combination is not supported by C# (because of Liskov's substitution principle).

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • Suppose the above class structure is created. In that case which class constructor will be called and why ? (Can you please explain for all valid and Invalid cases ?) – Sagar Jun 01 '16 at 18:31
  • Normally I'd say, just ask the question. However, since that question was asked over 6 years ago, I'll just put this link here... [C# constructor execution order](http://stackoverflow.com/questions/1882692/c-sharp-constructor-execution-order); – Erik Philips Jun 01 '16 at 21:42
  • And [Why Do Initializers Run In The Opposite Order As Constructors? Part One](https://blogs.msdn.microsoft.com/ericlippert/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one/); – Erik Philips Jun 01 '16 at 21:43
1

Derived class has all the information about the base class, as inheritance is a "is-a" relationship.

We have a base class "Base" and a derived class "Derived"

according to inheritance rule "Derived is-a Base". All the properties of Base is present in Derived.

Base b = new Derived(); //It is possible as Derived as all the information about base.

Dervied d = new Base(); //It is not possible because base don't have the information about derived.

Mohit
  • 1,204
  • 2
  • 13
  • 19
  • Base b = new Derived(); // in the memory of dervied class we can accommodate base. – Mohit Feb 28 '16 at 06:22
  • Derived d = new Base(); // not possible because in memory of base we can't accommodate derived – Mohit Feb 28 '16 at 06:23
0

That's easy. The reference (variable declared, so left hand side) must be of less derived type. The instance on the right side may be more derived.

Jakub Szumiato
  • 1,318
  • 1
  • 14
  • 19