• 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

constructors

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why cant consructors be marked final and absract?
 
Ranch Hand
Posts: 198
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you want to mark constructors as final or abstract?

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

Rajiv Chelsea wrote:Why cant consructors be marked final and absract?



constructors can't be abstract because they are the initial constructing points of a class' object and you can't just leave them abstract

they can't be final because then you won't be able to override them or overload them
 
Rajiv Chelsea
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lalit
perhaps you dont know we can certainly
overload constructors.

But they are not inherited and hence cant be overridde.

I want the reason why they cant be inherited
and also why they cant be marked as final?
 
Lalit Mehra
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Rajiv ...

sorry buddy ... i didn't take it right ... yes of course they are not inherited and hence can not be overridden ...

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

Rajiv Chelsea wrote:
I want the reason why they cant be inherited
and also why they cant be marked as final?



i guess since if you will be allowed to inherit a constructor you certainly would be able to call it without creating an object of that class ... which is not permissible

moreover since you cannot have two classes with same name in a single package or even a file ... it makes sense why they cannot be overridden ...
 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 198
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact constructors are final by default.

When there is a chance to modify things final makes sence to restrict the usage.
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prabhakar Bokka wrote:In fact constructors are final by default.


Interesting, I never considered that. Would it be more accurate to say constructors are always final?
 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 198
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ofcourse we can say it accurate. Happy to discuss if you have any other ideas.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajiv Chelsea wrote:Why cant consructors be marked final and absract?


Constructors are not methods, and that is the fundamental answer.
The term "abstract" is only relevant to the idea of "polymorphism": (run-time vs. compile-time decision making). So consider how construction works:

"Object obj = new Whatever(x, y, z);"

As you can see, at compile-time, we already know an instance of "Whatever" is created. There is no dynamism here. Which constructor to use? Well.. its the constructor for "Whatever".

Further, the fathers of Java only want a constructor to initialize the state of an object. Set the instance variables to values that allow the object to work correctly. So, a class can say: "I need this constructor with those variables to initialize myself and be ready to rock and roll". But why would it make sense for a class to tell its subclasses which variables it needs to initialize themselves (ie. an abstract constructor)? A class knows nothing about its subclasses.

Finally, constructors are not "inherited" in the normal way people talk about inheritance because they are not methods. Constructors are "hidden" not "overrridden", by subclasses, so "final" does not make sense in a constructor. Using the "super" keyword, you do have access to public and protected constructors in your parent class.

If you could give some very specific examples of what you are asking about, that would help the discussion.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic