• 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

abstract and interfaces

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could any one tell me the exact difference between abstract and interface class
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) A class can extend only one abstract class but implement multiple interfaces.

2) An interface can only declare constants (any field) and public abstract methods (any method).
An abstract class can have anything other classes can have: private fields, instance fields, protected members, constructors, you name it. Unlike other classes, it can also have abstract methods. Unlike interfaces, these abstract methods can also be protected or have no access modifier.
Please note that they cannot be private - private methods cannot be overridden, and therefore private abstract methods would never get an implementation.
 
hemapriya rao
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)so,should a immediate subclass of abstract class implement(define) all methods of abstract class?

2)when the class implements interface ,should that class override all methods of the interface?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For both situations, a class must either implement all abstract methods (so also methods defined inside an interface the class is implementing), or be abstract itself.

If a class is not abstract, it must make sure that all remaining abstract methods must be implemented.

Example:
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a frequently asked question.
See the answer in the JavaRanch FAQ: InterfaceVsAbstractClass
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like to add one point in term of use of Abstract class and Interface:-

It's generally considered good OO practice to specify the "contract" which you want to adhere to via Java interfaces.

Use abstract Java classes when you want to provide some standard base code but want/need to force the user's of your class to complete the implementation.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic