xamarin: inheritance and polymorphism

23
Inheritance & Polymorphism Eng Teong Cheah Microsoft MVP Windows Development

Upload: eng-teong-cheah

Post on 08-Jan-2017

182 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Xamarin: Inheritance and Polymorphism

Inheritance & Polymorphism

Eng Teong CheahMicrosoft MVP Windows Development

Page 2: Xamarin: Inheritance and Polymorphism

Inheritance

Page 3: Xamarin: Inheritance and Polymorphism

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.

Page 4: Xamarin: Inheritance and Polymorphism

InheritanceWhen a child class has all the features

of base class plus its own specialized

properties. Then this phenomenon is

known as inheritance.

Page 5: Xamarin: Inheritance and Polymorphism

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.

Page 6: Xamarin: Inheritance and Polymorphism

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}

}

Page 7: Xamarin: Inheritance and Polymorphism

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;

}

Page 8: Xamarin: Inheritance and Polymorphism

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.

Page 9: Xamarin: Inheritance and Polymorphism

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 -

Page 10: Xamarin: Inheritance and Polymorphism

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

Page 11: Xamarin: Inheritance and Polymorphism

Polymorphism

Page 12: Xamarin: Inheritance and 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.

Page 13: Xamarin: Inheritance and Polymorphism

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.

Page 14: Xamarin: Inheritance and Polymorphism

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.

Page 15: Xamarin: Inheritance and Polymorphism

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

Page 16: Xamarin: Inheritance and Polymorphism

Inherited MethodsThe method Foo() can be overridden in classes B and C:

Page 17: Xamarin: Inheritance and Polymorphism

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.

Page 18: Xamarin: Inheritance and Polymorphism

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.

Page 19: Xamarin: Inheritance and Polymorphism

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:

Page 20: Xamarin: Inheritance and Polymorphism

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:

Page 21: Xamarin: Inheritance and Polymorphism

Combining Method Overriding and HidingA class C can now declare a method Foo() that either overrides or hides Foo() from class B:

Page 22: Xamarin: Inheritance and Polymorphism

Demo

Page 23: Xamarin: Inheritance and Polymorphism

Resources• MSDN

• Techoschool