Wednesday 22 February 2017

Oops concept questions Part-4

1. Ques: How many type of Access modifier in C# ?
Answer:-
Public modifier : Public member can be accessed by any other code in the same assembly or outside the assembly.

Private modifier : Private  can be accessed only by code in the same class or struct.

Protected modifier: Protected can be accessed only by code in the same class or struct, or in a class that is derived from that class.

Internal modifier: Internal member can be accessed by any code in the same assembly, but not from another assembly.

Protected internal modifier: The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly.


2. Ques: What is difference between Abstract Class and an Interface ?
Answer:-
Abstract class:
  • We can not create an object (instance) of an abstract class.
  • An interface cannot implementation any code, It has only the signature.
  • We can not implement multiple inheritance in Abstract class.
  • Abstract class can have static methods, main method and constructor.
  • The abstract keyword is used to declare abstract class.
  • It is Collection of abstract method and Concrete method.
  • Abstract class can have constructor.
  • Abstract class is faster than interface.
Interface:
  • Interface has only the signatures of the methods,property not the implementation .
  • We can implement multiple inheritance in Interface.
  • We can not declare a member field in an Interface.
  • We can not use any access modifier in interface by default everything is public.
  • Interface class don't have static methods, main method and constructor.
  • The interface keyword is used to declare interface.
  • It is Collection of abstract method only.
  • Interface is slower as it takes some time to find implemented method in class.
  • Interface slower than Abstract class.

3. Ques: What is Destructor?
Answer:
A Destructor is automatically invoked when an object is finally destroyed. The name of the Destructor is same as class and prefixed with a tilde (~)

A Destructor is used to free the dynamic allocated memory and release the resources. You can Implement a custom method that allows to control object destruction by calling the destructor.
  • Destructors don not have any return type
  • Destructors are always public
  • Destructors can not be overloaded


4. Ques: What is the difference between Shadowing(Method hiding) and Overriding?
Answer:

Shadowing (Method hiding):
  • You can shadow a base class member in the derived class by using the keyword New.
  • The method signature, access level and return type of the shadowed member can be completely different than the base class member.
  • A method or function of the base class is available to the child (derived) class without the use of the “overriding” keyword.
  • The compiler hides the function or method of the base class. This concept is known as shadowing or method hiding.
  • In the shadowing or method hiding, the child (derived) class has its own function, the same function is also available in the base class.
  • Shadowing redefines an entire method or function.
  • We can change the access modifier.
Overriding:
  • Method overriding is an important feature of OOPS that allows us to re-write a base class function or method with a different definition.
  • Overriding is also known as “Dynamic Polymorphism” because overriding is resolved at runtime.
  • The method signature, access level and return type of the hidden member has to be same as the base class member
  • In other words both methods (base class method and derived class method) have the same name, same number and same type of parameter in the same order with the same return type.
  • The overridden base method must be virtual, abstract or override.
  • Overriding redefines only the implementation of a method or function.
  • We cannot change the access modifier. The access modifier must be the same as in the base class method or function.

No comments:

Post a Comment

Thank you for comment