Inheritance Inheritance is a fundamental feature of an object-oriented system, and it is simply the ability to inherit data and functionality from a parent object. Rather than developing new objects from scratch, The parent object that the new work is based upon is known as a base class, and the child object is known as a derived class. Inheritance gets a lot of attention in explanations of object-oriented design, but the use of inheritance isn’t particularly widespread in most designs. There are several reasons for this. - 14 - First, inheritance is an example of what is known in object-oriented design as an “is-a” relationship. If a system has an animal object and a cat object, the cat object could inherit from the animal object, because a cat "is-a" animal. In inheritance, the base class is always more generalized than the derived class. The cat class would inherit the eat function from the animal class, and would have an enhanced sleep function. In real-world design, such relationships aren’t particularly common. Second, to use inheritance, the base class needs to be designed with inheritance in mind. This is important for several reasons. If the objects don’t have the proper structure, inheritance can’t really work well. More importantly, a design that enables inheritance also makes it clear that the author of the base class is willing to support other classes inheriting from the class. If a new class is inherited from a class where this isn’t the case, the base class might at some point change, breaking the derived class. Some less-experienced programmers mistakenly believe that inheritance is “supposed to be” used widely in object-oriented programming, and therefore use it far too often. Inheritance should only be used when the advantages that it brings are needed
The following class implements an Engineer and methods to handle billing for that Engineer. using System; class Engineer { // constructor public Engineer(string name, float billingRate) { this.name = name; this.billingRate = billingRate; } // figure out the charge based on engineer's rate public float CalculateCharge(float hours) { - 40 - return(hours * billingRate); } // return the name of this type public string TypeName() { return("Engineer"); } private string name; protected float billingRate; } class Test { public static void Main() { Engineer engineer = new Engineer("Hank", 21.20F); Console.WriteLine("Name is: {0}", engineer.TypeName()); } } Engineer will serve as a base class for this scenario. It contains the private field name, and the protected field billingRate. The protected modifier grants the same access as private, except that classes that are derived from this class also have access to the field. Protected is therefore used to give classes that derive from this class access to a field. Protected access allows other classes to depend upon the internal implementation of the class, and therefore should be granted only when necessary. In the example, the billingRate member can’t be renamed, since derived classes may access it. It is often a better design choice to use a protected property. The Engineer class also has a member function that can be used to calculate the charge based on the number of hours of work done.
AttachmentsEasiest way of understanding Base Classes And Inheritance ()
|
No responses found. Be the first to respond and make money from revenue sharing program.
|