OOPS Interview Questions and Answers (133) - Page 5

When to use Interface?

Interface General Explanation

we can implement multiple inheritance with help of Interface and it will contain signature of methods or events no implementation will be there.

When to Use it

if we want to Implement same signature in different class then we can use it.
consider a company where there are staffs and supporting staffs. All will be given salary but staff salary calculation is different from supporting staff salary calculation.

here is how it is implemented

public interface ISalary
{
decimal CalculateSalary();
}

public class Staff : ISalary
{
public decimal CalculateSalary()
{
//Staff salary calcuation logic
}
}
public class SupportingStaffs :ISalary
{
public decimal CalculateSalary()
{
//Supporting Staff salary calcuation logic

}
}

Function Overloading is also called as?

NOTE: This is objective type question, Please click question title for correct answer.
Function Overriding is also called as?

NOTE: This is objective type question, Please click question title for correct answer.
Same Function Name but Different Signature or Parameter is called as?

NOTE: This is objective type question, Please click question title for correct answer.
Can we define Method Overloading in different classes?

No,we can not define Method Overloading in different classes.It must be on the same class.
As we know that Function Overloading is defined in the same class.

We can understand it by an example:-

Below function are an example of Overloaded Constructor,which are defined on the same class.

public class Person
{
public Person()
{
}

public Person(int person_id)
{
}
}

If we define same function in different classes,so they will not be called as function overloading.
Can Function Overriding be defined in Same Class?

No,Function Overriding can only be defined in different class because it treated as Base and Derived or parent-child relationship.

We can understand it by an example:-

public class Base_Class

{
public Base_Class()
{
}

public virtual void display()
{
Console.WriteLine("base class");
}
}

public class Derive_Class:Base_Class
{
public Derive_Class()
{

}

public override void display()
{
Console.WriteLine("derive class");
}
}


Note:- The Override keyword is used in Function Overriding to override methods.
Below C# function is of which type? public static Calculate operator +(Calculate c1,Calculate c2) { }

NOTE: This is objective type question, Please click question title for correct answer.
Does Function Overloading depend on Return Type?

No,It does not depend on Return Type.Because if return type is different and function name as well as parameter is also same.
Then it will give compile time error.

Below example will give us clear picture:-

public void display(int id)

{
}

public int display(int id)
{
}


Once we compile above example then it will give compile-time error as

Type '_Default' already defines a member called 'display' with the same parameter types

Here _Default is a Page Name.
What are the points which fulfill Function Overloading?

Following are the key points which fulfill Function Overloading?

1). Number of parameters.
2). Order of parameters.
3). Data-type of parameters.

We can understand this by an example:-

public void  display(int id,string name)

{
}

public void display(string name,int id)
{
}

public void display(string name,string value,int id)
{
}

public void display(string id,string name)
{
}

What will happen,if we compile below function? private double sum(int a, int b) { } private double sum(int i, int j) { } Note:- In above both function return type is same as Double.

If we compile above function,then it will throw an error saying that:-

Type '_Default' already defines a member called 'sum' with the same parameter types.


Here _Default is a page name.
What will happen,if we compile below function? private double sum(int a, int b) { } private int sum(int i,int j,int k) { } Note:- In above both function return type is different but no of parameters are also different.

It will not compile,will give us an error saying that:-

"Type '_Default' already defines a member called 'sum' with the same parameter types"


Here _Default is a page name.

We can not say that it's a Function Overloading.Because it does not depend upon return type.
What is the best example of Function Overloading?

Overloaded Constructor is the best example of Function Overloading.

For ex:-

In VB.Net
Public Class Person

Sub New()
End Sub

Sub New(ByVal person_id As Integer)
End Sub
End Class


In C#
public class Person

{
public Person()
{
}

public Person(int person_id)
{
}
}

What will happen,if we compile below code public interface intf_person { public int save_data(Person obj); public DataSet load_data(); }

It will not compile.As we can see,we have taken 1 interface intf_person, inside interface,we defined 2 method with public access modifiers.

If we compile,then it will give compile error as

"The modifier 'public' is not valid for this item"
Meaning that we can not write public access modifiers inside any interface.Because all the methods inside Interfaces are Public By-default.So we do not have to write Public keyword inside Interface.
What is final class related to OOP's

A class that is defined as final class can not be inherited further. All Methods of a final class are inherently final and must not be declared as final in the class definition.
In java there is keyword “final”,but In c# there is no keyword like “final” but the same thing is achieved by keyword “sealed”.

namespace OOPS_Concept
{
sealed class SealedClassDemo
{
public void test1()
{
Console.WriteLine("Method Test1");
}
}

class childclass : SealedClassDemo
{
}
}
Above code will generate an error saying that class cannot be inherited.

In C#, Methods cannot be “sealed” directly. Methods of only derived class can be made sealed with keyword sealed and override.

namespace OOPS_Concept
{
class SealedMethodDemo
{
public virtual void Method1()
{
Console.Write("Base class Method1");
}
}

class ChildClass : SealedMethodDemo
{
public sealed override void Method1()
{
Console.Write("Derived class Method1");
}
}
}
How can you define polymorphism?

The word polymorphism is meant for different forms.
It means the ability of an entity to exhibit different forms at runtime, also allows methods and properties in multiple classes to have the same signature with different behavoiur.
Explain Encapsulation?

It is an approach to implement abstraction.
Abstraction is the process of hiding complexity or unwanted details. Encapsulation helps to achieve data hiding through which data security is achieved.
Explain the concept Data Hiding?

A class contains private data members which are not accessible to the users of other class.
these data members can be modified only internally by the class.
Class contains public functions through which user can interact with these data which implies that direct manuplation of data is not possible by the user.
How to declare an array?

A array can be created by using the 'new' keyword.
Syntax:
arrayRefVar = new dataType[arraySize];
It creates an array using new dataType[arraySize].
It assigns the reference of the newly created array to the variable arrayRefVar.

Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be combined in one statement, as shown below:

dataType[] arrayRefVar = new dataType[arraySize];

Alternatively way to create arrays:

dataType[] arrayRefVar = {value0, value1, ..., valuek};

The array elements are accessed through the index.
what is the use of private constructor?

When we declare a class constructor as private , we can not do 2 things:-

-- We can not create a object of the class from out side of the class.
-- We can not inherit the class.

Private constructor is used for controlling the object generation which Is singleton design pattern implementation..

public class Singleton

{
private static Singleton instance = new Singleton();
//Private constructor, which does not allow object creation
//from outside the classs..
private Singleton() { }

public static Singleton GetInstance
{
get
{
return instance;
}
}
}

Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories