Tuesday, February 16, 2016

Lesson 6


Outline

Chapter 11


Inheritance Continued


Diagraming
Is-A relationship
Constructors
Protected

Problem 11.5
(Shape Inheritance Hierarchy) The world of shapes is much richer than the shapes included in the inheritance hierarchy of Fig. 11.3. Write down all the shapes you can think of—both two-dimensional and three-dimensional—and form them into a more complete Shape hierarchy with as many levels as possible. Your hierarchy should have class Shape at the top. Class TwoDimensionalShape and class ThreeDimensionalShape should extend Shape. Add additional derived classes, such as Quadrilateral and Sphere, at their correct locations in the hierarchy as necessary.

 
Diagraming
 
In the previous lesson, we learned about base and derived classes where the base class is the parent class and the derived class is the child class. A derived class can only have one base class. In order for a derived class to inherit from more than one base class the current base class would need to inherit from second class. However, a base class can have many derived classes.

In the example below, we have a simple diagram outlining a series of classes and their inherited relationship. The top class Person is the root base class for all the other classes. All of the other classes in the inheritance diagram have the public and internal capabilities specified in the Person class.



The Staff base class has two derived classes (Teacher and Cleaner). Both will have the same capabilities outlined in the Staff class but can also contain separate properties and methods specific to their own class. In an inheritance diagram the arrows point back to the base class.

Is-A

Is-A is a phrase used to describe inherited relationships. In the previous diagram, you can say Staff Is-A Person and Teacher Is-A Staff. This relationship only works up the inheritance hierarchy. We can say Teacher is a Staff but cannot say staff is a teacher. So all teachers are staff but not all staff are teachers.

Constructors

Earlier in the lessons we talked about class constructors and their ability to accept values passed into the class when the class is instantiated into an object.  

Suppose the staff class did not have a default constructor and only supported constructors that took one or more values?

class Staff
{
    private readonly string payGrade = string.Empty;
 
    public Staff(string payGrade)
    {
        //Update private variable to specified pay grade 
        this.payGrade = payGrade;
    }
}

Our Teacher class is derived from the Staff class.

class Teacher:Staff
{
 
}

This will cause an exception. Since Staff cannot be created without the pay grade parameter the Teacher class must have at least one constructor to support the Staff class requirement.

class Teacher:Staff
{
 
    public Teacher(string payGrade)
        : base(payGrade)
    { }
 
}

The Teacher class accepts the parameters via its constructor and then passes it to the base class (Staff) via the : base. The "base" refers to the current class base class. The Teacher class can still accept its own parameters.  

private string className = string.Empty;
 
public Teacher(string className, string payGrade)
    : base(payGrade)
{
    this.className = className;
}

In the example above, the Teacher class accepts two parameters (className and payGrade). Pay grade is passed to the base constructor and the class name is handled locally within the Teacher class. Note: the sequence of the parameters does not play a factor. It would have been acceptable to reverse the order of the parameters.

Lets extend our Teacher class to support multiple constructors.

class Teacher:Staff
{
    private string className = string.Empty;
    private string firstName = string.Empty;
    private string lastName = string.Empty;
 
    public Teacher(string className, string payGrade)
        : base(payGrade)
    {
        this.className = className;
    }
 
    public Teacher(string firstName, string lastName, string className, string payGrade): this(className, payGrade)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }
 
}

The Teacher class now supports to separate constructors to instantiate the class into a object but only one constructor references the base class. With the use of constructor chaining we can call the other Teacher constructor and pass in the values collected from this constructor. Constructor chaining is acceptable as long as ultimately the base class constructor require is satisfied.

Protected

Private and Protected are very similar. Both cannot be accessed via an instantiated object. Both can be seen by their peer methods within the same brackets "{}". However, there is one main distinction. Private variables and methods can only be seen within the class. Protected variables and methods can be referenced in the derived class.

 
class Staff
{
    private readonly string payGrade = string.Empty;
    protected int Age;
 
    public Staff(string payGrade)
    {
        //Update private variable to 
        this.payGrade = payGrade;
    }
}

class Teacher:Staff
{
    private string className = string.Empty;
    private string firstName = string.Empty;
    private string lastName = string.Empty;
 
    public Teacher(string className, string payGrade)
        : base(payGrade)
    {
        this.className = className;
    }
 
    public Teacher(string firstName, string lastName, string className, string payGrade): this(className, payGrade)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        Age = 27;
    }
}

The Age variable is accessible in the Teacher class due to the protected description. Note: Protected is only accessible in the immediate derived class. In the diagram at the top, should Person have a protected variable or method, it would be accessible in the Staff class but NOT in the Teacher or Cleaner class.









No comments:

Post a Comment