473,395 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

When are interfaces preferred instead of Abstract classes?

9
Hi,

Could someone please tell me why interfaces are prefered over abstarct classes? Even abstract classes can be subclassed by more than one class just the way interfaces are implemented by many classes. In such a situation, why do we favour interfaces more than abstract classes?
Jul 30 '08 #1
3 3689
JosAH
11,448 Expert 8TB
You can only extend from one super class but you can implement many interfaces.

kind regards,

Jos
Jul 30 '08 #2
BigDaddyLH
1,216 Expert 1GB
Could someone please tell me why interfaces are preferred over abstract classes?
Java has both, and both are widely used, so it's not "use interfaces not abstract classes" but use the right one.

One big advantage to an abstract class is that it can contain code common to its subclasses. One big advantage to interfaces is that a single class can implement more than one interface, as Jos mentioned. As an example, class java.util.LinkedList implements all the interfaces Serializable, Cloneable, Iterable<E>, Collection<E>, Deque<E>, List<E> and Queue<E>.

Can you have it both ways? Yes! There's a common pattern to the models in Swing:
Expand|Select|Wrap|Line Numbers
  1. public interface ListModel {...}
  2.  
  3. public abstract class AbstractListModel implements ListModel {...}
  4.  
  5. public class DefaultListModel extends AbstractListModel {...}
Here, interface ListModel describes what it takes to be the data inside a JList widget. Abstract class AbstractListModel provides the boilderplate code common to many list models -- you just have to implement two methods to complete it. Finally, DefaultListModel provides a complete solution that works right out of the box, in many cases. With this design you have plenty of options:
  1. Directly implement ListModel
  2. extend AbstractListModel
  3. directly use DefaultListModel
  4. extend DefaultListModel further
  5. etc...
So it's quite a flexible design, thanks to its combination of interface and abstract class.
Jul 30 '08 #3
shoby
9
Thanks Jos n BigDaddy for the repiles...
Jul 31 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Vishal Gandhi | last post by:
Hi , Please help me by advising an real life scenario where Abstract Classes should be used over Interfaces or vice versa . Whats the basic difference between Abstract Class and interface other...
12
by: Peter N Roth | last post by:
If i build a class in C#, i can declare a method abstract. If someone derives from this class, the abstract method must be overriden for the computation to succeed. ISTM that an Interface...
10
by: Michael Grimsley | last post by:
I have been looking at interfaces in C# and cannot seem to understand them. I am told that interfaces resembles a class with no code and that methods declared in an interface are not allowed to...
12
by: Anders Borum | last post by:
Hello! I was wondering why we're allowed to define interfaces as internal, when all the members of the interface, when implemented in a class, are made public? I know that interfaces serve as...
17
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
5
by: Michael McCarthy | last post by:
I want to develop plugin support for a system.montitor module I am working on. A lot of the modules will do mostly interop stuff for an older system, but I want to use it myself as well to monitor...
6
by: Steve | last post by:
I am designing a new class hierarchy and I've already run into a bit of a bump in the road. I have this structure so far; class CodeGen class CodeGenHeader : CodeGen class CodeGenProtocolHeader...
18
by: abcd | last post by:
Can someone tell me whats the exact advantage of Interfaces in C#. To me abstract classes serves the purpose and have all good things then what is special about interfaces...can someone tell me...
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.