Let's say that we have 2 classes. The first one is Person
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace People
{
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
The second one is Teacher
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace People
{
class Teacher:Person
{
public string Position { get; set; }
}
}
And I want to make a teacher object using this syntax Person teacher = new Teacher(); When I make it and I try to refer to the Position property, I can't... Why does this happen? I can instead use this syntax Person teacher = new Teacher() {Position="boss" }; but even though, after that I can't refer to the teacher position using "teacher.Position".
Person, which has noPositionproperty. Did you mean to add that property toPerson? Related to this, look into something called the Liskov Substitution Principle. Meaningful type inheritance is more than just what keywords are used where.Personclass doesn't have aPositionproperty, the compiler will not let you access it, even if the actual object acutally has that property.