-Add a class
Class1.cs and write the following :
class Class1
{
public Class1()
{
Console.WriteLine("class1 constructor");
}
public void test1()
{
Console.WriteLine("Method one");
}
public void test2()
{
Console.WriteLine("Method two");
}
}
-Add a class
Class2.cs and write the following :
class Class2:Class1
{
public Class2()
{
Console.WriteLine("class2 constructor");
}
public void test3()
{
Console.WriteLine("Method three");
}
static void Main ()
{
Class2 obj = new Class2();
obj.test1(); obj.test2();
obj.test3();
Console.ReadLine();
}
}
Rules to be
considered while working with inheritance:
- In inheritance the execution of child class starts by invoking default constructor of parent class. So parent class’s default constructor should be accessible to child class, otherwise inheritance cannot be performed.
- In inheritance child classes can access members of its parent classes whereas the parent class cannot access child class members moreover a parent never knows about its children.
To test this rewrite the code under main
method of child class Class2 as following-
static void Main ()
{
Class1 obj = new Class1();
obj.test1(); obj.test2();
// obj.test3(); // invalid
Console.ReadLine();
}
- Earlier we have discussed object of a class can be assign to the variable of the same class to make it as a reference in the same way object of a child class can be assigned to the variable of its parent to make it as a reference, so that both object and reference will be pointing to the same memory but in this situation also parent class reference cannot be used for invoking child class members.
To test this
rewrites the code under main method of Class2 as following-
static void Main ()
{
Class2 c = new Class2();
Class1 p = c; //valid
p.test1(); p.test2();
//p.test3(); //invalid
Console.ReadLine();
}
Note – if required reference of a parent class
i.e. created from object of the child class can be converted back into child
class reference but this required ‘explicit conversion ’ which can be done as
blow.
Class2 c = new Class2();
- creating
reference of parent using object of child:
Class1 p = c; //valid
- converting
parent reference back into child reference:
Class2 c2 =
(Class2)P ;
or
Class2 c2 =
p as Class2 ;
- Each and every class in .NET languages has the default parent value i.e. a class object present under system name space. This always stands at top of the hierarchy becoming a parent for every class directly or indirectly.
The members
which are defined in the class object can be accessed from any of the class in
.NET languages.
eg.
GetType() , ToString() etc.
Types of INHERITANCE
as par the standers of object oriented programming we have two different type of inheritance-- Single inheritance
- Multiple inheritance
If a class has one immediate parent class to it we call it as single
inheritance
If a class has more than
one immediate parent class we call it as multiple Inheritances
- Traditional C++ language supports both single and multiple and multiple inheritance whereas modern object oriented languages like JAVA .Nat doesn’t provide the support for multiple inheritance through classes because because it suffers from the problem of antiquity i.e. a same member may be defined in multiple parents of a class in the same hierarchy
No comments:
Post a Comment