Find the output of C#.Net programs | Method Overriding | Set 1: Enhance the knowledge of C#.Net Method Overriding concepts by solving and finding the output of some C#.Net programs.
Question 1:
using System;
namespace Demo
{
class Parent
{
public void Print()
{
Console.WriteLine("Parent class Method called");
}
}
class Child:Parent
{
public void Print()
{
Console.WriteLine("Child class Method called");
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Parent P = new Parent();
P.Print();
P = new Child();
P.Print();
}
}
}
Question 2:
using System;
namespace Demo
{
class Parent
{
public override void Print()
{
Console.WriteLine("Parent class Method called");
}
}
class Child:Parent
{
public virtual void Print()
{
Console.WriteLine("Child class Method called");
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Parent P = new Parent();
P.Print();
P = new Child();
P.Print();
}
}
}
Question 3:
using System;
namespace Demo
{
class Parent
{
public virtual void Print()
{
Console.WriteLine("Parent class Method called");
}
}
class Child:Parent
{
public override void Print()
{
Console.WriteLine("Child class Method called");
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Parent P = new Parent();
P.Print();
P = new Child();
P.Print();
}
}
}
Question 4:
using System;
namespace Demo
{
class Parent
{
public void Print()
{
Console.WriteLine("Parent class Method called");
}
}
class Child:Parent
{
public new void Print()
{
Console.WriteLine("Child class Method called");
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Parent P = new Parent();
P.Print();
P = new Child();
P.Print();
}
}
}
Question 5:
using System;
namespace Demo
{
class Parent
{
public virtual void Print();
}
class Child:Parent
{
public override void Print()
{
Console.WriteLine("Child class Method called");
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Parent P = new Child();
P.Print();
}
}
}
Answer 1:
Output:
Explanation:
In the above program, we created three classes Parent, Child, and Program. Here, Parent class inherited in Child class. Both Parent and Child class contains the Print() method.
Now look to the Main() method of Program class, Here we created the object P of Parent class and then call Print() method, then it will call the Print() method of Parent class,
P = new Child();
Using the above statement we re-initialized the P using Child class. Then we called the Print() method, it will call the Print() method of Parent class again. If we want to override the method in Child class then we need to use virtual and override keywords.
Answer 2:
Output:
Explanation:
The above program will generate syntax error because we used the override keyword in the Parent class method. We can use the override method in child class only. Because we can override existing parent class method in child class.
Answer 3:
Output:
Explanation:
In the above program, we created three classes Parent, Child, and Program. Here, Parent class inherited in Child class. Both Parent and Child class contains the Print() method. We used the virtual keyword in the Parent class method and override keyword in Child class.
Now look to the Main() method of Program class, Here we created the object P of Parent class and then call Print() method, then it will call the Print() method of Parent class.
P = new Child();
Using the above statement we re-initialized the P using Child class. Then we called the Print() method, it will call the Print() method of Parent class again. If we want to override the method in Child class then we need to use virtual and override keywords.
Answer 4:
Output:
Explanation:
In the above program, we created three classes Parent, Child, and Program. Here, Parent class inherited in Child class. Both Parent and Child class contains the Print() method.
Now look to the Main() method of Program class, Here, we created the object P of Parent class and then call Print() method, then it will call the Print() method of Parent class.
P = new Child();
Using the above statement we re-initialized the P using Child class. Then we called the Print() method, it will call the Print() method of Parent class again. If we want to override the method in Child class then we need to use virtual and override keywords.
Answer 5:
Output:
Explanation:
The above program will generate syntax error because we did not define the Print() method in Parent class. That's why errors will be generated in the above program.
need an explanation for this answer? contact us directly to get an explanation for this answer