• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Constructor's in Interface. Why not ?

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

Can anyone expalain me, why we cannot specify the types of constructor
a class should have, implementing a particular interface.

e.g interface I{
I();
I(String name);
I(String name, int i);
}

Thanks,
Ramdas.
 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ramdas ,
Remember constructors are never inherited /over ridden by subclasses..

And all methods in interface are meant to be overriden/defined by implementing classes...( an interface is similar to an abstract class having all methods declared as abstract )

---
Shivani.
 
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
We need a constructor to instantiate, in the sence, to allocate memory for a perticular instance, but Interfaces are never instantiated, cause they are by default abstract. So having a constructor is not needed. Regards.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing I want to add is we cannot declare constructors in an interface
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I met the same issue and I use abstract class to solve it. because I need some variable too, so I declare them under protected so that they can be used by subclass.
 
R Sawant
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shivani,

Thanks. But can u explain why actually the constructor is not inherited ?
Upon thinking on your reply i formed this opinion.

"Actually the constructor is no where related to the object. An object constitutes of properties & behaviour, i.e fields & methods.
A class extending another class inherits the actual characteristics of the object, of which the constructor is not part of. Constructor is not a part of object, it's a feature provided by java."

Your thoughts will be helpful.

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

I think this might help you.

The constructor is executed everytime an object is instantiated.
So if we want any properties of the class to be added dynamically when the object is created we specify them in constructor.

So the constructor is specific to a particular class.

What happens if we inherit a constructor?
If the constructor is inherited then the constructor should behave as a method which it is not.That is why we cant call the constructor using a reference as we call method.
So I think for the above reason constructor is not inherited.
Please let me know if am wrong.

Thanks & regards,
Charan.
 
Poop goes in a willow feeder. Wipe with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic