• 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

when to use abstract ?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems people are prefer to use interface for its flexibility. My question is when wee need to use abstract over interface ?

thanks !
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A class must be declared abstract when it has an abstract method. Its common to declare an abstract class when the class represents a base class in a hierarchy. Any extender that's not abstract must implement its abstract methods.

Interfaces are public contracts. They list the signatures of public methods that implementers must implement.
You can't instantiate interfaces (only anonymous inner interfaces) or abstract classes.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I frequently use both. An interface to define the public API, and an abstract base class to provide common implementation.


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


Abstract class vs an interface;
An interface is like a 100% Abstract class where ALL the methods are implicitly public abstract and it is up to the class implementing the interface to provide an implementation for all these methods.

On the other hand, the Abstract class might have some OR none of his methods abstract: if you declare just one method to be abstract then you must declare the class to be abstract, however you can still declare a class to be abstract without having any of his methods declared as abstract method. Moreover an abstract class CANNOT be instantiated (ex. you cannot create objects of the abstract class) , it can only be extended so that implementations could be given to the abstract methods if he has any.

I hope this helped clarify the differences.


 
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
Have a look at Interface vs. abstract class in our FAQ.
 
I child proofed my house but they still get in. Distract them with 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