• 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 and Access Modifiers

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All
From various books it is clear that Constructors cannot have access modifiers (public,private, protected).
Yet, I was studying the Complete Java 2 Certification book which gave the following code :
1 class Base {
2 public Base (String s) {
3 //initialise Object using i
4 }
5 public Base (int i ) {
6 //initialise this object using i
7 }
8 }
9
10 class Derived extends Base {
11 public Derived (String s) {
12 //pass control to Base constructor at line 2
13 super(s);
14 }
15 public Derived (int i) {
16 //pass control to base constructor at line 5
17 super (i);
18 }
19 }
Can someone please explain to me how the constructors at lines 2 and 5 can have the "public" keywords attached to them ???
Thanks in Advance.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Constructors can have access modifiers.
They cannot have abstract, static, final, synchronized, native modifers.
 
Zahid, Butt
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
What would be the purpose of giving access modifiers to Constructors ???
They only Initialise Objects.
Please can you explain.
Thanks, Zahid.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The purpose would be the same as for giving access modifiers to other member variables and methods.
If you dont want an object of your class to be created anywhere but in the class itself,you can have a private constructor for your class.
Similarly we can think of scenarios for public and protected constructors.
------------------
Udayan Naik
Sun Certified Java 2 Programmer
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A quick example is the Math class, which is public final.
The constructor is private so that the class can't be instantiated outside the class.
 
Zahid, Butt
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Does anyone know of a possible mock exam question related to constructors and access modifiers ???
Any sample questions would be appreciated.
You can email me them at Zahid_Butt321@hotmail.com
Thanks in advance.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Zahid,
Try Bill Brogden's mock exam
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Zahid, Butt
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Has anyone else got any code samples ???
Thanks in advance.
reply
    Bookmark Topic Watch Topic
  • New Topic