Inheritance
SurveyBuilder Corner
In SurveyBuilder, our question and answer application, we make use of inheritance to build up our supporting types.
We define a few base classes, such as Question and Answer:
public abstract class Question { public string Label { get; set; } public Answer Ask() { PrintQuestion(); while (true) { var input = Console.ReadLine(); string errorMessage; if (!ValidateInput(input, out errorMessage)) { Console.WriteLine(errorMessage); Console.WriteLine("Please, correct your input."); continue; } return CreateAnswer(input); } } protected virtual void PrintQuestion() { Console.WriteLine(Label); } protected abstract bool ValidateInput(string input, out string errorMessage); protected abstract Answer CreateAnswer(string validInput); }
SurveyBuilder supports a number of different Question and Answer types, including the TextQuestion:
public class TextQuestion : Question { private const int MaxUserInputTextLength = 128; protected override bool ValidateInput(string input, out string errorMessage) { if (input.Length > MaxUserInputTextLength) { errorMessage = string.Format("Input text is too long. Expected {0} or less characters.", MaxUserInputTextLength; return false; } errorMessage = null; return true; } protected override Answer CreateAnswer(string validInput) { return new TextAnswer { Text = validInput, Question = this }; } }
Inheritance allows us to build quite complex applications using terse, robust code.
Page 16 of 18