• 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

Interfave vs. Abstract

 
Greenhorn
Posts: 9
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi buddy, well i got that there is always a default constructor with abstract class. OK. But why not with interface?

Thanks in advance.
 
Sheriff
Posts: 22781
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

Originally posted by Tanzeem Akhtar:
Hi buddy, well i got that there is always a default constructor with abstract class. OK.


No there isn't. Abstract classes use the same approach to constructors as any non-abstract class - it only adds a default constructor if you haven't provided one. Nothing prevents me from writing only one constructor that takes a string, and there will be no default constructor without arguments then.

But why not with interface?


Because there is nothing to construct in interfaces. Instead, the constructor from the extended class is used. So in the case where you extend nothing explicitly, the Object() constructor is used, not the constructor from any implemented interface.
 
Stop it! You're embarassing me! And you are embarrassing this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic