Académique Documents
Professionnel Documents
Culture Documents
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#
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;
}
}
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.