Click here to Skip to main content
15,898,984 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Student is inherited from Person.

Person claire = new Student("Claire");
            claire.DescribeYourself();


Why is claire described as above instead of just doing something like:

Person claire = new Person("Claire");
            claire.DescribeYourself();


Thanks
Posted

Because if you want to use an instance as a class, you can only go back down the inheritance tree.

If you declare Claire as a Person, you can use her as a Person, but you can't use her where you want to use a Student, because a Person is not a Student, and cannot be cast to one - the additional information cannot be invented by the system automatically.

Conversely, if you define Claire as a Student, you can use her anywhere you would use a Person, because she contains everything a Person does.
 
Share this answer
 
You can see it this way: Not every Person is a Student but every Student is a Person, which mean every student have the attributes of being a student and a person. If you declare Claire as a Student Inherited from Person, you can access the Person and Students attributes but if you declare Claire as a Person, you can only access the Person attributes.
 
Share this answer
 
This is because a Student is special type of Person. Since it possesses any properties of that a Person has. But a Person may not be a Student. One example can be a Person may not take an exam. Here is a full inheritance article.Inheritance and behavioral subtyping[^].

BTW, The first concept is called LSP[^].
C#
Person claire = new Student("Claire");
            claire.DescribeYourself();
This is one of S.O.L.I.D Software Engineering principle[^]
 
Share this answer
 
All the above Solutions do fair justice with the Topic. Let me put this with some technical example.

Lets declare a "Person" Class. In this class we have a method "DescribeYourself", which is declared as a "virtual", so that it's implementation can be Overriden in it's Child Classes.

C#
public class Person
{
    private string PersonName;

    public Person() { }

    public Person(string personName)
    {
        PersonName = personName;
    }

    public virtual string DescribeYourself()
    {
          return "I am a Person. And my name is " + PersonName +".";    
    }

}


Now lets declare "Student" Class, inherited from "Person" Class. In this we have Overrided implementation of method "DescribeYourself".

C#
public class Student: Person
{
    private string StudentName;

    public Student(string studentName)
    {
        StudentName = studentName;
    }

    public override string DescribeYourself()
    {
        return "I am a Student. And my name is " + StudentName + ".";
    }
}


Lets declare one more Class "Teacher", inherited from "Person" Class, In this also we have Overrided implementation of method "DescribeYourself".

C#
public class Teacher : Person
{
    private string TeacherName;

    public Teacher(string teacherName)
    {
        TeacherName = teacherName;
    }


    public override string DescribeYourself()
    {
        return "I am a Teacher. And my name is " + TeacherName + ".";
    }

}


Now lets see Output of different Instantiations.

1) Firstly lets declare object of Class "Person", instantiated by the same Class.
C#
string whoIAm = string.Empty;
Person someOne = null;

someOne = new Person("Claire");
whoIAm = someOne.DescribeYourself();

Result: I am a Person. And my name is Claire.

2) Now instantiate object of Class "Person" by Class "Student".
C#
string whoIAm = string.Empty;
Person someOne = null;

someOne = new Student("Claire");
whoIAm = someOne.DescribeYourself();

Result: I am a Student. And my name is Claire.

3) Lets instantiate object of Class "Person" by Class "Teacher".
C#
string whoIAm = string.Empty;
Person someOne = null;

someOne = new Teacher("Claire");
whoIAm = someOne.DescribeYourself();

Result: I am a Teacher. And my name is Claire.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900