This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between abstract and interface,not Syntactical diff Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between abstract and interface,not Syntactical diff" Watch "Difference between abstract and interface,not Syntactical diff" New topic
Author

Difference between abstract and interface,not Syntactical diff

Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
What is difference between abstract and interface

some of the obvious ones are,

1)In interfaces,concrete methods are not allowed and some syntax differences..

2)abstract is meant to be used for IS-A Relation ship, and interface is used for IS-HAS a relation ship.

that is,as GANG OF FOUR suggests interfaces can be used for supporting AGGREGATION OR COMPOSITION OVER INHERITANCE.

But,abstract also supports these features


Is there any other differences ,
[ January 26, 2008: Message edited by: ramya sri ]
Ali Khalfan
Ranch Hand

Joined: Nov 03, 2007
Posts: 126
you can't instantiate an interface like

List<Integer> iList = new List<Integer>();
Zaheer A Baloch
Greenhorn

Joined: Jan 24, 2008
Posts: 18
Hi Ali!

Your response is a little tricky! Is abstract class instantiable?


Zaheer Baloch
SCJP 5.0 (84%), SCWCD 5.0 (95%)
Milton Ochoa
Ranch Hand

Joined: Oct 23, 2007
Posts: 336

hi ramya

I dont get one thing:

you say:

...and interface is used for IS-HAS a relation ship.

What is that relationship??? please giveme one answer or somebody
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
Plese don't ask the same question on several fora.
gupta prakash
Greenhorn

Joined: Jan 25, 2008
Posts: 5
1) For Interfaces, you cannot have any kind of concrete method.

But in the abstract class, apart from having only abstract methods you can also have some methods for which implementation is provided. i.e., you will have atleast one method declared as abstract.

2)And also Interfaces can extend 1 or more interface, whereas the abstract classes cannto extend more than 1 class.

In other words.. abstract classes are some what flexible when compared to interfaces.
Dean Jones
Ranch Hand

Joined: Dec 29, 2007
Posts: 129
Hi ramya, I didn't understand the following lines
abstract is meant to be used for IS-A Relation ship, and interface is used for IS-HAS a relation ship.

that is,as GANG OF FOUR suggests interfaces can be used for supporting AGGREGATION OR COMPOSITION OVER INHERITANCE.

But,abstract also supports these features

Can anyone please explain?
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
Hello,
replies i got is only syntactical differences,anyway thanks....

If you observe ,interfaces allows you to type cast ,with out touching the

inheritance class hierarchy,so interfaces makes easy to do AGGREGATION over Inheritance.
Where in other languages like C++, they has to use Inheritance to do Aggregation ( for type casting Problems)



Correct me if i am wrong.......
siva sankara kumar
Greenhorn

Joined: Jan 28, 2008
Posts: 3
hai sri


Abstract class is to avoid object creation
in this, defined and abstract method is possible the abstract method should override.

for example consider "livin_being" as a class in this catagory human ,animal or living being are possible so we can declare generalised class as abstract from this we can extend subclass such human,animal,etc.,


Interface is totally different concept don't relate with abstract

it is just like a template, if we need we can use it, if we use interface we must override because,
all the methods are abstract method. it is one way of achieveing multiple inheritance but java never support multiple inheritance


so don't compare abstract and interface
siva sankara kumar
Greenhorn

Joined: Jan 28, 2008
Posts: 3
hai sri


Abstract class is to avoid object creation
in this, defined and abstract method is possible the abstract method should override.

for example consider "livin_being" as a class in this catagory human ,animal or living being are possible so we can declare generalised class as abstract from this we can extend subclass such human,animal,etc.,


Interface is totally different concept don't relate with abstract

it is just like a template, if we need we can use it, if we use interface we must override because,
all the methods are abstract method. it is one way of achieveing multiple inheritance but java never support multiple inheritance

by siva

so don't compare abstract and interface
sitaram
Greenhorn

Joined: Jan 24, 2008
Posts: 26
Hi Gupta Prakash,

In Abstract class ,Concreate methods and abstract methods declaration. Abstract methods are not compusory in the abstract class.
Gayk Gevorkyan
Greenhorn

Joined: Feb 08, 2008
Posts: 26

Some differences between abstract classes and interfaces is:

1. You can extend only one class (abstract or not abstract) but you can implement multiple interfaces. Thus interfaces make multiple inheritance possible.

2. Abstract classes can have private, default, protected members. Interfaces have only public abstract methods and public static final fields.

3. Interface is a contract between the class (Server) that implements the interface and classes (Client) that use the class through reference to the interface. Abstract class is considered to be more general version of more concrete subclass, but not a contract.

4. Abstract classes can have constructors. Interfaces cannot.


SCJP 6 (96%), SCWCD 5 (88%)
 
Consider Paul's rocket mass heater.
 
subject: Difference between abstract and interface,not Syntactical diff
 
Similar Threads
interface and abstract class
Anstract class, Interface real world scenario
What is difference between an Interface and an abstract class
Difference between abstract and interface,not the obvious one
Abstract class and interface