What will be the output of the following C# code? [code] class sample { public int i; void display() { Console.Write

Thê Hy

New member
What will be the output of the following C# code?
Code:
 class sample
 {
     public  int i;
     void display() 
     {
         Console.WriteLine(i);
     }
}    
class sample1 : sample 
{
    public  int j;
    public void display() 
    {
        Console.WriteLine(j);
    }
}    
class Program
{
    static void Main(string[] args)
    {
        sample1 obj = new sample1();
        obj.i = 1;
        obj.j = 2;
        obj.display();
        Console.ReadLine();
    }
}
A. 1
B. 3
C. 2
D. Compile Time error
 
Answer: c
Explanation: class sample & class sample1 both contain display() method, class sample1 inherits class sample, when display() method is called by object of class sample 1, display() method of class sample 1 is executed rather than that of Class sample.
 

Tìm kiếm

Back
Top