1 C# - Inheritance and Polymorphism
2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The Sealed Keyword 6.Object Class 7.Polymorphism 8.Overriding Methods 9.Keywords 10.Boxing and Un-boxing Course Topics
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.
4 Implementing Inheritance in C# C# uses the colon ':' operator to indicate inheritance. Sample code
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.
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
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.
8 Protected Internal Access Modifier In 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.
9 Contd…
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
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
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.
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
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.
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.
16 Contd…
17 Contd… “as”
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
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
20 Contd…
21 Exercises 1.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 Constructors c) Implement Overriding concept d) Call base class methods inside Derived Class e) Create the instance of Derived Class in the Main() to access the methods f) De-allocate the memory manually g) 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
22 References URL : us/vbcn7/html/vaconinheritanceforbasiclanguage.asp