Thursday 16 February 2017

Oops concept questions Part-3

1. Ques: Difference between association, aggregation and composition ?
Answer:
  • Association: Association is a relationship where all objects have their own life cycle and there is no owner.
Let's take an example of Teacher and Student. Multiple students can associate with single teacher and single student can associate with multiple teachers, but there is no ownership between the objects and both have their own life cycle. Both can be created and deleted independently.
  • Aggregation: Aggregation is a specialized form of Association where all objects have their own life cycle, but there is ownership and child objects can not belong to another parent object.
Let's take an example of Department and teacher. A single teacher can not belong to multiple departments, but if we delete the department, the teacher object will not be destroyed. We can think about it as a “has-a” relationship.
  • Composition: Composition is again specialized form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object does not have its life cycle and if parent object is deleted, all child objects will also be deleted.
Let's take again an example of relationship between House and Rooms. House can contain multiple rooms - there is no independent life of room and any room can not belong to two different houses. If we delete the house - room will automatically be deleted.


2. Ques: What are the advantages of Property in class ?
Answer:
  • Properties  are useful for data binding.
  • Properties are used to restrict direct access to member variables of a class.
  • Setting property may require certain validation, and validation code in "Set" part of code can easily help you validate the input and report errors accordingly.
  • Set part of method can raise notification events such as INotifyPropertyChanged.PropertyChanged which other objects can listen for and update the display value.

3. Ques: What is difference between Generalization and Specialization ?
Answer:
Generalization:-
The process of extracting common characteristics from two or more classes and combining them into a generalized superclass, is called generalization.

Specialization:-
Specialization is the reverse process of generalization means creating new sub classes from an existing class.




4. Ques: What is static constructor ? What are the properties of static constructors.
Answer:
A static constructor is used to initialize any static data.
It is called automatically before the first instance is created or any static members are referenced.

Static constructors have the following properties:
  • A static constructor does not take access modifiers or have parameters.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • A static constructor cannot be called directly.
  • The user has no control on when the static constructor is executed in the program.
  • A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
  • Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

5. Ques: What is Private constructor ? When we are using Private constructor.
Answer:
Private constructors are used to prevent creating instances of a class.
It is used in singleton design patterns, where the code only one instance of a class can ever be created.
When we declare a class constructor as private , we can not do 2 things:-
  • We can not inherit the class.
  • We can not create a object of the class.

6. Ques:  How can we implement singleton pattern in .NET?
Answer:
Singleton pattern restricts only one instance running for an object.
Singleton pattern is commonly used in print spoolers.

Following three steps needed to implement singleton pattern
  • A class with static members needs to be created.
  • A private constructor to the class above should be defined. 
  • A static method can be used to access the singleton object.
Public class SampleStaticClass
        Private shared objemployee as clsemployee
End class
 

7. Ques:  What is Inheritance ? Explain different types of Inheritance ? Answer:
Taking the properties of one class into another class is called inheritance. Inheritance provides
reusability by allowing us to extend an existing class. The reason behind OOP programming is
to promote the reusability of code and to reduce complexity in code and it is possible by
using inheritance.

There are four types of Inheritance in Object oriented programming.

  • Single Inheritance : contains one base class and one derived class
  • Hierarchical Inheritance : Contains one base class and multiple derived classes of same base class
  • Multilevel Inheritance : Contains a class derived from a derived class
  • Multiple Inheritance : Contains several base class and a derived class 

No comments:

Post a Comment

Thank you for comment