Inheritance
Volley Ball Football Basketball Handball Golf The primary characteristic of the objects on the above pictures is that they are balls used in different…
Introduction to Inheritance#
Definition#
Volley Ball Football Basketball Handball Golf The primary characteristic of the objects on the above pictures is that they are balls used in different sports. Another characteristic they share is that they are round. On the other hand, although these balls are used in sport, one made for one sport cannot (or should not) be used in another sport (of course, it is not unusual for a footballer to mess with a basketball on a lawn but it is not appropriate). The common characteristics of these objects can be listed in a group like a C# class. The class would appear as:
class Ball
{
TypeOfSport;
Size;
}If you were asked to create a class to represent these balls, you may be tempted to implement a general class that defines each ball. This may be a bad idea because, despite their round resemblance, there are many internal differences among these balls. Programming languages like C# provide an alternate solution to this type of situation. Inheritance consists of creating a class whose primary definition or behavior is based on another class. In other words, inheritance starts by having a class that can provide behavior that other classes can improve on. Practical Learning: Introducing Inheritance
- Start Microsoft Visual Web Developer
- To create a new web site, on the main menu, click File -> New Web Site...
- Make sure the Language is set to Visual C#. Click the second box to the right of Location, delete the suggested name of the web site and replace it with geometry8
- Click OK
- In the Solution Explorer, right-click Default.aspx and click Rename
- Type index.aspx and press Enter
- On the main menu, click Website -> Add New Item...
- In the Templates list, click Class
- Replace the suggested name with Circle
- Click Add
- Read the message box that comes up and click Yes
- Change the class as follows: using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for Circle /// </summary> public class Circle { public double rad; public Circle() { this.rad = 0.00; } public Circle(double radius) { this.rad = radius; } }
- Save the file
Class Derivation#
As you may have guessed, in order to implement inheritance, you must first have a class that provides the fundamental definition or behavior you need. There is nothing magical about such a class. It could appear exactly like any of the classes we have used so far. Here is an example:
public class Circle
{
private double _radius;
public double Diameter()
{
return Radius * 2;
}
}The above class is used to process a circle. It can request or provide a radius. It can also calculate the diameter of a circle. Now, suppose you want to create a class for a sphere. You could start from scratch as we have done so far. On the other hand, since a sphere is primarily a 3-dimensional circle, and if you have a class for a circle already, you can simply create your sphere class that uses the already implemented behavior of a circle class. 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
}In this formula, 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; that is, the compiler must be able to find its definition. Based on the above formula, you can create a sphere class based on the earlier mentioned Circle class as follows:
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:
class Circle
{
private double _radius;
}
class Sphere : Circle
{
}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. That is, the child class doesn't have to "qualify" the public members of the parent class when these public members are used in the body of the derived class. Practical Learning: Deriving a Class
- On the main menu, click Website -> Add New Item...
- In the Templates list, click Class
- Replace the suggested name with Cylinder
- Click Add
- Read the message box that comes up and click Yes
- Change the class as follows: using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for Cylinder /// </summary> public class Cylinder : Circle { public double hgt; public Cylinder() { this.rad = 0.00; this.hgt = 0.00; } public Cylinder(double radius, double height) { this.rad = radius; this.hgt = height; } public double Volume() { return this.rad * this.rad * this.hgt * 3.14159; } }
- Click the index.aspx tab
- To access the method, change the code as follows: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Geometry: Circular Shapes</title> </head> <body> <% // Showing the circle var Disk = new Circle(42.86); Response.Write("=-=Circle Characteristics=-="); Response.Write("<br>Radius: "); Response.Write(Disk.rad); Response.Write("<p></p>"); // Showing the cylinder var Cup = new Cylinder(45.66, 36.38); Response.Write("=-=Cylinder Characteristics=-="); Response.Write("<br>Radius: "); Response.Write(Cup.rad); Response.Write("<br>Height: "); Response.Write(Cup.hgt); Response.Write("<br>Volume: "); Response.Write(Cup.Volume()); %> </body> </html>
- Press Ctrl + F5 to see the result
- Return to your programming environment
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 (such as a method) 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:
class Circle
{
private double _radius;
public double Diameter()
{
return Radius * 2;
}
public double Area()
{
return Radius * Radius * 3.14159;
}
}
class Sphere : Circle
{
public double Area()
{
return 4 * Radius * Radius * 3.14159;
}
}Besides customizing the methods of a parent class, you can add new members as you wish. This is another valuable feature of inheritance. In our example, while the circle is a flat shape, a sphere has a volume. In this case, you may need to calculate the volume of a sphere as a new method or property of the derived class. Here is an example:
class Circle
{
private double _radius;
public double Area()
{
return Radius * Radius * 3.14159;
}
}
class Sphere : Circle
{
public double Area()
{
return 4 * Radius * Radius * 3.14159;
}
public double Volume()
{
return 4 * 3.14159 * Radius * Radius * Radius / 3;
}
}If you create a method in a derived class and that method already exists in the parent class, when you access the 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 method of a parent class from the derived class, type the base keyword, followed by the period operator, followed by the name of the method of the base class.
The new Modifier#
Imagine you create a class used to process a circle as we saw earlier. You can use this as the base class for a sphere. Both the circle and the sphere have an area but their values are different. This means that, as mentioned in our introduction to inheritance, when deriving the sphere class, you would have to calculate a new area for the cube. 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, you may want to indicate to the compiler that, instead of overriding that same method that was defined in the base class, you want to create a brand new and independent version of that method. When doing this, you would be asking the compiler to hide the member of the base class that has the same name, when the member of the current class is invoked. To do this, type the new keyword to its left. Here is an example:
class Circle
{
private double _radius;
public double Area()
{
return Radius * Radius * 3.14159;
}
}
class Sphere : Circle
{
new public double Area()
{
return 4 * Radius * Radius * 3.14159;
}
public double Volume()
{
return 4 * 3.14159 * Radius * Radius * Radius / 3;
}
}Practical Learning: Implementing New Derived Methods
- Click the Circle.cs tab and change the document as follows: using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for Circle /// </summary> public class Circle { public double rad; public Circle() { this.rad = 0.00; } public Circle(double radius) { this.rad = radius; } public double Area() { return this.rad * this.rad * 3.14159; } }
- Click the Cylinder tab and change the document as follows: using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for Cylinder /// </summary> public class Cylinder : Circle { public double hgt; public Cylinder() { this.rad = 0.00; this.hgt = 0.00; } public Cylinder(double radius, double height) { this.rad = radius; this.hgt = height; } public new double Area() { return (2 * this.rad * this.hgt * 3.14159) + (2 * this.rad * this.rad * 3.14159); } public double Volume() { return this.rad * this.rad * this.hgt * 3.14159; } }
- Click the index.aspx tab
- To access the method, change the code as follows: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Geometry: Circular Shapes</title> </head> <body> <% // Showing the circle var Disk = new Circle(42.86); Response.Write("=-=Circle Characteristics=-="); Response.Write("<br>Radius: "); Response.Write(Disk.rad); Response.Write("<br>Area: "); Response.Write(Disk.Area()); Response.Write("<p></p>"); // Showing the cylinder var Cup = new Cylinder(45.66, 36.38); Response.Write("=-=Cylinder Characteristics=-="); Response.Write("<br>Radius: "); Response.Write(Cup.rad); Response.Write("<br>Height: "); Response.Write(Cup.hgt); Response.Write("<br>Area: "); Response.Write(Cup.Area()); Response.Write("<br>Volume: "); Response.Write(Cup.Volume()); %> </body> </html>
- Press Ctrl + F5 to see the result
- Return to your programming environment
Previous Copyright © 2008-2009 C# Key Next

