22
1 C# - Inheritance and Polymorphism

1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

Embed Size (px)

DESCRIPTION

3 Inheritance  Reusability in Object Oriented Programming languages is achieved by reducing coupling between different classes, while extensibility is achieved by sub-classing.  The process of sub-classing a class to extend its functionality is called Inheritance or sub-typing.  The original class (or the class that is sub-typed) is called the base, parent or super class. The class that inherits the functionality of the base class and extends it in its own way is called the sub, child, derived or inherited class.

Citation preview

Page 1: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

1

C# - Inheritance and Polymorphism

Page 2: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

2

1. Inheritance2. Implementing Inheritance in C#3. Constructor calls in Inheritance4. Protected Access Modifier5. The Sealed Keyword6. Object Class7. Polymorphism8. Overriding Methods9. Keywords10. Boxing and Un-boxing

Course Topics

Page 3: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

3

Inheritance Reusability in Object Oriented Programming languages is achieved

by reducing coupling between different classes, while extensibility is achieved by sub-classing.

The process of sub-classing a class to extend its functionality is called Inheritance or sub-typing.

The original class (or the class that is sub-typed) is called the base, parent or super class. The class that inherits the functionality of the base class and extends it in its own way is called the sub, child, derived or inherited class.

Page 4: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

4

Implementing Inheritance in C# C# uses the colon ':' operator

to indicate inheritance. Sample code

Page 5: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

5

Constructor calls in Inheritance When we instantiate the sub-class, the compiler first instantiates the

base-class by calling one of its constructors and then calling the constructor of the sub-class.

Page 6: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

6

Calling Constructors of Base Class We can explicitly call the constructor of a base-class using the

keyword base. base must be used with the constructor header (or signature or declaration) after a colon ':' operator

Page 7: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

7

Types Protected Access Modifier C# provides the protected access modifier to protect the data. Protected members of the class can be accessed either inside the

containing class or inside its sub-class. Users still won't be able to call the protected members through

object references and if one tries to do so, the compiler will generate an error.

Page 8: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

8

Protected Internal Access ModifierIn a similar way, the protected internal access modifier allows a member (field, property and method) to be accessed:

Inside the containing class, or Inside the same project, or Inside the sub-class of the containing class. Hence, protected internal acts like 'protected OR internal', i.e.,

either protected or internal.

Page 9: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

9

Contd…

Page 10: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

10

The sealed keyword Finally, if you don't want your

class to be inherited by any class, you can mark it with the sealed keyword.

No class can inherit from a sealed class.

Sample Code

Page 11: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

11

Object Class In C# all the types (classes,

structures and interfaces) are implicitly inherited from the Object class defined in the System namespace.

This class provides the low-level services and general methods to each and every class in the .NET framework.

The Object class is extremely useful when it comes to polymorphism, as a reference of type Object can hold any type of object.

Object Class Methods

Equals Static Equals GetHashCode GetType Static ReferenceEquals ToString ProtectedFinalize ProtectedMemberwiseClone

Page 12: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

12

Polymorphism Polymorphism is the ability for classes to provide different

implementations of methods that are called by the same name. Polymorphism allows a method of a class to be called without

regard to what specific implementation it provides.

Page 13: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

13

Method Overriding Virtual and Override

keywords allows you to implement Methods Overriding in Base and Derived Classes.

Different implementations of a method with the same name and signature in the base and sub-classes is called as Polymorphism.

Sample code

Page 14: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

14

The new keyword C# introduces a keyword new to mark a method as a non-

overriding method and as the one which we don't want to use polymorphically.

Page 15: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

15

“is” and “as” keywords To check the run-time type of

an object, you can use either “is” or “as” keyword.

“is” compares the type of the object with the given type and returns true if it is cast-able otherwise, it returns false.

Page 16: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

16

Contd…

Page 17: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

17

Contd… “as”

Page 18: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

18

Boxing Boxing a value type allocates an object instance on the heap and

copies the value into the new object. Boxing is an implicit conversion of a Value Types to the type object

or to any interface type implemented by this value type. Boxing is used to store value types in the garbage-collected heap. It is called automatically when the object is about to be destructed.

* Source from MSDN

Page 19: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

19

Un-boxing Un-boxing is an explicit conversion from the type object to a

value type or from an interface type to a value type that implements the interface. An un-boxing operation consists of:Checking the object instance to make sure it is a boxed value of

the given value type.Copying the value from the instance into the value-type

variable.

* Source from MSDN

Page 20: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

20

Contd…

Page 21: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

21

Exercises1. Create a Class with Name Person (Base Class). Create another class with the

name Employee (Derived Class). Inherit the Person Class into Employee Class. 2. Implement the following things in the program for both classes.

a) Write default Constructors b) Overload the Constructorsc) Implement Overriding conceptd) Call base class methods inside Derived Classe) Create the instance of Derived Class in the Main() to access the methodsf) De-allocate the memory manuallyg) Work with Access Specifiers according to requirement

3. Create the instance of BaseClass and check the output of the program.4. Operator (+,/,-,*) overloading implementation to perform operations on objects.5. Implement the following and check out the output of the program.

You need to implement Console.WriteLine in each class constructors.• Create a Base class called B. • Create a class call D derived from B. • Create a Class called C, in constructor instantiate Class D. • In main function of the program instantiate class C

Page 22: 1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The

22

ReferencesURL :http://msdn2.microsoft.com/en-us/library/ms173149.aspxhttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconinheritanceforbasiclanguage.asp