jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructors" Watch "constructors" New topic
Author

constructors

Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88
Why cant consructors be marked final and absract?
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

Why do you want to mark constructors as final or abstract?


SCJP 5, SCWCD 5
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

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


http://plainoldjavaobject.blogspot.in
Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88
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

Joined: Jun 08, 2010
Posts: 384

@ 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

Joined: Jun 08, 2010
Posts: 384

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

Joined: Jul 26, 2005
Posts: 189

In fact constructors are final by default.

When there is a chance to modify things final makes sence to restrict the usage.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
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

Joined: Jul 26, 2005
Posts: 189

Ofcourse we can say it accurate. Happy to discuss if you have any other ideas.
Masa Saito
Greenhorn

Joined: Jun 08, 2010
Posts: 14
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructors