Showing posts with label OopsConcept. Show all posts
Showing posts with label OopsConcept. Show all posts

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.

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 

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.

Friday, 6 January 2017

Oops concept questions Part-1

Ques: What is object in c# ?
An object is an instance of a class. The new operator is used to create an object of a class. When an object of a class is instantiated, the system allocates memory for every data member of that class.

Ques: What is class ?
Class is a template or blueprint that is used for creating an object.
Class is reference type.

Ques: What are the different type of class in c# ?
There are four types of classes in C# .Net
1. Abstract class
2. Sealed class
3. Static class
4. Partial class

1) Abstract class - An Abstract Class means that, we can not create instance of this class, but can create instance of derivation of this class.Abstract class always act as base class. for creating Abstract Class the abstract keyword use.

2) Sealed class - A sealed class is a class which cannot be inherited. A sealed class cannot be a base class. The modifier abstract cannot be applied to a sealed class. By default, struct (structure) is sealed.Sealed Class is denoted by the keyword sealed.

3) Static class - A Static Class is one which cannot be instantiated. The keyword new cannot be used with static classes.

4) Partial class - Partial Class allows its members – method, properties, and events – to be divided into multiple source files. At compile time these files get combined into a single class.

Ques: What are the main OOP’s concepts ?
Abstraction, Encapsulation, Inheritance and Polymorphism are the main OOP’s concepts.

Ques: What is Encapsulation ?
Encapsulation is way to hide data, properties and methods from outside the class scope.
for Encapsulation private modifier keyword usually used.

Ques: How we can implement Encapsulation in class ?
by using "private" access access modifier
ex:
public Class Car
          {
                private enginstype="";
                private void CalculateSpeed(){
                Console.WriteLine("speed of car 300 m/h");
          }
}

Ques: What is Abstraction ?
Abstraction is a mechanism which represent the essential features without including implementation details.Abstraction Showing Whats Necessary to client

Ques: What is Polymorphism ?
Polymorphism means having more than one form.it is one of the most important future of object-oriented programming
                                                   or
Polymorphism is the ability of objects of different types to provide interface for implementations of methods.

Ques: What are different types of polymorphism in C#
There are two type of polymorphism
1. Compile time Polymorphism or Early Binding (Method Overloading)
2. Runtime Polymorphism or Late Binding (Method Overriding)


Ques: What is Inheritance ?
 Inheritance provide you to create new classes (child class) that reuse, extend, and modify the behavior that is defined in other classes (Parent class).

Ques: What is Constructor ?
A constructor is an instance method which is the same name as the class.
                                                   or
Constructor is a method which executed automatically when we instant object of that class.
it used to initialize the filed of class before using of  an object.
  • A class can have many number of constructors.
  • A constructor doesn't have any return type.
  • A constructor can be called another constructor by using "this" keyword.

Ques: What are type of Constructor ?
Default Constructor: In default constructor does not have any input parameter.
Parametrized Constructor:In parameterized constructor having one or more parameters.
Static Constructor: It invoked only during the first initialization of instance. It is used to initialize static fields of the class
Private Constructor:A class with private constructor cannot be inherited.
Copy Constructor:A constructor that contains a parameter of same class type is called as copy constructor.

Ques: Define Destructor ?
Answer:
Destructor is a method which is automatically called when the object is destroyed.
Destructor name is also same as class name but with the tilde symbol before the name.


Ques: What is the difference between a class and a structure ?
Answer:
Class:
A class is a reference type.
While instantiating a class, its instance allocated on heap memory .
Classes support inheritance.
Variables of a class can be assigned as null.
Class object cannot be created without using the new keyword.
Class  can be abstract.
Classes are usually used for large amounts of data.

Structure:
A structure is a value type.
In structure, memory is allocated on stack.
Structures does not support inheritance.
Structure members cannot have null values.
Structure object can be created without using the new keyword.
A structure can't be abstract.
structure are usually used for smaller amounts of data.


Ques: What is namespace ?
A namespace provides a fundamental unit of logical code grouping.

Ques: What is Sealed class ?
Answer: A sealed class is a class that does not allow inheritance.
Some object model designs need to allow the creation of new instances but not inheritance, if this is the case, the class should be declared as sealed.