Home »
C#.Net »
C#.Net find output programs
C#.Net find output programs (Method Overriding) | set 1
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.
Submitted by Nidhi, on February 09, 2021
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();
}
}
}
Output:
Parent class Method called
Parent class Method called
Press any key to continue . . .
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.
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();
}
}
}
Output:
main.cs(7,30): error CS0115: `Demo.Parent.Print()' is marked as
an override but no suitable method found to override
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.
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();
}
}
}
Output:
Parent class Method called
Child class Method called
Press any key to continue . . .
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.
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();
}
}
}
Output:
Parent class Method called
Parent class Method called
Press any key to continue . . .
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.
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();
}
}
}
Output:
main.cs(7,29): error CS0501: `Demo.Parent.Print()' must have a body
because it is not marked abstract, extern, or partial
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.