Inheritance

  1.what is inheritance ?
The method of constructing one class from another is called Inheritance.The derived class inherits all the properties and methods from the base class and it can add its own methods also.

2.Are constructors and destructors inherited ?
No

3.Advantages of Inheritance.
a)Code re usability-Public and protected methods can be used in derived classes
b) Extensibility- base class can be extended as per business logic in derived classes.
c)Polymorphism

4.What are diff types of inheritance?
a) Single inheritance --Class B is derived from base class A. A->B
b)Multiple inheritance ---Class C derives from both A and B.  A,B->C
c)Multilevel inheritance--Class B derives from Class base class A.class C derives from B.  A->B->C
d)Hybrid inheritance --class B and C derives from same base class A.  A->B,C

5.Is multiple inheritance possible in C#.Why?
No. because
a)Its not supported by CLR since its support many diff language and not all languages can have multiple inheritance concept. 
b)Because of the complexities involved where method name can clash when two diff classes have same method name.This is resolved by pointers in C++ but its not possible in c#.


Instead interfaces can be used to achieve the same.


6.Is circular inheritance possible. like A:B ,B:A ?
No


7.How do you prevent a class from being inherited ?
a)Make the class as sealed.
b)Add private constructors to the class.


8. Can derive class have public modifier when there are no modifiers specified on the base class?
No. bc derived class cannot be more accessible that base class.


9.why c# does not support inheritance by structure?
Because structures are mainly used for light weight process. And if they are allowed to be inherited then they have to act as base class which is not possible as they are value types.


10.Does structs inherit from interfaces?
Yes structs can inherit only from interface.


11.What do you mean by sealed keyword ?
If you mark a class as sealed it means that you cannot inherit from it but you can create objects of that class.


12.can you mark method as sealed ?
Yes.But for a method to be marked as sealed you need to have override keyword also.



13.what do you mean by upcasting and downcasting ?
class DerivedClass :BaseClass

Upcasting--assigning a derived class object to a base class.This is implicit.
BaseClass b= new DerivedClass.


Downcasting--assigning baseclass object to derived class. This is explicit and it throws run time error .
a)BaseClass b = new BaseClass()
   DerivedClass d= b  //will give compile time error.
  
b) d=(DerivedClass)b  //will give runtime error.


c)b=d;
   d=(DerivedClass)b;
   d.method()             //will always call derivedclass method

   //there is no point in taking so much of pain and using this kind of code !!!


14.

 

 

12 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hey Nice Post, Keep Updating more question and answers
    http://allittechnologies.blogspot.com/

    ReplyDelete
  3. C# (Sharp) Programming Interview Questions and Answers
    http://allinterviewquestionsandanswerspdf.blogspot.in/2016/06/top-110-c-sharp-programming-interview.html

    ReplyDelete
  4. If you are adopted and given a new name,and your adopted family passes away and leaves you land,and ND then you leaguely go back to your birth name are you still able to keep inheritance even though there are others who were born with the adopters last name

    ReplyDelete
  5. If you are adopted and given a new name,and your adopted family passes away and leaves you land,and ND then you leaguely go back to your birth name are you still able to keep inheritance even though there are others who were born with the adopters last name

    ReplyDelete
  6. Thank you so much for sharing this worth while to spent time on. You are running a really awesome blog. Keep up this good work

    Mobile Application Development Training in Chennai

    ReplyDelete
  7. The Dot Net Interview Questions shared are Very much useful My sincere thanks for sharing this post and please continue to share this kind of post
    Dot Net Training in Chennai

    ReplyDelete
  8. really you have been shared very informative post. before i read this blog i didn't have any knowledge about this but now i got some knowledge. so keep on sharing such kind of an interesting blogs.
    dot net training in chennai

    ReplyDelete
  9. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in dot net programming your post help me a lot.Thanks for your informative article.Dot Net training in chennai | dot net training and placement | Dot Net training in velachery

    ReplyDelete