How we can use inheritance and polymorphisms in c# programming?

Showing Answers 1 - 11 of 11 Answers

Pallav

  • Jun 16th, 2006
 

HI, Inheritance in C# is similar to cpp, if you are familiar to cpp, the only difference is you dont inherit a class in public or protected mode, the way to inherit a class is Class Base { protected int length, breadth; public Base(int a, int b) { this.length = a; this.breadth = b; } public virtual int Show() { Console.WriteLine(Length of a rectangle is {0} and breadth is {1}, length, breadth); } } Class Derived: Base // The way to inherit a class in C# { public Derived(int l, int b, string color): base(l, b) // call Base constructor { this.color = color; } // an overridden method beacuse in the derived method we can // change the behaviour, This is a kind on polymorphism. public override int Show() { base.Show(); Console.WriteLine("Color of the rectangle is " color); } public string color; } Class Tester { public static void Main() { Base b = new Base(10, 20); Derived d = new Derived(30, 40, "Red" ); b.Show(); d.show(); } }Output: Length of a rectangle is 10 and breadth is 20 Length of a rectangle is 30 and breadth is 40 Color of the rectangle is RedI hope this will make you a bit clear about Inheritance and polymorphism. Thanks Pallav

  Was this answer useful?  Yes

Vijay Selvaraj

  • Sep 10th, 2006
 

Inheritance is access data from base class to derive classinheritance is a part of the cpppolymorphisms is a multiple implementaion in a same method

  Was this answer useful?  Yes

swatias

  • Dec 23rd, 2006
 

When you derive a class from a base class, the derived class will inherit all members of the base class except constructors, though whether the derived class would be able to access those members would depend upon the accessibility of those members in the base class. C# gives us polymorphism through inheritance. Inheritance-based polymorphism allows us to define methods in a base class and override them with derived class implementations. Thus if you have a base class object that might be holding one of several derived class objects, polymorphism when properly used allows you to call a method that will work differently according to the type of derived class the object belongs to.

Reference: http://www.codeproject.com/csharp/csharpintro01.asp

  Was this answer useful?  Yes

george

  • Aug 11th, 2011
 

When we create a new class which is derived from the existing class is called inheritance... This class is called as derived class and the existing class is called as base class..

Polymorphism is nothing but one name many form.. The purpose of polymorphism is overloading and overriding logic...

  Was this answer useful?  Yes

Suresh Jayaraman

  • Sep 27th, 2011
 

Inheritance:

We can access the properties,fields,events methods from one class to another class.

Polymorphism:

There are two types of polymorphism.

1.Compile time polymorphism -- function overloading is example for compile time polymorphism

2.Run time polymorphism -- function overriding is example for run time polymorphism

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions