using System;
class MyClass
{
public static void Main()
{
Student a = new Student();
Person y = new Gamer();
Console.WriteLine(a.X);
Console.WriteLine(y.X); <=== Error here
}
}
abstract class Person
{
public abstract void Display();
}
class Student : Person
{
public int X { get; set; } = 1;
public override void Display()
{
Console.WriteLine("Student");
}
}
class Gamer : Person
{
public int X { get; set; } = 1;
public override void Display()
{
Console.WriteLine("Gamer");
}
}
Hi,
I’ve tried to inherit Student, Gamer class from Person. In the code above, I don’t know why y object cannot access to X – property of Gamer class.
Please, help me to explain. Thanks
>Solution :
just edit this line:
// Person y = new Gamer(); ==> Person does not have a field named X
Gamer y = new Gamer();