You are on page 1of 12

INHERITANCE

Inheritance
The Concept of one class deriving the properties of another class
is termed as inheritance
The Features of inheritance are
The Class derived is known as sub class or child class
The Class from which the another class is derived is called as base
class or parent class
Subclass inherit the methods and members of the base class
It provides an elegant mechanism of re-use
Types of inheritance are
Single Inheritance
Multiple Inheritance
Multi-Level Inheritance
Multiple Inheritance is not supported in C#

Polymorphism or Virtual Functions or Method Overriding


The ability of an object to act in different ways based on the
context is termed as polymorphism
Uses
Enables the same code to have different types of implementations
Defines the same method to any number of derived classes
Scenario
If you want to declare a method with the same signature in both
base and derived classes but probably doesnt do the same thing.
We can achieve the above scenario, By declaring a base class
function as virtual, which allows you the function to be overridden
in any derived classes:
It is also permitted to declare a property as virtual. For a virtual
or overridden property, the syntax is the same as for a nonvirtual
property, with the exception of the keyword virtual, which is
added to the definition.

class MyBaseClass
{
public virtual void display()
{
Console.WriteLine(Base Class Function was executed);
}
}
Class MyDerivedClass : MyBaseClass
{
public overriden void display()
{
Console.WriteLine(Derived Class Function was executed);
}
}

Hiding Methods:
If a method with the same signature is declared in both
base and derived classes, but the methods are not declared
as virtual and override, respectively, then the derived class
version is said to hide the base class version.
In these situations, C# generates a compilation warning.
That reminds you to use the new keyword to declare that
you intend to hide a method, like this:
class MyDerivedClass : MyBaseClass
{
public new voiddisplay()
{
// some implementation;
}
}

Calling Base Versions of Functions


C# has a special syntax for calling base versions of a method from
a derived class: base.<MethodName>().
For example, if you want a method in a derived class to return 90
percent of the value returned by the base class method, you can
use the following syntax:
class CustomerAccount
{
public virtual decimal CalculatePrice()
{ // implementation return 0.0M;
}}
class GoldAccount : CustomerAccount
{
public override decimal CalculatePrice()
{
return base.CalculatePrice() * 0.9M;
}
}

Constructors of Derived Classes


An interesting question arises as to what happens when you start
defining your own constructors for classes that are part of a
hierarchy, inherited from other classes that may also have custom
constructors.
You might be wondering why there is any special problem with
derived classes.
The reason is that when you create an instance of a derived class,
there is actually more than one constructor at work.
The constructor of the class you instantiate isnt by itself
sufficient to initialize the class - the constructors of the base
classes must also be called. Thats why weve been talking about
construction through the hierarchy.

Sealed Classes and Methods


C# allows classes and methods to be declared as sealed. In the
case of a class, this means that you cant inherit from that class.
In the case of a method, this means that you cant override that
method.
sealed class FinalClass {
// etc
}
class DerivedClass : FinalClass // wrong. Will give compilation error
{
// etc
}
Sealed Classes are fast in the execution because the compiler
doesnt look for any derived classes

Abstract Class and Methods


Abstract classes can be simply defined as incomplete classes.
Abstract classes contain one or more incomplete methods called
abstract methods.
The abstract class only provides the signature or declaration of the
abstract methods and leaves the implementation of these methods
to derived or sub-classes.
Abstract methods and abstract classes are marked with the abstract
keyword.
An abstract class itself must be marked with the abstract keyword.
Since abstract classes are incomplete, they can not be instantiated.
They must be sub-classed in order to use their functionality. This is
the reason why an abstract class can't be sealed.
A class inheriting an abstract class must implement all the abstract
methods in the abstract class, or it too must be declared as an
abstract class.
A class inheriting an abstract class and implementing all its abstract
methods is called the concrete class of the abstract class.
We can declare a reference of the type of abstract class and it can
point to the objects of the classes that have inherited the abstract
class

abstract class TaxCalculator


{
protected double itemPrice;
protected double tax;
public abstract double CalculateTax();
double Tax {
get {
return tax;
}
}
public double ItemPrice {
get {
return itemPrice;
}
}
}

public

Interface
Interfaces are a special kind of type in C#, used to define the
specifications (in terms of method signatures) that should be followed
by its sub-types.
An interface is declared using the interface keyword.
Interfaces, like abstract classes, can not be instantiated.
An interface can contain a signature of the methods, properties and
indexers.
An interface is a type whose members are all public and abstract by
default.
An interface is implemented by a class. A class implementing the
interface must provide the body for all the members of the interface.
To implement an interface, a class uses the same syntax that is used
for inheritance. A colon : is used to show that a class is implementing
a particular interface.
A class can implement more than one interface, contrary to classinheritance where you can inherit only one class.
An interface itself can inherit other interfaces.
We can declare the reference of the interface type and it can point to
any class implementing the interface.
It is a convention in C#, to prefix the name of interfaces with
uppercase 'I' like IDisposable, ISerializable, ICloneable, IEnumerator,
etc.