Wednesday, November 23, 2011

Abstract Class & Interface Interview Questions

Abstract Class & Interface Interview Questions

What is abstract class?
Ans: An abstract class is a special kind of class that cannot be instantiated, it only allows other classes to inherit from it. It consists of methods and variables. Methods can have their own default implementation. The class which derived from abstract class not necessary to implement all methods of abstract class. Abstract class cannot be sealed. An abstract class is an incomplete class and must be implemented in a derived class.

Can you create an instance of an abstract class?
Ans: No, abstract classes are incomplete and you cannot create an instance of an abstract class.

What is an Interface?
Ans: An interface is a standard or contract that contains only the declaration of methods (signatures of methods or events.) not implementation. The implementation is done in the class that inherits from this interface. Interfaces are primarily used to set a common standard or contract. The class which derives from interface must contain implementation of all methods of interface. An interface is not a class.

What is the difference between an abstract class and interface?








Explain me scenario where to use abstract class? OR
Give me situation where you will use abstract class ?
Ans: 1) If you want to update base class throughout life of your program, it is best to use base class as abstract class. Because you can make a change to it and all of the inheriting classes will now have this new functionality. If the base class will be changing often and an interface was used instead of an abstract class, we are going to run into problems. Once an interface is changed, any class that implements that will be broken.
2) If you are creating something that provides common functionality to unrelated classes, use an interface.
3) If you are creating something for objects that are closely related in a hierarchy, use an abstract class.
4) When you want some default implementation of methods of base class then use abstract class.
5) When you want some variables that has to be use by several derived type as well base type also in this case you have to use abstract class.


Using the Code
Let me explain the code to make it a bit easier. There is an Employee abstract class and an IEmployee interface. Within the Abstract class and the Interface entity I am commenting on the differences between the artifacts.
I am testing both the Abstract class and the Interface by implementing objects from them. From the Employee abstract class, we have inherited one object: Emp_Fulltime. Similarly from IEmployee we have inherited one object: Emp_Fulltime2.
In the test code under the GUI, I am creating instances of both Emp_Fulltime and Emp_Fulltime2 and then setting their attributes and finally calling the calculateWage method of the objects.



Abstract Class Employee

using System;

namespace AbstractsANDInterfaces
{
///


/// Summary description for Employee.

///


public abstract class Employee
{
//we can have fields and properties


//in the Abstract class

protected String id;
protected String lname;
protected String fname;

//properties


public abstract String ID
{
get;
set;
}

public abstract String FirstName
{
get;
set;
}

public abstract String LastName
{
get;
set;
}
//completed methods


public String Update()
{
return "Employee " + id + " " +
lname + " " + fname +
" updated";
}
//completed methods


public String Add()
{
return "Employee " + id + " " +
lname + " " + fname +
" added";
}
//completed methods


public String Delete()
{
return "Employee " + id + " " +
lname + " " + fname +
" deleted";
}
//completed methods


public String Search()
{
return "Employee " + id + " " +
lname + " " + fname +
" found";
}

//abstract method that is different


//from Fulltime and Contractor

//therefore i keep it uncompleted and

//let each implementation

//complete it the way they calculate the wage.


public abstract String CalculateWage();

}
}
Interface Employee

using System;


namespace AbstractsANDInterfaces
{
///




/// Summary description for IEmployee.

///





public interface IEmployee
{
//cannot have fields. uncommenting


//will raise error!
// protected String id;
// protected String lname;
// protected String fname;


//just signature of the properties

//and methods.

//setting a rule or contract to be

//followed by implementations.


String ID
{
get;
set;
}

String FirstName
{
get;
set;
}

String LastName
{
get;
set;
}




// cannot have implementation


// cannot have modifiers public

// etc all are assumed public

// cannot have virtual


String Update();

String Add();

String Delete();

String Search();

String CalculateWage();
}
}
Inherited Objects
Emp_Fulltime:
using System;

namespace AbstractsANDInterfaces
{
///


/// Summary description for Emp_Fulltime.

///


//Inheriting from the Abstract class

public class Emp_Fulltime : Employee
{
//uses all the properties of the


//Abstract class therefore no

//properties or fields here!


public Emp_Fulltime()
{
}


public override String ID
{
get

{
return id;
}
set
{
id = value;
}
}

public override String FirstName
{
get

{
return fname;
}
set
{
fname = value;
}
}

public override String LastName
{
get

{
return lname;
}
set
{
lname = value;
}
}

//common methods that are

//implemented in the abstract class

public new String Add()
{
return base.Add();
}
//common methods that are implemented


//in the abstract class

public new String Delete()
{
return base.Delete();
}
//common methods that are implemented


//in the abstract class

public new String Search()
{
return base.Search();
}
//common methods that are implemented


//in the abstract class

public new String Update()
{
return base.Update();
}

//abstract method that is different


//from Fulltime and Contractor

//therefore I override it here.

public override String CalculateWage()
{
return "Full time employee " +
base.fname + " is calculated " +
"using the Abstract class...";
}
}
}
Emp_Fulltime2:
using System;

namespace AbstractsANDInterfaces
{
///

/// Summary description for Emp_fulltime2.


///


//Implementing the interface

public class Emp_fulltime2 : IEmployee
{
//All the properties and


//fields are defined here!

protected String id;
protected String lname;
protected String fname;

public Emp_fulltime2()
{
//


// TODO: Add constructor logic here

//

}

public String ID
{
get

{
return id;
}
set
{
id = value;
}
}

public String FirstName
{
get
{
return fname;
}
set

{
fname = value;
}
}

public String LastName
{
get
{
return lname;
}
set
{
lname = value;
}
}

//all the manipulations including Add,Delete,


//Search, Update, Calculate are done

//within the object as there are not

//implementation in the Interface entity.

public String Add()
{
return "Fulltime Employee " +
fname + " added.";
}

public String Delete()
{
return "Fulltime Employee " +
fname + " deleted.";
}

public String Search()
{
return "Fulltime Employee " +
fname + " searched.";
}

public String Update()
{
return "Fulltime Employee " +
fname + " updated.";
}

//if you change to Calculatewage().


//Just small 'w' it will raise

//error as in interface

//it is CalculateWage() with capital 'W'.

public String CalculateWage()
{
return "Full time employee " +
fname + " caluculated using " +
"Interface.";
}
}
}