|
INTERVIEW QUESTIONS
MICROSOFT
C#
DETAILS
Question: How we can use inheritance and polymorphisms in c# programming?
Answer: 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.
|
|
|
Category |
C# Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.3) By 7218 users |
Added on |
7/28/2011 |
Views |
70770 |
Rate it! |
|
|
Question:
How we can use inheritance and polymorphisms in c# programming?
Answer:
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. Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
Can we use Friend Classes or functions in C# the way we use it in C++
|
View Answer
|
|
Can u create the instance for abstract classes
|
View Answer
|
|
Which keyword is used of specify a class that cannot inherit by other class?
|
View Answer
|
|
What is protected internal class in C#
|
View Answer
|
|
What is wrapper class?is it available in c#?
|
View Answer
|
|
Which debugging window allows you to see all the name and values of all the variables in scope?
|
View Answer
|
|
Which debugging window allows you to see the methods called in the order they were called?
|
View Answer
|
|
You have an event handler called MyEvent and you want to link the click event of control, MyButton, to use MyEvent, what is the code that will like them together?
|
View Answer
|
|
What is the difference between shadow and override
|
View Answer
|
|
How do I make a DLL in C#?
|
View Answer
|
|
Does C# support C type macros?
|
View Answer
|
|
Does C# support templates?
|
View Answer
|
|
How do I declare inout arguments in C#?
|
View Answer
|
|
How do destructors and garbage collection work in C#?
|
View Answer
|
|
How do I port "synchronized" functions from Visual J++ to C#?
|
View Answer
|
|
Does C# support properties of array types?
|
View Answer
|
|
Is there a way of specifying which block or loop to break out of when working with nested loops?
|
View Answer
|
|
How do I get deterministic finalization in C#?
|
View Answer
|
|
How do I convert a string to an int in C#?
|
View Answer
|
|
Is there an equivalent of exit() for quitting a C# .NET application?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All C# Interview Questions & Answers - Exam Mode /
Learning Mode
|