Inheritance and Polymorphism

Two Important Principles of OOP

Topics

  • What is Inheritance?
  • What is Polymorphism?
  • Why are these principles essential to Object-Oriented Programming?

Inheritance

  • In object-oriented programing, inheritance is a feature that demonstrates the “IS A” relationship between different classes.
  • For example, a TIGER is a CAT.
  • Inheritance allows a class to have the same states and behaviors as another class.
  • Inheritance also allows derived classes to customize that behavior for the sake of specificity.
  • For example, all cats EAT, but tigers EAT a certain way.

Example of a Base Class

public class Shape
{
    private int sides;

    public Shape()
    {  }

    public Shape(int sides)
    {
        this.sides = sides;
    }

    public int Sides
    {
        get { return this.sides; }
        set { this.sides = value; }
    }
}

Derived Classes

  • A derived class (also called a child class or a subclass) is any class that inherits from another class.
  • To create a derived class, include a colon and the name of the base class.
//Example:
public class ChildClass : ParentClass

Example of Derived Class

public class Rectangle : Shape
{
    private double area;

    public Rectangle(double area)
    {
        this.area = area;
    }

    public double Area
    {
        get { return this.area; }
        set { this.area = value; }
    }
}

Your Turn!

  • What are some examples of base classes?
  • What are some examples of classes that could be derived from those base classes?

Members of a Derived Class

  • In OOP, the states and behaviors of a class are, all together, called the “members” of that class.
  • Let’s talk about what makes the members of a derived class different from the members of a base class.

Constructors

  • A derived class does NOT inherit the constructors of the base class.
  • However, the constructors of the base class can still be accessed using the “base” keyword.
public ChildClass(int num) : base(string)
{
        // body of constructor
}

Base Constructor Example

public class Rectangle : Shape
{
    private double area;

    public Rectangle(double area) : base(4)
    {
        this.area = area;
    }

    public double Area
    {
        get { return this.area; }
        set { this.area = value; }
    }
}
  • By including the base(4), the public Shape(int sides) is included and sets sides equal to 4.

Access Modifiers in Derived Classes

Let’s review access modifiers:

public - can be accessed from every class

private - cannot be accessed from any other class (default for elements of a class)

protected - can be accessed from its class and all its derived classes

internal - can only be accessed from the same assembly (default for classes) → More later.


Access Modifiers Example

public class Rectangle : Shape
{
    private double area;

    public Rectangle(double area)
    {
        this.Sides = 4; //Why can't we use this.sides here?
        this.area = area;
    }

    // ...
}

NOTICE: We use the property Sides in the Rectangle class because the field sides is set to private in the Shape class.

If we had used the protected access modifier for the sides field instead of private, we could have used the sides field instead.


PRACTICE!!

In Visual Studio, open a new project and create a base class and 2 classes that are derived from that base class.

  • Add two Properties to your base class.
  • Add a constructor to your derived class that invokes your base class's constructor.
  • Add two methods to your derived classes.
  • Add a method to your base class and call it in both of your derived classes.

Polymorphism

  • Polymorphism is the ability to redefine methods for derived classes.
  • The Greek roots - “poly” for many and “morph” for form - tells us that this principle allows us to have one method take many forms.
  • In order to incorporate polymorphism in C#, we need to use the virtual and override keywords.

“virtual” keyword

public virtual void Clean()
{
	// body of the method
}

In order to give the derived classes permission to create their own version of a method, the keyword virtual must be included in the method declaration in the base class.


Example

class Appliance
{
	public virtual void Clean()
	{
		// general cleaning instructions
	}
}

“override” keyword

public override void Clean()
{
	// body of the method
}

When implementing a new version of the method in the derived class, add the keyword override.


Override Examples

class Dishwasher : Appliance
{
	public override void Clean()
	{
	   //specific instructions for how a dishwasher cleans
	}
}
class WashingMachine : Appliance
{
	public override void Clean()
	{
	   //specific instructions for how a washing machine cleans
	}
}

PRACTICE!!

Create a new base class.

  • Create two derived classes.
  • Create a virtual method in the base class.
  • Create an override method in each derived class.

Abstract Classes

What is an abstract class?

  • An abstract class should be used when instances will be created from derived classes only.
  • An abstract class can not be instantiated.
  • Abstract classes can have abstract, virtual, and normal methods.

“abstract” keyword

Abstract Classes:

  • Use the abstract keyword to denote a class that will not be instantiated.

Abstract Methods:

  • Use the abstract keyword to denote a method that will not be implemented in the base class, but must be implemented in the derived class(es).
  • Abstract methods can only be placed inside of abstract classes.

Something peculiar about Abstract classes

An abstract method does NOT need a body.

In fact, abstract methods usually do NOT have a body.


Example of Abstract Class

public abstract class Appliances
{
	public abstract void Clean();
	// an abstract method does not
	// have a body
}

NOTE: An appliance is a good example of an abstract class, because you would never just create an “Appliance” object, you would create a type of appliance, like a “Toaster”.


Class Derived from Abstract Class

class Dishwasher : Appliances
{
	public override void Clean()
	{
	   //in the derived class, use the override keyword to rewrite
	   //an abstract method from the base class
	}
}

Let me repeat that ... we use the OVERRIDE keyword in the derived class with abstract methods from the base class.


PRACTICE!!

Create an abstract Furniture class.

  • Create fields and/or Properties in the Furniture class that relate to all furniture types.
  • Create an abstract method in your Furniture class.
  • Create three derived classes of types of furniture.
  • Each type of furniture should have three fields and/or Properties.
  • Each type of furniture should have its own, non-override method.
  • Each type of furniture should also have a method that overrides the abstract method in the Furniture class.
  • In your Program class, instantiate an object of each type of furniture.
  • Use all methods from all derived classes inside of your Program class.