Monday, November 9, 2009

INHERITANCE IN C#.NET

I have already given the definition for inheritance. Here we can see a sample program for inheritance


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication14
{
class MyClass
{
public int x, y;
public void readvalue()
{
x = 10;
y = 20;
}
public void printvalue()
{
Console.WriteLine("The Value of X= " + x);
Console.WriteLine("The value of Y= " + y);
}
}
class MainClass
{

public static void Main()
{
MyClass1 m = new MyClass1();
m.readvalue();
m.readnextvalue();
m.printvalue();
m.printnextvalue();
Console.ReadLine();
}
}
class MyClass1 : MyClass
{
public int p, q;
public void readnextvalue()
{
p = 30;
q = 40;

}
public void printnextvalue()
{
Console.WriteLine("The Value of P= " + p);
Console.WriteLine("The Value of Q= " + q);

}
}

}



This is the sample program to demonstrate inheritance.
The operator used for inheritance is ‘:’
the syntax is Class derived class: baseclass

In the above program,

class MyClass1 : MyClass

Shows that we are inheriting Myclass1 Form Myclass.

No comments:

Post a Comment