Friday, April 23, 2010

OOPS Concepts

Inherited Methods
A method Foo() which is declared in the base class A and not redeclared in classes B or C is inherited in the two subclasses
using System;

namespace Polymorphism

{

class A

{

public void Foo()

{

Console.WriteLine("A::Foo()");

}

}

class B : A

{}

class Test

{

static void Main(string[] args)

{

A a = new A();

a.Foo();

B b = new B();

b.Foo();



} } }

Output :-

// output --> "A::Foo()"

// output --> "A::Foo()"


The method Foo() can be overridden in classes B and C:





using System;

namespace Polymorphism

{

class A

{

public void Foo()

{

Console.WriteLine("A::Foo()");

}

}

class B : A

{

public void Foo()

{

Console.WriteLine("B::Foo()");

}

}

class Test

{

static void Main(string[] args)

{

A a;

B b;

a = new A();

b = new B();

a.Foo();

b.Foo();

a = new B();

a.Foo();

}

}

}

Output :-

// output --> "A::Foo()"

// output --> "B::Foo()"

// output --> "A::Foo()"

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Basic Thing ... But Very important. Helped a lot to recall the basic concept of oops.

    ReplyDelete