Question 1:
using System;
namespace Demo
{
class ABC
{
public ABC()
{
Console.WriteLine("ABC-Constructor called");
}
public void FUN()
{
Console.WriteLine("FUN() called");
}
}
class XYZ : ABC
{
public XYZ()
{
Console.WriteLine("XYZ-Construtor called");
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
XYZ X = new XYZ();
X.FUN();
}
}
}
Question 2:
using System;
namespace Demo
{
class ABC
{
public ABC()
{
Console.WriteLine("ABC-Constructor called");
}
protected void FUN()
{
Console.WriteLine("FUN() called");
}
}
class XYZ : ABC
{
public XYZ()
{
Console.WriteLine("XYZ-Construtor called");
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
XYZ X = new XYZ();
X.FUN();
}
}
}
Question 3:
using System;
namespace Demo
{
class ABC
{
protected ABC()
{
Console.WriteLine("ABC-Constructor called");
}
public void FUN()
{
Console.WriteLine("FUN() called");
}
}
class XYZ : ABC
{
public XYZ()
{
Console.WriteLine("XYZ-Construtor called");
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
XYZ X = new XYZ();
X.FUN();
}
}
}
Question 4:
using System;
namespace Demo
{
class ABC
{
protected ABC()
{
Console.WriteLine("ABC-Constructor called");
}
public void FUN()
{
Console.WriteLine("FUN() called");
}
}
class XYZ : ABC
{
protected XYZ()
{
Console.WriteLine("XYZ-Construtor called");
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
XYZ X = new XYZ();
X.FUN();
}
}
}
Question 5:
using System;
namespace Demo
{
class ABC
{
public ~ABC()
{
Console.WriteLine("ABC-Destructor called");
}
public void FUN()
{
Console.WriteLine("FUN() called");
}
}
class XYZ : ABC
{
public ~XYZ()
{
Console.WriteLine("XYZ-Destrutor called");
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
XYZ X = new XYZ();
X.FUN();
}
}
}
Answer 1 :
Output:
Explanation:
In the above program, we created two classes ABC and XYZ. Here, we inherited class ABC into class XYZ.
Class ABC contains a no-argument constructor and a method FUN(), and class XYZ contains a no-argument constructor.
Now look to the Main() method, the Main() method of Program class is the entry point of the program. Here, we created the object of class XYZ then it calls the constructor of class ABC before calling the constructor of class XYZ because of inheritance. Then we called the FUN() method of ABC class.
Answer 2:
Output:
Explanation:
The above program will generate a syntax error because a protected member of a class can only be accessed in the child or derived class, here FUN() method is called in Main() method of Program, which is not possible due to protection level.
Answer 3:
Output:
Explanation:
In the above program, we created two classes ABC and XYZ. Here, we inherited class ABC into class XYZ.
Class ABC contains a no-argument constructor with a protected access modifier and a method FUN(), and class XYZ contains a no-argument constructor.
Now look to the Main() method, the Main() method of Program class is the entry point of the program. Here, we created the object of class XYZ then it calls the constructor of class ABC before calling the constructor of class XYZ because of inheritance. Here, constructor of ABC class is protected member; here it was called from child class XYZ. Then we called the FUN() method of ABC class.
Answer 4:
Output:
Explanation:
The above program will generate a syntax error because here we accessed the constructor of XYZ class from Program class, but here we declared constructor of XYZ as a protected member. But protected members can only be accessed in the same class on the derived class.
Answer 5:
Output:
Explanation:
The above program will generate two syntax errors because we cannot use an access modifier with destructors.
need an explanation for this answer? contact us directly to get an explanation for this answer