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.
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
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
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.
Are private class-level variables inherited? Yes, but they are not accessible, so looking at it you can honestly say that they are not inherited. But they are.
Are constructors and destructors inherited ?
No
Does structs inherit from interfaces?
Yes structs can inherit only from interface.
How do you prevent a class from being inherited ?
a)Make the class as sealed.
b)Add private constructors to the class.
Is circular inheritance possible. like A:B ,B:A ?
No
can you mark method as sealed ?
Yes.But for a method to be marked as sealed you need to have override keyword also.
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.
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
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
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.
Are private class-level variables inherited? Yes, but they are not accessible, so looking at it you can honestly say that they are not inherited. But they are.
Are constructors and destructors inherited ?
No
Does structs inherit from interfaces?
Yes structs can inherit only from interface.
How do you prevent a class from being inherited ?
a)Make the class as sealed.
b)Add private constructors to the class.
Is circular inheritance possible. like A:B ,B:A ?
No
can you mark method as sealed ?
Yes.But for a method to be marked as sealed you need to have override keyword also.
0 comments:
Post a Comment