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

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

متابعة

Which one is better to use abstract class or interface in java ? in which condition we use interface or abstract class ? give example in real app .

user-image
تم إضافة السؤال من قبل Mohd shahnawaz khan , Associate Project , Cognizant Technology Solution
تاريخ النشر: 2014/01/10
Huzefa chikhly
من قبل Huzefa chikhly , JAVA DEVELOPER , PRIMALINK SOLUTIONS INDIA PVT LTD

the basic diffrence between interface and abstract class is, interface have all the method declaration only but abstract class can have method declaration as well as method defination also.. bt if we use abstract class and if we inherit tht class then we cant inherit other class this is boundation.bt no such boundation is with interface.

Which one is better to use abstract class or interface in java? - interface and abstract classes cannot be used interchangeably. Both of them have separate intentions in the Java programming language. Interfaces do not have any functionalities built into them, where as abstract classes have some of their functionalities (methods) already defined. Now whether you want to make a class as interface or abstract depends on how much of the classes functionality (methods) can be pre-determined and will not change. If at least one of the method that you define in a class can be predetermined (and you can code the same) then you can create an abstract class instead of an interface.

 

Cedric Nabaa
من قبل Cedric Nabaa , Java developer , Capital Banking Solutions

Which one is better to use? this will depend on urobject model. For example let us say that you have a class Animal and two classes Cat and Dog. A cat IS an Animal so here you should implement Animal as an abstract class. Furthermore, the Cat and the dog both eat. so you would want to implement the eat method once in animal and have ur Cat class and Dog class inherit the same eating method. So an abstract class represent an IS a relation due to the inheritance it generates. On the other hand, an interface is only a contract that you have to respect. It is less allienating than an abstract class. if you have a specific case please share it 

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

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