Your code already shows you making a new A()
and a new B()
. There is no AB()
. By the nature of inheritance, a B
is already an A
; every B
is also an A
– just a more specific sub-type.
A B = new AB();
says “declare B
to be a new variable of type A
, and assign it the reference to a new instance of AB
“. This would actually even compile if you create class AB : A
. It probably isn’t what you meant to do, though.
solved Not able to create an object of both the class why in c#?