GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Microsoft  >  C#
Go To First  |  Previous Question  |  Next Question 
 C#  |  Question 345 of 436    Print  
How we can use inheritance and polymorphisms in c# programming?

  
Total Answers and Comments: 3 Last Update: December 23, 2006     Asked by: sandeep 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
June 16, 2006 01:09:08   #1  
Pallav        

RE: How we can use inheritance and polymorphisms in c#...
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
 
Is this answer useful? Yes | No
September 10, 2006 02:40:32   #2  
Vijay Selvaraj        

RE: How we can use inheritance and polymorphisms in c#...
Inheritance is access data from base class to derive classinheritance is a part of the cpppolymorphisms is a multiple implementaion in a same method
 
Is this answer useful? Yes | No
December 23, 2006 02:20:52   #3  
swatias Member Since: December 2006   Contribution: 5    

RE: How we can use inheritance and polymorphisms in c#...

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


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape