Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Constructor

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody explain me why constructors cannot be inherited?
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !
Constructors cannot be inherited because they are different from ordinary methods, they are called when the class is instantiated. The sub-class can invoke the methods of the super class and not he contructor.
Thanx
Afroz.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Afroz,
A subclass can invoke the constructor of it's superclass as well as it's methods.
<pre>
class TestConstructor {
public static void main(String args[]){
new Sub();
}

}
class Super {
Super(){
System.out.println("Super Constructor");
}
}
class Sub extends Super{
Sub(){
super();
}
}
</pre>
The output is:
Super Constructor
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a class can access the super class constructors and methods then why we say that this class can not inherit its super class constructors. I know you can not override a Constructor, which means you can not add a new feature to the constructor. But as long as you are able to access it can't we say we are inheriting it.
Can someone put more thoughts in it........
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
if u look into JLS, 8.8 constructor delclarations, u will get an answer for ur question. I have copied the the reqd part here..


Constructors are invoked by class instance creation expressions (�15.9), by the conversions and concatenations caused by the string concatenation operator + (�15.18.1), and by explicit constructor invocations from other constructors (�8.8.5). Constructors are never invoked by method invocation expressions (�15.12).
Access to constructors is governed by access modifiers (�6.6).
This is useful, for example, in preventing instantiation by declaring an inaccessible constructor (�8.8.8). Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding.


thanks,
Baskaran
---------------------------------------------
[This message has been edited by Baskaran Subramani (edited September 06, 2000).]
 
Wink, wink, nudge, nudge, say no more, it's a tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic