Defining A Subclass
Defining A Subclass
Inheritance
C# is pure object-oriented language.
C# has the ability to create new classes from the existing classes.
Reusability is achieved by designing new classes, reusing all or some of the
properties of existing ones.
When we want to add more properties to an existing class without
actually modifying it, we use inheritance and for that we define derived class.
( Existing class from which new class is derived is called base class.
Newly derived class from existing class is called derived class. )
Types of Inheritance:
1) Single inheritance or Single level inheritance:
When class derived from single base class such type of inheritance is
called single level inheritance.
3) Hierarchical inheritance:
When multiple (i.e. two or more) classes derived from same base class
such type of inheritance is called hierarchical inheritance.
4) Multiple inheritance:
When class derived from more than one base classes such type of
inheritance called multiple inheritance.
C# does not directly implement multiple inheritance.
Multiple inheritance can be implemented using concept of interface.
2. Constructors and destructors are not inherited. All other members are
inherited. Their accessibility in the derived class depends on their declared
accessibility in the base class.
3. An instance of a derived class contains a copy of all instance fields (i.e. data
members) declared in the class and its base classes.
4. A derived class can hide an inherited member.
5. A derived class can override an inherited member.
========================================================
When implemented the subclass will contain its own members as well those of
the baseclass.
You can specify only one base class for any derived class that you create.
C# does not support the inheritance of multiple base classes into a single derived
class.
Derived class inherits (or includes) all of the members of base class.
A private member of a base class is not accessible to a derived class.
Derived class cannot access private members of the base class.
When a derived class is used as a base class for another derived class, any
protected member of the initial base class that is inherited by the first derived
class is also inherited as protected by a second derived class.
using System;
class Data
{
protected int a;
public void Setdata(int x)
{
a = x;
}
public void Showdata()
{ Console.WriteLine("a=" + a);
}
}
class SData : Data
{
public void Square()
{ int s = a * a;
Console.WriteLine("square=" + s);
}
}
class Test
{
public static void Main( )
{
SData s1 = new SData( );
s1.Setdata(4);
s1.Showdata( );
s1.Square();
}
}
Output:
a=4
square=16
===========================================================
The class A serves as a base class for the derived class B which in turn
serves as a base class for the derived class C. The chain ABC is known as
inheritance path.
{
-----
}
class C : B // second level derivation
{
-----
}
The constructors are executed from top downwards, with the bottom
most class constructor being executed last.
In a class hierarchy, the constructors are called in order of derivation,
from base class to derived class.
That is, the constructors are executed in order of derivation.
{ Console.WriteLine("a=" + a);
}
}
class SData : Data
{
public void Square()
{ int s = a * a;
Console.WriteLine("square=" + s);
}
}
class CData : SData
{
public void Cube()
{ int c = a * a * a;
Console.WriteLine("cube=" + c);
}
}
class Test
{
public static void Main()
{
CData c1 = new CData();
c1.Setdata(4);
c1.Showdata();
c1.Square();
c1.Cube();
}
}
Output:
a=4
square=16
cube=64
Hierarchical Inheritance:
When multiple (i.e. two or more) classes derived from same base class
such type of inheritance is called hierarchical inheritance.
Inheritance is used for hierarchical classification.
{
public void Cube()
{ int c = a * a * a;
Console.WriteLine("cube=" + c);
}
}
class ITest
{
public static void Main()
{
SData s1 = new SData();
s1.Setdata(4);
s1.Showdata();
s1.Square();
Console.WriteLine("----------------");
CData c1 = new CData();
c1.Setdata(4);
c1.Showdata();
c1.Cube();
}
}
Output:
a=4
square=16
----------------
a=4
cube=64
=======================================
Any form of constructor defined by the base class can be called by base.
The constructor executed will be the one that matches the arguments.
When a derived class specifies a base clause, it is calling the constructor
of its immediate base class. The base always refers to a base class immediately
above the calling class. If no base clause is present, then the base class default
constructor is called automatically.
Base always refers to the constructor in the closest base class. In a class
hierarchy, if a base class constructor requires parameters, then all derived
classes must pass those parameters “up the line”. This is true whether or not a
derived class needs parameters of its own.
In a class hierarchy, the constructors are called in order of derivation, from
base class to derived class.
That is, the constructors are executed in order of derivation.
If base is not used, then the default (parameterless) constructor of each base
class will be executed.
======================================