ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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
تم إضافة السؤال من قبل mohd ikram , Software Engineer , VSK IT Services Pvt. Ltd.
تاريخ النشر: 2016/08/04
Manikandan natarajan
من قبل Manikandan natarajan , Programmer , Dot Ne

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

Abhijeet Saxena
من قبل 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
من قبل 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

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟