Introduction
This article introduces you to the inheritance concepts in C# and explains how to implement function overriding and shadowing in C#
Inheritance Concepts in C#
Inheritance allows you to declare a new class that retains all of the members and functionality of a previously defined class. This enables you to create classes that implement basic, common functionality, and then create specialized subclasses that serve different but related functions.
In this article I have Used a Class called Car which contains the following Structure
public class Car { //public float speed=50; public Car() { // // TODO: Add constructor logic here // }
//this is a virtual method which may be either overriden or shadowed by child class- cielo public virtual string drive() { return " car ";//drives at "+speed + "km/hour"; } }
I have also used a Class Cielo which inherits from the class Car and either overrides a method or hides a method and provides a complete new implementation
public class Cielo:Car { //public new float speed=100; public Cielo() { // // TODO: Add constructor logic here // } public override string drive() { return " cielo " +speed+" km/hour" ; }
(or)
public new string drive() { return " cielo "; } }
Hiding Base Class Members with Visual C#
When working in C#, you can obscure a base class member and replace it with a completely new implementation. This is called hiding. When you hide a member, you replace the base class implementation with a new implementation. The new implementation must have the same signature as the member that is being hidden and be the same kind of member, but it can have a different access level, return type, and completely different implementation. Any method that has the same name as a preexisting method but a different signature is treated as an overload of that method and will not hide the preexisting method. You use the new keyword to hide a base class member as shown in this example The Below table explains the concepts we discussed so far as a tabular data with examples and the result sets
Please make appropriate changes to the class declarations to understand this example better
Concept overriding methods Remarks Base object calls base implementation and child object calls child implementation Base Class Child Class Virtual method Overridden method
Car Cielo Drive Drive
Output
Car c=new Car(); c.drive()àreturns car Cielo ci=new Cielo(); ci.drive()à returns Cielo
Concept Shadowing methods
Remarks Base object even though created of child class type still calls Base implementation and child object calls child implementation Base Class Child class Virtual method New/Shadowed method Car Cielo Drive Drive
Output
Cielo ci=new Cielo(); ci.drive());àreturns Cielo Car c=new Cielo(); c.drive();àreturns Car
Concept Shadowing fields and accessing through override method
Remarks Even though a base class reference was declared it was initialized to a child class reference so it is forced only to return the child implementation
Base class Child class Base field Car Cielo Speed=100 New /Shadowed Field accessed (through overridden method) New Speed=50
Output
Cielo ci=new Cielo(); ci.drive() //returns Cielo 100 Car c=new Cielo(); c.drive();//returns Cielo 100 {/CODE]
Concept Shadowing fields and accessing through a new/shadowed method Remarks Base object calls base implementation even though containing a child reference Exhibits real polymorphism
Base class Car Child class Cielo Base field Speed =50 Child field New speed=100 Base Virtual method drive() Child shadowed /new method New drive() Output Cielo ci=new Cielo(); ci.drive();àreturns “Cielo drives at 100” Car c=new Cielo(); c.drive(); àreturns “Car drives at 50”
Concept shadow methods alone no fields and access a shadowed method Remarks Base object calls base implementation even though containing a child reference Does not exhibit polymorphism we get undesired results Base Class Car Child Class Cielo Base virtual method Drive Child new method Drive Output Car c=new Car(); c.drive());àreturns car c=new Cielo(); c.drive(); àstill returns car
Summary • You can create classes that combine all of the functionality of a previously defined class with new specialized functionality through inheritance • Inherited classes contain all of the members of their base class, and instances of an inherited class can polymorphically act as instances of their base class. • You can add additional members to your inherited class to provide custom functionality. You can also provide new implementation for existing members. There are two ways to do this: o Overriding members (Using the virtual and overrides keyword) o Hiding members (Using new keyword )
|
No responses found. Be the first to respond and make money from revenue sharing program.
|