class Program
{
static void Main(string[] args)
{
MyClassB b = new MyClassB();
MyClassA a = b;
a.abc();
Console.ReadLine();
}
}
class MyClassA
{
public MyClassA()
{
Console.WriteLine("constructor A");
}
public void abc()
{
Console.WriteLine("A");
}
}
class MyClassB:MyClassA
{
public MyClassB()
{
Console.WriteLine("constructor B");
}
public void abc()
{
Console.WriteLine("B");
}
}
Answer :-
constructor A
constructor B
A
During initialization of the B class, the constructor of the A class will be executed by default, then constructor of the B class. After assignment of the b value to a type variable of the A class, we will get an instance of the B class in it. One would think that abc() from the B class should be called, but since there is no specification of any predicate of the abc method in the B class, it hides abc from the A class. The example is not quite correct and abc() in the B class will be underlined, since the new predicate is required.
0 Comments:
Post a Comment