Tuesday, January 27, 2009

Monday, January 5, 2009

Inheritance

/******************************************************************************
INHERITANCE EXAMPLE: 1
The following program simply have a parent and child class with their own constructors.
Parent class additionaly have one member function. While creating an object for a child
class we will check how it call the parent constructor and member function.
When the control comes to Child Constructor, it first call the parent constructor and
then call the child constructor because of inheritance. Again the method print is not
actually in child class, but it finds it in parent class and execute it.
******************************************************************************/
using System;
public class parent
{
public parent()
{
Console.WriteLine("Parent Constructor");
}
public void print()
{
Console.WriteLine("Member from parent class");
}
}
public class child : parent
{
public child()
{
Console.WriteLine("Child Constructor");
}
public static void Main() /* OUTPUT *****/
{
child c = new child(); // Parent Constructor
c.print();} // Child Constructor
} // Member from parent class


/*******************************************************************************
INHERITANCE EXAMPLE:2
The following program have a parent and child class with their own constructors
and member functions of same name. Parent class also have a paremeterized
constructor.
Child object always call the child constructor first but control goes to parent constructor
and then execute the child constructor.
If you want to check this just give the base keyword like public child():base()
then first it will call default parent constructor then child constructor.
In our example it first calls the parameterized constructors then child's.

The new keyword in print method for redeclaring the same function again in child class.
*******************************************************************************/
using System;
public class parent
{
string s;
public parent() { Console.WriteLine("parent constructor"); }
public parent(string s1) { s=s1; Console.WriteLine(s); }
public void print() { Console.WriteLine("Method from parent class"); }
}
public class child : parent
{
public child():base("from Child") { Console.WriteLine("Child constructor");}
public new void print() { Console.WriteLine("Method from child class");}
public static void Main()
{ // OUTPUT
child c= new child(); // from child
c.print(); // Child constructor
((parent)c).print(); // Method from child class
} // Method from parent class
}
/**************************************************************************************
INHERITANCE EXAMPLE: 3
The Parent class contains a protected string with two member functions
one for return the original string as such, another for replace some character and return
The Child class actually derived from Parent class without any member function.
Through child class object we call the first member function to print string as such, and
call the second member function to print the modified string from the parent class.
The Modified string returned by parent class again altered by the child class and print
the result
****************************************************************************************/
using System;
public class p
{
protected string s="abc@def//xyz";
public string hello()
{
return s;
}
public string m1()
{
string s1;
s1=s.Replace("@","");
return s1;
}
}
public class c : p
{
public static void Main()
{
string s; //OUTPUT
c c1 = new c();
Console.WriteLine(c1.hello()); //abc@def//xyz
s=c1.m1(); // abcdef//xyz
Console.WriteLine(s); // abcdefxyz
s=s.Replace("/","");
Console.WriteLine(s);
}
}