xxxxxxxxxx
// Parent Class
public class A
{
public void Method1()
{
// Method implementation.
}
}
// inherit class A in class B , which makes B the child class of A
public class B : A
{ }
public class Example
{
public static void Main()
{
B b = new B();
// It will call the parent class method
b.Method1();
}
}
xxxxxxxxxx
class parent_class
{
}
class child_class : parent_class
{
//Will simply move Data and Methods from the parent_class to the child class.
}
xxxxxxxxxx
class Animal {
// fields and methods
}
// Dog inherits from Animal
class Dog : Animal {
// fields and methods of Animal
// fields and methods of Dog
}
xxxxxxxxxx
//Example of inheritence
using System;
public class Parent
{
public string parent_description = "This is parent class";
}
//child class inherits parent class
public class Child: Parent
{
public string child_description = "This is child class";
}
class TestInheritance
{
public static void Main(string[] args)
{
Child d = new Child();
Console.WriteLine(d.parent_description);
Console.WriteLine(d.child_description);
}
}
xxxxxxxxxx
using System;
namespace InheritanceApplication {
class Shape {
public void setWidth(int w) {
width = w;
}
public void setHeight(int h) {
height = h;
}
protected int width;
protected int height;
}
// Derived class
class Rectangle: Shape {
public int getArea() {
return (width * height);
}
}
class RectangleTester {
static void Main(string[] args) {
Rectangle Rect = new Rectangle();
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.ReadKey();
}
}
}