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: 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: 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. 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-2016, FunctionX, Inc. | Next |
|