[Solved] Inheritance and Polymorphism

Should I use the Parent's methods or the Child's?

Page 1 of 1

8 Replies - 730 Views - Last Post: 27 January 2010 - 06:22 PM Rate Topic: -----

#1 becstribe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 09-April 09

[Solved] Inheritance and Polymorphism

Post icon  Posted 27 January 2010 - 05:36 PM

Hi all
I have a question that relates more to OO theory rather than C# specifically. What I want to know is that if I have created a parent class and the child class should the child class override the parent's methods or should it use the parent's methods. I will post my 2 classes below as I know what I'm trying to say, but I don't think I'm articulating it correctly.

Parent Class
public abstract class AppFormData
{
	//Variables
	private String errorMsg;
	private int fieldErrorNum;
	

	//No argument constructor
	public AppFormData()
	{
		
	}

	//Getters and setters
	protected void setErrorMsg(String msg)
	{
		errorMsg = msg;
	}
	private String getErrorMsg()
	{
		return errorMsg;
	}
	protected void setFieldErrorNum(int num)
	{
		fieldErrorNum = num;
	}
	private int getFieldErrorNum()
	{
		return fieldErrorNum;
	}


	/// <summary>
	/// Returns an error message that has been set by the checkPage()
	/// method.
	/// </summary>
	/// <returns></returns>
	public String getErrorMessage()
	{
		return getErrorMsg();
	}


	/// <summary>
	/// Returns an integer indicating at which input field an
	/// error occurred.Integers from 0. Eg if the error occurred
	/// at the first input field, this method would return 0, if
	/// the error occurred at the 3rd input field, this method 
	/// would return 2.
	/// </summary>
	/// <returns></returns>
	public int fieldNumWhereErrorOccurred()
	{
		return getFieldErrorNum();
	}


	/// <summary>
	/// Method that validates all of the input fields on the
	/// page to see if they are valid and conform to the 
	/// business rules. Returns true if all fields are valid
	/// and returns false if any of the fields are invalid.
	/// Triggers the setting of the errorMessage and the
	/// fieldErrorNum valiables.
	/// </summary>
	/// <returns></returns>
	public Boolean isPageDataValid()
	{
		return checkPage();
	}


	/// <summary>
	/// Protected method that needs to be written by the
	/// child class. It checks each input field by instantiating
	/// the ValidateUserInput and DateCheck classes. It also
	/// sets the errorMessage and sets the fieldErrorNum if an 
	/// error occurs. Returns true if all input fields pass
	/// validation, returns false if any input fields fail
	/// the validation.
	/// </summary>
	/// <returns></returns>
	protected abstract bool checkPage();



	//Protected method that checks if the input is
	//null or empty. If true it sets the setter to "0"
	//which is a rule of the PeopleSoft system. Else
	//it sets the setter to the field input value.
	protected String setStringData(String input)
	{
		string setter;
		input = input.Trim();
		if (input == "" || input == null)
		{
			setter = "0";
		}
		else
		{
			setter = input;
		}
		return setter;
	}
}


Child Class
public class PersonDetails1 : AppFormData
{
	//Variables
	String userTitle;
	String fName;
	String lName;
	String mName;
	String prevName;
	String dob;
	String gender;
	String prevSID;
	ValidateUserInput userData = new ValidateUserInput();
	DateCheck birthday = new DateCheck();

	//Constructor
	public PersonDetails1(String title, String firstName, String middleName, String lastName, 
		String previousName, String dateBirth, String gender, String previousStudentID)
	{
		setUserTitle(title);
		setFName(firstName);
		setMName(middleName);
		setLName(lastName);
		setPrevName(previousName);
		setDob(dateBirth);
		setGender(gender);
		setPrevSID(previousStudentID);

	}

	//Setters and Getters
	private void setUserTitle(String title)
	{
		userTitle = title;
	}
	private String getUserTitle()
	{
		return userTitle;
	}
	private void setFName(String firstName)
	{
		fName = setStringData(firstName);
	}
	private String getFName()
	{
		return fName;
	}
	private void setLName(String lastName)
	{
		lName = setStringData(lastName);
	}
	private String getLName()
	{
		return lName;
	}
	private void setMName(String middleName)
	{
		mName = setStringData(middleName);
	}
	private String getMName()
	{
		return mName;
	}
	private void setPrevName(String pName)
	{
		prevName = setStringData(pName);
	}
	private String getPrevName()
	{
		return prevName;
	}
	private void setDob(String dateBirth)
	{
	   dob = setStringData(dateBirth);
	}
	private String getDob()
	{
		return dob;
	}
	private void setGender(String sex)
	{
		gender = setStringData( sex);
	}
	private String getGender()
	{
		return gender;
	}
	private void setPrevSID(String sid)
	{
		prevSID = setStringData( sid);
	}
	private String getPrevSID()
	{
		return prevSID;
	}
	
	
	//Methods

	
	/// <summary>
	/// returns any error messages received from the validation process 
	/// 
	/// </summary>
	/// <returns></returns>
	public String getErrorMessage()
	{
		return getErrorMsg();
	}

	/// <summary>
	/// Returns an integer from 0 to 7 indicating in order which input field
	/// caused the error. eg if Title caused the error 0 would be returned,
	/// if Date of Birth caused the error, 5 would be returned
	/// </summary>
	/// <returns></returns>
	public int fieldNumWhereFirstErrorOccurred()
	{
		return getFieldErrorNum();
	}

	/// <summary>
	/// returns a boolean value indicating if the data entered 
	 ///is valid data.Returns true if the page contains valid data or false
	 ///if the data is invalid.
	/// </summary>
	 ///<returns></returns>
	public Boolean isPageDataValid()
	{
		return (checkPage());
	}

	//Helper methods

	
	//Provides the checking and validation for each page field.
	//Overridden abstract method from Parent, AppFormData.
	private override Boolean checkPage()
	{
		bool valid = true;
		if (!(userData.isXMLDropDownValid(getUserTitle(), "TestTitle")))
		{
			valid = false;
			setErrorMsg("Please select your title eg Mr, Mrs");
			setFieldErrorNum(0);
			
		}
		if (valid == true)
		{
			if (!(userData.isStringSafe(getFName())))
			{
				valid = false;
				setErrorMsg("Please enter your first name");
				setFieldErrorNum(1);
				
			}
		}
		if (valid == true)
		{
			if (getMName() != "0")
			{
				if (!(userData.isStringSafe(getMName())))
				{
					valid = false;
					setErrorMsg("Please enter your middle name or leave the field blank if you " +
								"do not have a middle name");
					setFieldErrorNum(2);
					
				}
			}
		}
		if (valid == true)
		{
			if (!(userData.isStringSafe(getLName())))
			{
				valid = false;
				setErrorMsg("Please enter your last name");
				setFieldErrorNum(3);
				
			}
		}
		if (valid == true)
		{
			if (getPrevName() != "0")
			{
				if (!(userData.isStringSafe(getPrevName())))
				{
					valid = false;
					setErrorMsg("Please enter your previous or maiden name or leave the field blank " +
								"if you do not have a middle name");
					setFieldErrorNum(4);
					
				}
			}
		}
		if (valid == true)
		{
			if (!(userData.isStringSafe(getDob())))
			{
				valid = false;
				setErrorMsg("Please select your date of birth from the calendar");
				setFieldErrorNum(5);
				
			}
		}
		if (valid == true)
		{
			if (!(birthday.checkOver15(getDob()) > 0))
			{
				valid = false;
				setErrorMsg(birthday.getErrorMessage());
				setFieldErrorNum(5);

			}
		}
		if (valid == true)
		{
			if (!(userData.isRadioButtonValid(getGender())))
			{
				valid = false;
				setErrorMsg(birthday.getErrorMessage());
				setFieldErrorNum(6);
			}
		}
		if (valid == true)
		{
			if (getPrevSID() != "0")
			{
				if (!(userData.isStringSafe(getPrevSID())))
				{
					valid = false;
					setErrorMsg("Please enter your previous student number. If you do not remember your " +
								"student number please leave this field blank");
					setFieldErrorNum(7);
				}
			}
		}
		return valid;
	}
	
}


I guess what I'm trying to ask is should I use the isPageDataValid() method from the Parent class or do I write it again and use it from the Child class. I'm just a little confused.

Cheers
Bec

This post has been edited by becstribe: 27 January 2010 - 06:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: [Solved] Inheritance and Polymorphism

#2 knichols  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 21
  • Joined: 09-November 09

Re: [Solved] Inheritance and Polymorphism

Posted 27 January 2010 - 05:45 PM

I didn't look over the entire code snippet, but imo the ispagedatavalid() method should not be overridden in the child class. ispagedatavalid is calling the abstract checkpage method which will be overridden. You could probably get rid of the ispagedatavalid altogether and call checkpage directly.
Was This Post Helpful? 0
  • +
  • -

#3 becstribe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 09-April 09

Re: [Solved] Inheritance and Polymorphism

Posted 27 January 2010 - 05:50 PM

Thanks :^: so with the other methods like getMessage, I should leave that in the parent class and not override it in the child class? I hope I'm understanding this correctly LOL

Cheers
Bec
Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: [Solved] Inheritance and Polymorphism

Posted 27 January 2010 - 05:52 PM

Generally speaking you want a parent class to have methods the child classes share in common.

For example, an Animal class would have the MakeNoise(); method. When you create your Dog and Cat class, they will have to override that inherited method to make it their own, so Dog.MakeNoise('Bark'); and Cat.MakeNoise('Meow');

You want to override inherited methods when the child class has to do something special with the inherited method.

If you have any more questions, just ask away. :)
Was This Post Helpful? 0
  • +
  • -

#5 becstribe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 09-April 09

Re: [Solved] Inheritance and Polymorphism

Posted 27 January 2010 - 05:59 PM

So essentially, if the child class doen't need to do anyting extra or special with the method, then it should just use the parent's methods. Then if the child needs to do something special as with your dog and cat examples above, should the parent method be an abstract method? Or can it be a method in it's own right like for example the toString method that is inherited from the Object class?

Thanks for your help
Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: [Solved] Inheritance and Polymorphism

Posted 27 January 2010 - 06:01 PM

That's the general idea. :)
Was This Post Helpful? 1
  • +
  • -

#7 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: [Solved] Inheritance and Polymorphism

Posted 27 January 2010 - 06:09 PM

Quote

Then if the child needs to do something special as with your dog and cat examples above, should the parent method be an abstract method?


If the child needs to do something different than the parent but the parent DOES do something, that method cannot be abstract. Abstract methods have no implementation and must be implemented by the child.

If the parent has a method that does something that might change in context to its usage in derived classes, consider making the method virtual so that it may be overridden and the implementation can be changed.
Was This Post Helpful? 1
  • +
  • -

#8 becstribe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 09-April 09

Re: [Solved] Inheritance and Polymorphism

Posted 27 January 2010 - 06:19 PM

So making the Parent's method that does something, Virtual means that it CAN be overridden, not that it MUST be overridden right.

Thank you all so much for your help, sometimes just asking the questions help clarify it for me.
Was This Post Helpful? 0
  • +
  • -

#9 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: [Solved] Inheritance and Polymorphism

Posted 27 January 2010 - 06:22 PM

No problem.

Mark the thread as solved so people know where to look for info in the future. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1