xamarin: inheritance and polymorphism
TRANSCRIPT
Inheritance & Polymorphism
Eng Teong CheahMicrosoft MVP Windows Development
Inheritance
InheritanceC# is an object oriented programming language so it supports one key feature of OOPS;
Inheritance.
In general, Inheritance can be explained as acquiring the properties of the parent.
For this, we will use the most common example of the car class.
You can see Car is an Object who has basic feature like name, height, no of wheels. It has two
specialized version one is sedan and other is hatchback. Both sedan and hatch back has all the
basic feature of car and some specialized property of their own. This feature is known as
inheritance.
InheritanceWhen a child class has all the features
of base class plus its own specialized
properties. Then this phenomenon is
known as inheritance.
InheritanceTypes of inheritance supported by C# are follows:
- Single inheritance
Single base class and single derived class.
- Multilevel inheritance
B derive from A and C derive from B.
- Hierarchical inheritance
B and C both derived from A.
- Multiple inheritance using Interfaces
Class D derived from Interface A, Interface B and Class C.
InheritancePreventing a Class from Being Inherited C#
We can prevent the class from Being Inherited by Other Class by putting Sealed Keyword in class.
See Example Below:-
sealed class stringHandler{
public string inputString { get; set; }public string OutputString { get; set; }
public stringHandler(string inputstring){
this.inputString = inputString;}public String FormatString(){
// Some Code here}
}
InheritanceConstructor initialization C# Inheritance
See the car example on top of this page, if we create an object of HatchBack class . The
constructor initialization series is given below –
HatchBack hb = new HatchBack("hatchback",4,125,40,true);
1. First Car constructor is initialized
public Car(string name, int wheelcount, float length, float breadth){
this.Name = name;this.WheelCount = wheelcount;this.length = length;this.breadth = breadth;
}
Inheritance2. Second Hatchback Constructor will be initialized
public HatchBack(string name, int wheelcount, float length, float breadth, bool isconvertible) : base(name, wheelcount, length, breadth){
this.IsConvertible = isconvertible;}
So, from these examples we can say that constructor initialization in inheritance will always follow
from base to derived downwards.
InheritanceCalling Base class’s Constructor in Inherited C#
Classes :
Base class constructor is called in derived class using
base Keyword. See Car example on top of the
page Calling a Base class’s Hidden members in
inherited class C# :
Base class hidden member function can be called by
using base.functionname syntax.
See Example below -
InheritanceType Casting in Inheritance C#
Parent class reference can hold child class reference but
opposite is not true. Casting an object to a parent class
is called upcasting. Casting an object to child class is
called downcasting.
See Example below -
We have Draw() method both in base class and derived
class. But the see which one is called when.
If the Shape class’s Draw() Method is declared as virtual
and overridden in Oval class’s Draw() method then result
would be different. Result would be.
Output when Draw() method will be declared as Virtual
and overridden in derived class :Shape DrawnOval DrawnOval Drawn
Polymorphism
PolymorphismPolymorphism is one of the key features of OOPS. Polymorphism can be broken as below.
Polymorphism =Poly + morphism =Multiple + forms.
Polymorphism is simply calling derived class methods from base class reference during runtime. It
is the behavioural change of methods depending upon the instance called at runtime.
PolymorphismBefore we understand it in polymorphism in C# language, let’s understand it with a simple fantasy
example.
A mermaid, yes a mermaid. Viola……But how a mermaid is related to Polymorphism see image
below.
Mermaid shows a polymorphic
behavior according to
environment.
Mermaid behaves like human
while on earth and as a fish
while in water.
Example Class HierarchyLet's assume the following simple class hierarchy with classes A, B and C for the discussions in this
text. A is the super- or base class, B is derived from A and C is derived from class B. In some of the
easier examples, we will only refer to a part of this class hierarchy.
Inherited MethodsA method Foo() which is declared in the base class A and not redeclared in classes B or C is
inherited in the two subclasses
Inherited MethodsThe method Foo() can be overridden in classes B and C:
Inherited MethodsThere are two problems with this code.
- The output is not really what we, say from Java, expected. The method Foo() is a non-virtual
method. C# requires the use of the keyword virtual in order for a method to actually be virtual. An
example using virtual methods and polymorphism will be given in the next section.
- Although the code compiles and runs, the compiler produces a warning:
...\polymorphism.cs(11,15): warning CS0108: The keyword new is required on
'Polymorphism.B.Foo()' because it hides inherited member 'Polymorphism.A.Foo()'
This issue will be discussed in section Hiding and Overriding Methods.
Virtual and Overridden MethodsOnly if a method is declared virtual, derived classes can override this method if they are explicitly
declared to override the virtual base class method with the override keyword.
Method HidingWhy did the compiler in the second listing generate a warning? Because C# not only supports
method overriding, but also method hiding. Simply put, if a method is not overriding the derived
method, it is hiding it. A hiding method has to be declared using the new keyword. The correct
class definition in the second listing is thus:
Combining Method Overriding and HidingMethods of a derived class can both be virtual and at the same time hide the derived method. In
order to declare such a method, both keywords virtual and new have to be used in the method
declaration:
Combining Method Overriding and HidingA class C can now declare a method Foo() that either overrides or hides Foo() from class B:
Demo
Resources• MSDN
• Techoschool