Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is the difference between abstract class and interface? Please give an example with scenario when i use abstract class and why? when i use interface?

user-image
Question added by mohd ikram , Software Engineer , VSK IT Services Pvt. Ltd.
Date Posted: 2016/08/04
Manikandan natarajan
by Manikandan natarajan , Programmer , Dot Ne

we can use only one abstract class at a time but multiple interface can be created

Abhijeet Saxena
by Abhijeet Saxena , Software Engineering Analyst , Accenture Services Pvt. Ltd

  • Abstract class methods may have implementation i.e. it may contain non-abstract methods. Interface will not have any kind of implementation in it.
  • Scenarios to choose between Interface and Abstract Class:
  • To choose Interface : Assume you have 6 functionalities(methods) to be implemented to any Class which implements the interface and you know that the no. of functionalities may increase (suppose 4 new func. which are totally unrelated to existing behaviour, corresponds to multiple inheritance) in  the future. To tackle this , Class C1 : I1{// existing 6 functionalities of Interface I1}Now class C2 has to implement existing 6 and 4 new functionalities.
  • So its easy to implement all 10 together in class C2. public class C2 : I1, I2{// 6 func. of I1 and 4 new func. of I2}

To choose Abstract class : to maintain the common behaviour (acquired by base/ abstract class) into the child classes which implements the abstract class.

 

 

awais awais
by awais awais , Lead c# Developer , Integrated Security solution

i will show the code example

public abstract class Employee{

public virtul string FullNamePrint()

{ return "Awais Shabir";

}

}

// Interface Example

 

Public Interface IEmployee

{string FullNamePrint();

}

if you are using multi inheritance then used interface because interface supports multi inheritance

More Questions Like This