Inheritance |
|
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. |
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:
class Exercise { public static int Main() { var Round = new Circle(); Round.Radius = 25.55; Console.WriteLine("Circle Characteristics"); Console.WriteLine("Side: {0}", Round.Radius); Console.WriteLine("Diameter: {0}", Round.Diameter); Console.WriteLine("Circumference: {0}", Round.Circumference); Console.WriteLine("Area: {0}", Round.Area); return 0; } } This would produce: Circle Characteristics Side: 25.55 Diameter: 51.1 Circumference: 160.535249 Area: 2050.837805975 Press any key to continue The above class is used to process a circle. It can request or provide a radius. It can also calculate the circumference and the area 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: using System; class Circle { private double _radius; public double Radius { get { if( _radius < 0 ) return 0.00; else return _radius; } set { _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 { } class Exercise { public static int Main() { var Round = new Circle(); Round.Radius = 25.55; Console.WriteLine("Circle Characteristics"); Console.WriteLine("Side: {0}", Round.Radius); Console.WriteLine("Diameter: {0}", Round.Diameter); Console.WriteLine("Circumference: {0}", Round.Circumference); Console.WriteLine("Area: {0}", Round.Area); var Ball = new Sphere(); Ball.Radius = 25.55; Console.WriteLine("\nSphere Characteristics"); Console.WriteLine("Side: {0}", Ball.Radius); Console.WriteLine("Diameter: {0}", Ball.Diameter); Console.WriteLine("Circumference: {0}", Ball.Circumference); Console.WriteLine("Area: {0}", Ball.Area); return 0; } } This would produce: Circle Characteristics Side: 25.55 Diameter: 51.1 Circumference: 160.535249 Area: 2050.837805975 Sphere Characteristics Side: 25.55 Diameter: 51.1 Circumference: 160.535249 Area: 2050.837805975 Press any key to continue When a class is based on another class, all public (we will also introduce another inheritance-oriented keyword for this issue) 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. This is illustrated in the following program: using System; class Circle { private double _radius; public double Radius { get { if( _radius < 0 ) return 0.00; else return _radius; } set { _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; } } public void ShowCharacteristics() { Console.WriteLine("Circle Characteristics"); Console.WriteLine("Side: {0}", Radius); Console.WriteLine("Diameter: {0}", Diameter); Console.WriteLine("Circumference: {0}", Circumference); Console.WriteLine("Area: {0}", Area); } } class Sphere : Circle { public void ShowCharacteristics() { // Because Sphere is based on Circle, you can access // any public member(s) of Circle without qualifying it(them) Console.WriteLine("\nSphere Characteristics"); Console.WriteLine("Side: {0}", Radius); Console.WriteLine("Diameter: {0}", Diameter); Console.WriteLine("Circumference: {0}", Circumference); Console.WriteLine("Area: {0}\n", Area); } } class Exercise { public static int Main() { var Round = new Circle(); Round.Radius = 25.55; Round.ShowCharacteristics(); var Ball = new Sphere(); Ball.Radius = 25.55; Ball.ShowCharacteristics(); return 0; } } This would produce the same result. |
|
||
Previous | Copyright © 2008-2016, FunctionX, Inc. | Next |
|