Part - 5 : Oops Interview Question's Answer



41. What is mean by abstraction?

Abstraction is the process of showing necessary information and hiding unwanted information. Let us consider the "CalculateSalary" in your Employee class, which takes EmployeeId as parameter and returns the salary of the employee for the current month as an integer value. Now if someone wants to use that method. He does not need to care about how Employee object calculates the salary? An only thing he needs to be concern is name of the method, its input parameters and format of resulting member

42. What is mean by abstraction class?

Abstract classes contain one or more abstract methods that do not have implementation. An abstract class is a parent class that allows inheritance but can never be instantiated. Abstract classes allow specialization of inherited classes.

43. What id mean by Interface?

Interface defines the set of properties, signature of the methods and events. It does not include any implementation. Class which implements the interface can provide the behavior to the implemented method. For example two class MyEnglishClass and MyFreanchClass implementing same interface and provide two different set of behavior in their implementation.
public interface IMyInterface
 {
   string Hello(string name);
 }

public class MyEnglishClass:IMyInterface 
 {
  public string Hello(string name)
   {
     return "Hello " + name;
   }
 }
public class MyFrenchClass : IMyInterface
 {
  public String Hello(string name)
   {
     return "allo " + name; 
   }
 }

44. What is difference between Abstract class and Interface?

  • In Interface all the method must be abstract; in abstract class we can have both abstract and concrete methods.
  • Access modifiers cannot be specified in interface because it should always be public; in Abstract class, we can specify the access modifier.

45. In which Scenario you will go for Abstract or Interface Class?

Abstract classes are useful when creating components because they allow you specify an invariant level of functionality in some methods, but leave the implementation of other methods until a specific implementation of that class is needed. They also version well, because if additional functionality is needed in derived classes, it can be added to the base class without breaking code.Interfaces are often used to describe the peripheral abilities of a class, not its central identity, e.g. an Automobile class might implement the Recyclable interface, which could apply to many otherwise totally unrelated objects.

46. What is mean by polymorphism?

Polymorphism means the ability to take more than one form. An operation may exhibit different behaviors in different instances. The behavior depends on the data types used in the operation. Polymorphism is extensively used in implementing Inheritance

47. What are different types of polymorphism?

There are two types of polymorphism
Static polymorphism - defining the method with same name and different signature is called as static polymorphism. In the below example there are three different Add() functionality this Add() will be executed based on the parameter passed.
Example :
public int Add(int a, int b)
 { 
   return a + b; 
 }
public double Add(double a, double b)
 {
   return a + b;
 }
public long Add(long a, long b)
 {
   return a + b;
 }
Dynamic polymorphism Dynamic polymorphism can be implemented using Virtual and Override keyword. By using polymorphism, each derived class can have its own behavior, Even though classes are derived or inherited from the same parent class
Example:
In the below example ClassB is inherited from ClassA. ClassB can have its own behavior by overriding the parent class method. Parent class method should be represented with virtual keyword to override the same method in derived class.
public class ClassA
{
 public virtual void Display()
  {
    Console.WriteLine ( "ClassA");
  }
}

public class ClassB:ClassA 
 {
  public override void Display() 
   {
     Console.WriteLine ( "ClassB");
   }
 }

static void Main(string[] args)
 {
   ClassA a = new ClassA();
   ClassB b = new ClassB();
   ClassA c = new ClassB();
    a.Display();
    b.Display();
    c.Display();
   Console.ReadLine();
 }

OutPut :
======
      ClassA
      ClassB
      ClassB


48. What you mean by Encapsulation?

Encapsulation is the procedure of covering up of data and functions into a single unit and protects the data from the outside world. Example "Class" only public functions and properties are exposed; functions implementation and private variables are hidden from outside world.

49. What is difference between data encapsulation and abstraction?

Abstraction refers to the act of representing essential features without including the background details or explanations. Storing data and functions in a single unit is called as encapsulation.

50. What is mean by Delegate?

Delegate is a type that holds a reference to a method or a function. . Once a delegate is assigned a method, it behaves exactly like that method. We can call the method using delegate instead of directly calling the method. Using delegate, we can also pass the parameter and get return value. Any method with matched the signature of the delegate can be assigned. Simply we can say .NET implements the concept of function pointers using delegate.
Example :
There are three step to following for using Delegate
  • Declaration
  • Instantiation
  • Invocation
In the below example we have declared the new delegate "MyDelegate", which accept string as parameter and return value as string. Two methods SayHello and SayBye function will be called using delegate.

//Declaring the delegate
 delegate string MyDelegate(string name);

//function called by delegate dynamically
private static string SayHello(string name)
 {
   return "Hello " + name;
 }

private static string SayBye(string name)
 {
   return "Bye " + name;
 }
After declaration of delegate, we have initialized with SayHello function. Now this delegate will hold reference to specified function. Function will be called using Invoke () method of delegate. In this example we have called two methods (SayHello and SayBye) with same signature(parameter type and return type).
static void Main(string[] args)
 {
  //Initialllizing delegate with function name
  MyDelegate delg = new MyDelegate(SayHello);

  //Invoking function using delegate
   Console.WriteLine(delg.Invoke("Sam"));
   delg = new MyDelegate(SayBye);

  //Invoking diffent function using same delegate
   Console.WriteLine(delg.Invoke("Sam"));
   Console.ReadLine();
 }

OutPut :
======
      Hello Sam
      Bye Sam
0 Komentar untuk "Part - 5 : Oops Interview Question's Answer"

Back To Top