• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Difference between abstract and interface,not Syntactical diff

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can't instantiate an interface like

List<Integer> iList = new List<Integer>();
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ali!

Your response is a little tricky! Is abstract class instantiable?
 
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76884
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Plese don't ask the same question on several fora.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.......
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 26
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
bacon. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic