Find the output of C#.Net programs | Method Overriding | Set 2: 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 abstract 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();
}
}
}
Question 2:
using System;
namespace Demo
{
abstract class Parent
{
public abstract 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();
}
}
}
Question 3:
using System;
namespace Demo
{
abstract class Parent
{
public abstract 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 Parent();
P.Print();
}
}
}
Question 4:
using System;
namespace Demo
{
abstract 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();
}
}
}
Question 5:
using System;
namespace Demo
{
abstract class Parent
{
public abstract void Print();
public void PrintHello()
{
Console.WriteLine("Hello World");
}
}
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();
P.PrintHello();
}
}
}
Answer 1:
Output:
Explanation:
The above program will generate syntax error because we cannot create an abstract method in the non-abstract class.
Answer 2:
Output:
Explanation:
In the above program, we created an abstract class Parent that contains the declaration of the Print() method and then we override the Print() method inside the Child class. The Parent class is inherited in Child class.
Now look to the Main() method,
In the above method we created object P using Child() class constructor, we override the Print() method in Child class that's why Child class constructor is called using object P.
Answer 3:
Output:
Explanation:
The above program will generate syntax error because here create the object of Parent class. But Parent is an abstract class and we cannot create the object of a Parent class.
Answer 4:
Output:
Explanation:
The above program will generate syntax error because we declared the Print() method in the Parent class without using the abstract keyword. Here Parent is an abstract class, so here we need to use the abstract keyword instead of the virtual keyword.
Answer 5:
Output:
Explanation:
In the above program, we created three classes Parent, Child, and Program. Parent is an abstract class that contains two methods Print() and PrintHello(). Here, Print() is an abstract method and PrintHello() is a non-abstract method. The Print() method is overridden in the Child class.
Let's look to the Main() method, here we created object P then call Print() and PrintHello() method.
need an explanation for this answer? contact us directly to get an explanation for this answer