Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between abstract and interface,not Syntactical diff

 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can't instantiate an interface like

List<Integer> iList = new List<Integer>();
 
Zaheer A Baloch
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ali!

Your response is a little tricky! Is abstract class instantiable?
 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Plese don't ask the same question on several fora.
 
gupta prakash
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gupta Prakash,

In Abstract class ,Concreate methods and abstract methods declaration. Abstract methods are not compusory in the abstract class.
 
Gayk Gevorkyan
Greenhorn
Posts: 26
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic