Home

Class Inheritance

 

Introduction to Inheritance

 

Introduction

Class inheritance is the ability to create a class that is based on another class. This makes it possible to create a class that improves on the characteristics of an existing class.

 

Deriving a Class

To implement inheritance, you must first have a class. There is nothing magical about such a class. Here is an example:

<%@ Page Language="C#" %>

<html>
<head>

<script runat="server">
public class Circle
{
    private double _radius;

    public double Radius
    {
	get
	{
	    return _radius;
	}
	set
	{
	    if( _radius < 0 )
		_radius = 0.00;
	    else
		_radius = value;
	}
    }

    public double Diameter
    {
	get
	{
	    return Radius * 2;
	}
    }

    public double Circumference
    {
	get
	{
	    return Diameter * 3.14159;
	}
    }
	
    public double Area
    {
	get
	{
	    return Radius * Radius * 3.14159;
	}
    }
}
</script>

<title>Exercise</title>

</head>
Circle
<body>

<%
    Circle round = new Circle();
    round.Radius = 25.55;

    Response.Write("<pre>Circle Characteristics<br />");
    Response.Write("Side:     " + round.Radius + "<br />");
    Response.Write("Diameter: " + round.Diameter + "<br />");
    Response.Write("Circumference: " + round.Circumference + "<br />");
    Response.Write("Area:     " + round.Area + "</pre>");
%>
</body>
</html>

This would produce:

Inheritance

Creating a class that is based on another class is also referred to as deriving a class from another. The first class serves as parent or base. The class that is based on another class is also referred to as child or derived. To create a class based on another, you use the following formula:

class NewChild : BaseClass
{
      // Body of the new class
}

You start with the class keyword followed by a name from your class. On the right side of the name of your class, you must type the : operator, followed by the name of the class that will serve as parent. Of course, the BaseClass class must have been defined. Here is an example:

class Sphere : Circle
{
      // The class is ready
}

After deriving a class, it becomes available and you can use it just as you would any other class. Here is an example:

<%@ Page Language="C#" %>

<html>
<head>

<script runat="server">
public class Circle
{
    private double _radius;

    public double Radius
    {
	get
	{
	    return _radius;
	}
	set
	{
	    if( _radius < 0 )
		_radius = 0.00;
	    else
		_radius = value;
	}
    }

    public double Diameter
    {
	get
	{
	    return Radius * 2;
	}
    }

    public double Circumference
    {
	get
	{
	    return Diameter * 3.14159;
	}
    }
	
    public double Area
    {
	get
	{
	    return Radius * Radius * 3.14159;
	}
    }
}

class Sphere : Circle
{
}
</script>

<title>Exercise</title>

</head>
<body>

<%
    Circle round = new Circle();
    round.Radius = 25.55;

    Response.Write("<pre>Circle Characteristics<br />");
    Response.Write("Side:     " + round.Radius + "<br />");
    Response.Write("Diameter: " + round.Diameter + "<br />");
    Response.Write("Circumference: " + round.Circumference + "<br />");
    Response.Write("Area:     " + round.Area + "</pre>");

    Sphere ball = new Sphere();
    ball.Radius = 25.55;

    Response.Write("<pre>Sphere Characteristics<br />");
    Response.Write("Side:     " + ball.Radius + "<br />");
    Response.Write("Diameter: " + ball.Diameter + "<br />");
    Response.Write("Circumference: " + ball.Circumference + "<br />");
    Response.Write("Area:     " + ball.Area + "</pre>");
%>
</body>
</html>

This would produce:

Inheritance

When a class is based on another class, all public members of the parent class are made available to the derived class that can use them as easily. While other methods and classes can also use the public members of a class, the difference is that the derived class can call the public members of the parent as if they belonged to the derived class.

Implementation of Derived Members 

 
 
 

Inheritance is used to solve various Object-Oriented Programming (OOP) problems. One of them consists of customizing, adapting, or improving the behavior of a feature (property or method, etc) of the parent class. For example, although both the circle and the sphere have an area, their areas are not the same. A circle is a flat surface but a sphere is a volume, which makes its area very much higher. Since they use different formulas for their respective area, you should implement a new version of the area in the sphere. Based on this, when deriving your class from another class, you should be aware of the properties and methods of the base class so that, if you know that the parent class has a certain behavior or a characteristic that is not conform to the new derived class, you can do something about that. A new version of the area in the sphere can be calculated as follows:

 
 
<%@ Page Language="C#" %>

<html>
<head>

<script runat="server">
public class Circle
{
    private double _radius;

    public double Radius
    {
	get
	{
	    return _radius;
	}
	set
	{
	    if( _radius < 0 )
		_radius = 0.00;
	    else
		_radius = value;
	}
    }

    public double Diameter
    {
	get
	{
	    return Radius * 2;
	}
    }

    public double Circumference
    {
	get
	{
	    return Diameter * 3.14159;
	}
    }
	
    public double Area
    {
	get
	{
	    return Radius * Radius * 3.14159;
	}
    }
}

class Sphere : Circle
{
    public double  Area
    {
	get
	{
	    return 4 * Radius * Radius * 3.14159;
	}
    }
}
</script>

<title>Exercise</title>

</head>
<body>

<%
    Circle round = new Circle();
    round.Radius = 25.55;

    Response.Write("<pre>Circle Characteristics<br />");
    Response.Write("Side:     " + round.Radius + "<br />");
    Response.Write("Diameter: " + round.Diameter + "<br />");
    Response.Write("Circumference: " + round.Circumference + "<br />");
    Response.Write("Area:     " + round.Area + "</pre>");

    Sphere ball = new Sphere();
    ball.Radius = 25.55;

    Response.Write("<pre>Sphere Characteristics<br />");
    Response.Write("Side:     " + ball.Radius + "<br />");
    Response.Write("Diameter: " + ball.Diameter + "<br />");
    Response.Write("Circumference: " + ball.Circumference + "<br />");
    Response.Write("Area:     " + ball.Area + "</pre>");
%>
</body>
</html>

This would produce:

Inheritance

Notice that, this time, the areas of both figures are not the same even though their radii are similar.

Besides customizing member variables and methods of a parent class, you can add new members as you wish. This is another valuable feature of inheritance. Here is an example:

<%@ Page Language="C#" %>

<html>
<head>

<script runat="server">
public class Circle
{
    private double _radius;

    public double Radius
    {
	get
	{
	    return _radius;
	}
	set
	{
	    if( _radius < 0 )
		_radius = 0.00;
	    else
		_radius = value;
	}
    }

    public double Diameter
    {
	get
	{
	    return Radius * 2;
	}
    }

    public double Circumference
    {
	get
	{
	    return Diameter * 3.14159;
	}
    }
	
    public double Area
    {
	get
	{
	    return Radius * Radius * 3.14159;
	}
    }
}

class Sphere : Circle
{
    public double  Area
    {
	get
	{
	    return 4 * Radius * Radius * 3.14159;
	}
    }

    public double Volume
    {
        get
        {
            return 4 * 3.14159 * Radius * Radius * Radius / 3;
        }
    }
}
</script>

<title>Exercise</title>

</head>
<body>

<%
    Circle round = new Circle();
    round.Radius = 25.55;

    Response.Write("<pre>Circle Characteristics<br />");
    Response.Write("Side:     " + round.Radius + "<br />");
    Response.Write("Diameter: " + round.Diameter + "<br />");
    Response.Write("Circumference: " + round.Circumference + "<br />");
    Response.Write("Area:     " + round.Area + "</pre>");

    Sphere ball = new Sphere();
    ball.Radius = 25.55;

    Response.Write("<pre>Sphere Characteristics<br />");
    Response.Write("Side:     " + ball.Radius + "<br />");
    Response.Write("Diameter: " + ball.Diameter + "<br />");
    Response.Write("Circumference: " + ball.Circumference + "<br />");
    Response.Write("Area:     " + ball.Area + "<br />");
    Response.Write("Volume:   " + ball.Volume + "</pre>");
%>
</body>
</html>

This would produce:

Inheritance

If you create a property or method in a derived class and that property or method already exists in the parent class, when you access the property or method in the derived class, you must make sure you indicate what member you are accessing. To make this possible, the C# language provides the base keyword. To access a property or method of a parent class from the derived class, type the base keyword, followed by the period operator, followed by the name of the property or method of the base class.

The new Modifier

If you create or declare a new member in a derived class and that member has the same name as a member of the base class, when creating the new member, type the new keyword to its left. Here is an example:

<script runat="server">
public class Circle
{
    private double _radius;

    public double Radius
    {
	get
	{
	    return _radius;
	}
	set
	{
	    if( _radius < 0 )
		_radius = 0.00;
	    else
		_radius = value;
	}
    }

    public double Diameter
    {
	get
	{
	    return Radius * 2;
	}
    }

    public double Circumference
    {
	get
	{
	    return Diameter * 3.14159;
	}
    }
	
    public double Area
    {
	get
	{
	    return Radius * Radius * 3.14159;
	}
    }
}

class Sphere : Circle
{
    new public double  Area
    {
	get
	{
	    return 4 * Radius * Radius * 3.14159;
	}
    }

    public double Volume
    {
        get
        {
            return 4 * 3.14159 * Radius * Radius * Radius / 3;
        }
    }
}
</script>
 

 

   
 

Previous Copyright © 2009-2015, FunctionX, Inc. Next