interface and abstract class

S

sandy

what is the main difference b/w interface and abstract class.
in which situation we will use choose interface and abstract class
 
R

Richard Reynolds

sandy said:
what is the main difference b/w interface and abstract class.
in which situation we will use choose interface and abstract class
Generally abstract classes are used for inheritance of (at least some)
implementation.
Interfaces are used for inheritance of type.

Another thing to note is that, in Java, you can extend only one class while
you can implement multiple interfaces.

Of course this is a massive simplification, there are many other factors to
take into account, mostly to do with OO design.
 
R

Robert Mark Bram

what is the main difference b/w interface and abstract class.
in which situation we will use choose interface and abstract class

To add my own 2 cents..

Interfaces define ONLY abstract methods i.e. methods with no
implementation.
Abstract classes can define abstract methods and concrete methods i.e.
methods with an implementation.

A class EXTENDS an abstract class.
A class IMPLEMENTS an interface.

A class can only extend one class, but can implement many interfaces.

Rob
:)
 
T

Tom Hawtin

Richard said:
Another thing to note is that, in Java, you can extend only one class while
you can implement multiple interfaces.

That's not necessarily to say that makes interfaces better than abstract
classes. Using an abstract class allows the compiler to detect
impossible casts. Multiple inheritance (of interface) tends to indicate
bad design (IMO).

In reality, (pure) abstract class or interface makes no significant
difference to client code. The main advantage of interface is that the
code is cleaner (doesn't require public abstract on methods of public
static final on fields). It also signals an intent that the type is
contains no implementation.

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top