Wednesday, 8 February 2017

Oops concept questions Part-2

1. Ques: What are the characteristics of an abstract class?  what purpose we can use it. 
Answer:
An abstract class is a class that cannot be instantiated and is always used as a base class.
The following are the characteristics of an abstract class:
  • You cannot instantiate an abstract class directly. You can create instance of it derived class.
  • An abstract class have abstract as well as non-abstract members.
  • At least one abstract method in should be present in abstract class.
  • An abstract class is always public.
  • An abstract class is declared using the abstract keyword.
  • An abstract class cannot be a sealed class because the sealed modifier prevents a class from being inherited.
  • An abstract class can be inherited from a class and one or more interfaces.
  • An abstract class cannot support multiple inheritance.
purpose to use it
The basic purpose of an abstract class is to provide a common definition of the base class that multiple derived classes can share.


2. Ques: What is difference between new and override keyword ?
Answer:
The override keyword is used to modify a method, property, declared in the base class and allow it to be overridden in the derived class.
The new keyword is used to hide a method, property base class into derived class.


3. Ques: Distinguish between abstract function and pure virtual function.
Answer:
Abstract Function:
It can be declared only inside abstract class.
It contains only method definition not the implementation.
It must be overridden.

Virtual Function:
It can be declared inside abstract as well as non abstract class.
It contains method implementation.
It may be overridden.


4. Ques:What is difference between method overloading and method overriding?
Answer:
Overloading :
  • Method overloading is the two or more methods in the same class with the same name but different in terms of No of parameter, Type of parameter, Order of parameter.
  • It also known as compile time polymorphism. 
  • It may or may not required inheritance in Method Overloading.
  • Method overloading can be overloaded in same class or in the child class.
Overriding
  • Overriding is the same method names with same arguments and return types associates with the class and its child class.
  • Overriding means having two methods with the same arguments, but different implementations.
  • It also known as run time polymorphism.
  • It always requires inheritance in Method Overriding.
  • Method overriding is only possible in derived class not within the same class where the method is declared

5. Ques: What is Sealed class ? what is advantages of sealed class.
Answer:
Sealed classes cannot be inherited. Sealed modifier is used to create a sealed class.sealed class restricts inheritance for security reason. A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class.Sealed class is the last class in the hierarchy.

Advantages of sealed class is it restrict the third party source for developing new software by inheriting from our logic.


6. Ques: Can constructors be inherited in c# ?
Answer:
No.

7. Ques: How can you call a base class constructor from a child class in c# ?
Answer:
Child class can call base class constructor by using the keyword base.


8. Ques:  What is a Nested Class?
Answer:
Nested class is nothing but defining a class within another class.Nested classes has the ability to specify private as an access modifier for the class itself.The use of the private access modifier defines the intended accessibility of the class and prevents access from outside the class.

No comments:

Post a Comment

Thank you for comment