• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

diff b/w abstract class & interface

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi pals,

can you tell me a difference between abstarct class and interface with proper real time example.


Thanks,
shailaja
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have a FAQ about this topic here.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well lets not get far away to get a practical use. Take java.util.Calendar for example. It is an abstract class. So it provides implementation for most of the methods leaving a few. The ones left are too specific for a generic calendar to implement. But java.util.Collection is an interface as everything related to it is too specific for a generic collection to implement...
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shialaja siddappa wrote:Hi pals,

can you tell me a difference between abstarct class and interface with proper real time example.


Thanks,
shailaja



Interfaces are used to add optional behaviour in class. Consider Comparable interface that allows a class to make sure that its instances are ordered with respect to other mutually comparable objects. Such interface is called a Mixin. You cannot define Abstract Classes as mixin.

 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Interfaces are used to add optional behaviour in class.



Not optional behavior. Having a class implement an interface means that you're saying that the class will perform behaviors (methods) specified by the interface.

John.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have read all the reply's, but for me this is the explanation:

The basic difference is that an interface is like a 100% abstract class, that means ALL of his methods are abstract, ALL. But an Abstract class can have, non Abstract methods too, so you have an specific behaviour, this is one's of the differences, think about it, you are getting a more specific behaviour with an abstract class.

The other difference is that in JAVA you can not extend more than one class, if you make an abstract class, you have to extend it, and you can not extend any other class, you loose that posibility. But is different with an interface, you can implement one or more interfaces, and have the posibility to extend any other class, so is more flexible, depends on what you need.

I hope this could help you, Bye.

Christian Polychroniadis soon SJCP...
 
Muhammad Khojaye
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John de Michele wrote:

Interfaces are used to add optional behaviour in class.



Not optional behavior. Having a class implement an interface means that you're saying that the class will perform behaviors (methods) specified by the interface.

John.



Ohh... you didn't understand my point. Let takes an example of Serialization. Its an optional behavior for any class to implement, but ones implemented, offcourse it becomes necessary for a class to implement all those methods defined in Interface.
 
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic