A a = new A();
A B = new B();
If that code is correct, then class B has to be derived from (or
inherit
) class A:
public class A {...}
Public class B : A {...}
Then, a variable containing an instance of A can also contain an instance of B because B "is an" A with added features.
It's like cars: a Car could be a Ford, or a Mercedes, or a BMW - but not an Orange because you can't drive an orange!
Ford, Mercedes, and BMW are all types of Car, so in a computer representation, their classes would inherit from the Car base class:
public class Car {...}
Public class Ford : Car {...}
public class Mercedes : Car {...}
Public class BMW : Car {...}
Unless a class contains the variable type in it's inheritance heirarchy, you can't assign an instance of that class to it.
Make sense?