0

Let's assume I have the following Abstract Class Student:

public abstract class Student
{
    public string studentID {get; private set;}
    public string FirstName {get; private set;}
    public string lastname {get; private set;}
    public int age {get; private set;}

    public Student(string id,string firstname, string lastname, int age)
    {
        this.FirstName = firstname;
        this.lastname = lastname;
        this.age = age;
    }

    public abstract void calculatePayment();

}

and the following sub-class:

public class InternationalStudent:Student
{
    public bool inviteOrientation {get; private set;}

    public InternationalStudent(string interID,string first, string last, int age, bool inviteOrientation)
        :base(interID,first,last,age)
    {
        this.inviteOrientation = inviteOrientation;

    }

    public override void calculatePayment()
    {
       //implementation left out
    }

}

Main

InternationalStudent svetlana = new InternationalStudent("Inter-100""Svetlana","Rosemond",22,true);

InternationalStudent checkEq = new InternationalStudent("Inter-101","Trisha","Rosemond",22,true);

Question:

How would I properly implement my equalsTo method inside my subclass? I've been reading here but I'm confused because some answers indicate that a subclass shouldn't know the members of the parent class.

If I wanted to implement IEquality in a subclass, so that svetlana.Equals(checkEq); returns false, how would I go it?

As per the comments what makes the object equals is if the ID, First name, and Last name are the same.

Community
  • 1
  • 1
  • You are not specifying the conditions in which `Equals` will return true and the condition where it will return false. Please elaborate – Eminem Mar 17 '16 at 18:45

2 Answers2

2

I'm confused because some answers indicate that a subclass shouldn't know the members of the parent class.

You have that backwards. A subclass has complete knowledge of all non-private parent class members.

How would I properly implement my equalsTo method inside my subclass?

What defines "equality" in your subclass? Typically it's a combination of property values, but most of the pertinent properties are on the parent class, so would two "students" with the same first name, last name, age be "equal"?

public override bool Equals(object obj)
{
    var student = obj as InternationalStudent;

    if (student == null)
    {
        return false;
    } 

    return this.FirstName == student.FirstName &&
           this.lastname  == student.lastname &&
           this.age       == student.age;
}

However, since all of the [properties in question are inherited, perhaps this belongs on the parent class instead of the sub class.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Many thinks for this. I quickly created this example to illustrate my problem. I should have put a student ID in the parent class now that you mention the same names. The override Equals would be in the InternationalStudent class right? –  Mar 17 '16 at 18:45
  • I could also check to make sure that the object passed was a IS object before casting, correct? –  Mar 17 '16 at 18:46
  • @Svetlana `as` makes sure that the object is an appropriate type. Use whatever properties makes two objects "equal". If ID fits that description the use that. – D Stanley Mar 17 '16 at 18:50
  • One more question, should the abstract class have a equalsTo as well, or just the sub class? –  Mar 17 '16 at 19:00
  • You _could_ create an equals method on the base class that compares its properties, and call it from the base class, but it's not required. – D Stanley Mar 17 '16 at 19:01
  • Just to reaffirm then I'll drop it, the code you provided goes inside InternationStudent class right? –  Mar 17 '16 at 19:14
  • Since it checks to make sure the input is a `InternationalStudent` then yes. – D Stanley Mar 17 '16 at 19:16
  • If I wanted to add IEquatable, do I put in the subclass or the abstract class? so InternationalStudent:IEquateable or Student:IEquateable. My guess is the InterS would take the IEquatable because that way you can check all the members. –  Mar 17 '16 at 23:40
2

Define Equals in base class, then call it from the child class

public override bool Equals(object obj) {
    var other = obs as InternationalStudent;
    return other!= null && base.Equals(obj) && other.inviteOrientation == this.inviteOrientation;
}
George Chen
  • 6,592
  • 5
  • 21
  • 23