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
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.