06-06-2013, 04:10 PM
Inheritance and Polymorphism
Inheritance.pptx (Size: 89.67 KB / Downloads: 19)
Introduction
It is always nice if we could use something that already exists rather than creating the same over again
Ability to create new class from existing class, achieves the reusability
In C#, that we can achieve through inheritance
Related to inheritance , equally important concept is Polymorphism, that permits same method name to be used for different operations in different derived class
Classical Form
Inheritance represents relationship between two or more class
From this figure it is clear that class B is derived from class A
So class A is known as super class , or parent class , or Base class
Class B is known as Subclass , or Child class or Derived class
We can now create of object of class A and class B like
A a;
B b;
In such case we say that b is a type of a
Such relationship is known as “is -a” relationship
Containment Inheritance
We can define this form of inheritance between class definition
In such case Object a is contained in object b
This relationship refer as a has-a relationship
Outer class B which contains inner class is known as parent class , and the contained class A is termed as child class
Example: Car has-a radio
House has-a store room
Characteristics of inheritance
Inheritance is transitive, means if C is derived from B and B is derived from A , than C inherits the members declared in B and A both
A derived class extends its direct base class, it can add its own members and methods , but it can not change the definition of inherited members
Constructors and Destructors are not inherited
An instance of a class(object) contains copy of all instance fields declared in the class and its base class
A derived class can hide an inherited members
A derived class can override an inherited members
Class Visibility
Each class needs to specify its level of visibility
It is used to decide, which part of the system can create class objects
C# class can have one of the two visibility: public or internal
If we do not mark, than it is implicitly set to ‘internal’, i.e. all class are by default internal
Internal classes are accessible within the same program assembly and are not accessible from out side the assembly
Class Member Visibility
Class member can have any of the following visibility modifiers
Private
Protected
Internal
Protected internal
public
Except for the protected internal combinations, it is an error to specify more than one modifiers
When no modifier is specified, it default to private
Define Subclass Constructors
A subclass constructor is used to construct the instance variables of the both subclass and super class
It uses the keyword base to invoke the constructor method of super class
Overriding Methods
Method that defined in super class , derived in sub class , can be used by sub class object
However, there may be occasions when we want object to respond same method but behave differently when method is called
That means, we should override the method defined in the super class
This is possible by defining a method in the subclass that has the same name, same argument, same return type as a method in super class
That we can achieve through
Specify that method as virtual in base class
Implement the method in subclass using the keyword override