This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why cannot constructors be final Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why cannot constructors be final" Watch "Why cannot constructors be final" New topic
Author

Why cannot constructors be final

Lokanadham Nalla
Greenhorn

Joined: Jul 21, 2005
Posts: 7
Hi there,

why can't constructors be final as we can not inherite constructors.

Loka
Vijay Gade
Ranch Hand

Joined: Jul 04, 2005
Posts: 81
I don't think it is anything to do with inheritance. I think it is just that if you have your constructor final, there is no point in creating a new object, because you would not be able to modify any fields/methods of that class. I might be wrong too...

Thanks,

-Vijay
Priya Jothi
Ranch Hand

Joined: Jul 13, 2004
Posts: 168
Hi,

Itz somehow related to inheritance.First of all u think when'll u use final modifier in class / method / variables(instance / local).

class - when class should not be subclassed

Method - when method should not be overridden

variable - when its value should not be modified

Now coming to constructors we can say it is a spl kinda method in a class.

No way constructors(of any class) are gonna be inherited by its subclass irrespective of its access modifier.So u cant override something which cant be inherited.So y do u wanna use final modifier with constructors?.

Something which doesn't give meaning cant / shouldn't be used..rite?!!

That must be the reason Sun wud have prevented using final modifier with constructors.

Hope u got my point!!!

Regards,
Priya.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
1. A subclass does inherit its base class' constructors. Otherwise, the subclass could not invoke the base class' constructor from its own constructors.

2. A constructor is not a method, so it cannot be overridden. So there is no need for a final qualifier.

(I have not read the previous post because it is not written in english)


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Vijay Gade
Ranch Hand

Joined: Jul 04, 2005
Posts: 81
Hello Barry,

You were saying something about the previous post not being written in English. Was that my post? Pardon my ignorance but I am just curious. Maybe it is not at all related to my post

Thanks,

-Vijay
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

1. A subclass does inherit its base class' constructors. Otherwise, the subclass could not invoke the base class' constructor from its own constructors.
I respectfully disagree. The subclass calls the superclass' constructor, not an "inherited" one. Only class members are inherited, and the JLS specifically excludes constructors from this classification (� 8.8: Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding.).

(I have not read the previous post because it is not written in english)

Agreed.

Please make the extra effort to write out words such as "why", "you", "would", etc. The extra keystrokes won't cost much in the way of time, and the enhanced clarity will be appreciated by those communicating on a forum with international readership.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Originally posted by Vijay Gade:

You were saying something about the previous post not being written in English. Was that my post?


I think he's talking about Priya Jothi's "spl" post.


[Jess in Action][AskingGoodQuestions]
Arulkumar Gopalan
Ranch Hand

Joined: Oct 13, 2003
Posts: 104
Isn't it simple like this:

Overridding does not make sense in case of constructors, since class name and constructior name should be same. You are asking a question which has a scenario as below.

Isn't it?


Anbudan & Mahalo,<br />Arul<br /> <br />-Not a sun certified Java professional :-)
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
I was just writing some stuff about constructors and created a class that demonstrated something I already knew (but yuou ahve to check). Constructors are not inherited. Thus I was correct and Barry was not. Barry was wrong, Barry was wrong, Barry was wrong...

(does this make me childish?)...

Oh, yes and I agree, please do not use contractions such as wanna,gonna etc, clarity is very important. Many people in these forums have English has a second language and such contractions can confuse. This is not about the ability to speak/write perfect English, we do not expect that at all, just give it your best shot to communicate as clearly as possible and not introduce any ambiguity. And Prya, you contributions are greatly appreciated, please don't take this as anyone "wagging their finger" at you


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Vijay Gade:
Hello Barry,

You were saying something about the previous post not being written in English. Was that my post? Pardon my ignorance but I am just curious. Maybe it is not at all related to my post

Thanks,

-Vijay


No, it was not your post. Nor was it the original post. Not much choice left is there?


What I am reacting to is the (increasing) usage of "text speak". I just turn away from it now. Spell the words out as in an English dictionary.

This I would read:

Hi,

It's somehow related to inheritance. First of all you think when will (?) you use final modifier in class / method / variables (instance / local).

class - when class should not be subclassed

Method - when method should not be overridden

variable - when its value should not be modified

Now coming to constructors we can say it is a special kind of method in a class.

No way constructors(of any class) are going to be inherited by its subclass irrespective of its access modifier. So you can't override something which can't be inherited.So why do you want use final modifier with constructors?.

Something which doesn't give meaning can't / shouldn't be used..right?!!

That must be the reason Sun would have prevented using final modifier with constructors.

Hope you got my point!!!

Regards,
Priya.

[ July 30, 2005: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Yes, I am wrong according to JLS. My fuzzy way of thinking led me to state that the constructor had to be inherited for the subclass constructor to be able to use super() (explicitly or implicitly). I stand corrected. Thanks. However, in my second point I wrote that a constructor was not a method and excluded the possibility of overriding (and implicitly hiding) it.

Constructors are not inherited.
Constructors are not inherited.
Constructors are not inherited.
Constructors are not inherited.
Constructors are not inherited.
.
.
.
Constructors are not inherited.

When I get my paper version of JLS 3.0 I will read that section aloud twenty times while having my back lashed with a wet kipper (or is it mackerel).
Arulkumar Gopalan
Ranch Hand

Joined: Oct 13, 2003
Posts: 104

Marcus:
please do not use contractions such as wanna,gonna etc, clarity is very important. Many people in these forums have English has a second language and such contractions can confuse.


I don't think the usage is because of English is a second language. These words are frequently used by the people who have English as the first language. so, others do follow that.


Barry:
What I am reacting to is the (increasing) usage of "text speak". I just turn away from it now. Spell the words out as in an English dictionary.


I understand/agree that it is recommended to use proper English in technical forums. But, I don't think Priya's communication was bad in such a way none could understand.

Perhaps you could have communicated directly to use proper text English instead of saying "it is not written in English".
(As Steve did.)
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Originally posted by Lokanadham Nalla:
why can't constructors be final as we can not [override] constructors.


If you could declare a constructor final and did not, what would that imply?

Given the choice among requiring it, prohibiting it or leaving it as optional they chose to prohibit it. Constructors are special so having special requirements is not an issue.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Arulkumar Gopalan:


I understand/agree that it is recommended to use proper English in technical forums. But, I don't think Priya's communication was bad in such a way none could understand.

Perhaps you could have communicated directly to use proper text English instead of saying "it is not written in English".
(As Steve did.)


Thanks for posting your view.

I think that the point that Marcus was making, was that many readers have English as their second language. If "text-speak" makes it harder for a english reader, then what does it do for a reader with English as a second language? "Text-speak" unfortunately makes the poster seem somewhat illiterate even if he or she isn't. It's also a matter of respect for your audience. A reduction in line noise is for everyone's benefit.

I agree with and accept your point about communicating more directly, but I had intended that my comment should have such an impact.
Cheers
-Barry
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
So, are abbreviations like "IMHO", "AFAIK", "BTW", "ASAP"... etc acceptable here?
Vincent Brabant
Ranch Hand

Joined: Nov 01, 2004
Posts: 33
Originally posted by Joyce Lee:
So, are abbreviations like "IMHO", "AFAIK", "BTW", "ASAP"... etc acceptable here?


Personally, I am a french speaker.
I know
IMHO means In My Humble Opinion
BTW means By The Way
ASAP means As Soon As Possible

But I have no idea of the meaning of AFAIK.

And I think that ASAP is not appropriate in a forum. Everything is urgent for everyone. So require a response ASAP is the same, IMHO , than posting a question.


Vincent Brabant<br /><a href="http://fr.netbeans.org" target="_blank" rel="nofollow">http://fr.netbeans.org</a>
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Vincent,

But I have no idea of the meaning of AFAIK.
AFAIK is an acronym for As Far As I Know. Honestly speaking, I'd trouble understanding some of the acroynms initially and they're used quite commonly here without much complaints from others. So perhaps it's better to spell out the whole word for clarity.

And I think that ASAP is not appropriate in a forum. Everything is urgent for everyone.

Actually, I simply used ASAP as an example for abbreviation. The phrase as soon as possible can be used in many contexts.

Joyce
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Joyce Lee:
So, are abbreviations like "IMHO", "AFAIK", "BTW", "ASAP"... etc acceptable here?


Good point Joyce! These are Usenet abbreviations. They have been around for many years now. Probably originally used when 600baud single duplex lines were in fashion. I found AFAIK was listed four times when using Dictionary.com from the Firefox search bar. They come from bulletin board/news culture and, in my humble opinion, are in a different league to "4 u", "thanx", "bcos" which come from chat (where you need to get information over in a few seconds) or mobile phone messaging (where you are limited to around 160 characters in a message).

So to your question I would answer: "Acceptable but discouraged".
Wensheng Li
Greenhorn

Joined: Jul 08, 2005
Posts: 16
Originally posted by Lokanadham Nalla:
Hi there,

why can't constructors be final as we can not inherite constructors.

Loka


Just consider constructor as a special method:
The only access modifer allowed is public,private and protected.

Any other modifers,i.e., void or any return tppe will just make the constructor 'ordinary'

or just illegal, like static, final,synchronized

Remember this rule is enough for the exam!


SCWCD 1.4(In progress)<br />SCJP 5.0<br />IBM Certified Solutions Expert for Webshpere Studio, V3.5 (retired)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why cannot constructors be final
 
Similar Threads
[static constructor]....
Why can't consructors be marked as final or abstract ?
doubt in constructor
doubt on final, native, abstract keywords
constructors