Monday, October 31, 2016

oop-page3

16. What is Method Overriding? How to override a function in C#?

Ans:
Use the override modifier to modify a method, a property, an indexer, or an event. An override method provides a new implementation of a member inherited from a base class. The method overridden by an override declaration is known as the overridden base method. The overridden base method must have the same signature as the override method. 
You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override. 

17. What is Method overloading?

Ans:
Method overloading occurs when a class contains two methods with the same name, but different signatures. 

18. What is Overriding?

Ans:
Method overriding is a feature that allows to invoke functions (that have the same signatures) and that belong to different classes in the same hierarchy of inheritance using the base class reference. In C# it is done using keywords virtual and overrides . 

19. What is Protected access modifier in C#?

Ans:
The protected keyword is a member access modifier. It can only be used in a declaring a function or method not in the class ie. a class can't be declared as protected class. 
A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declare this member. In other words access is limited to within the class definition and any class that inherits from the class 
A protected member of a base class is accessible in a derived class only if the access takes place through the derived class type. 

20. What is Internal access modifier in C#?

Ans:
The internal keyword is an access modifier for types and type members ie. we can declare a class as internal or its member as internal. Internal members are accessible only within files in the same assembly (.dll). In other words, access is limited exclusively to classes defined within the current project assembly. 



21. What is Private access modifier in C#?

Ans:
The private keyword is a member access modifier ie. we can't explicitly declare a class as Private, however if do not specify any access modifier to the class, its scope will be assumed as Private. Private access is the least permissive access level of all access modifiers. 
Private members are accessible only within the body of the class or the struct in which they are declared. This is the default access modifier for the class declaration. 

22. What is Public access modifier in C#?

Ans:
The public keyword is an access modifier for types and type members ie. we can declare a class or its member (functions or methods) as Public. There are no restrictions on accessing public members. 

23. What are Constructors?

Ans:
Constructors are used for initializing the members of a class whenever an object is created with the default values for initialization. 
If no constructor defined then the CLR will provide an implicit constructor which is called as Default Constructor. 
A class can have any number of constructors provided they vary with the number of arguments that are passed, which is they should have different signatures. 
Constructors do not return a value Constructors can be overloaded 

24. What are the various types of Constructor

Ans:
Public : Accessible to All
Private: Those classes in which only static members are there and you don't want there objects to be created in any class.
Static: Used for initializing only the static members of the class. These will be invoked for the very first time the class is being loaded on the memory. They cannot accept any arguments. Static Constructors cannot have any access modifiers.
Intern: implementations of the abstract class to the assembly defining the class. A class containing an internal constructor cannot be instantiated outside of the assembly (Namespace).  

25.  What is a private constructor? Where will you use it?

Ans:
When you declare a Constructor with Private access modifier then it is called Private Constructor. We can use the private constructor in singleton pattern. 
If you declare a Constructor as private then it doesnt allow to create object for its derived class, i.e you loose inherent facility for that class. 
Example: 
Class A 
// some code 
Private Void A() 
//Private Constructor 
}
  
Class B:A 
//code 
}   
B obj = new B(); // will give Compilation Error   
Because Class A constructor declared as private hence its accessibility limit is to that class only, Class B can't access. When we create an object for Class B that constructor will call constructor A but class B have no rights to access the Class A constructor hence we will get compilation error.





EmoticonEmoticon