aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Explicit default constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Explicit default constructor" Watch "Explicit default constructor" New topic
Author

Explicit default constructor

sabeer
Ranch Hand

Joined: Mar 15, 2005
Posts: 41
Dear ranches,
Am preparing for SCJP, about to sit for the exam in 2 weeks. I have doubt on following code. Please explain me.

If class has private default explicit constructor then it cannot be extended, am I wright?

e.g.,
class a {
private a() { } // explicit default constructor
}

class b extends a{}

Why compile error on class b?

Thks!
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Right, it can't be extended because a subclass must be able to call a constructor of it's superclass.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If all the constructors for a class are private, then it can't be extended. (Unless we're talking about nested classes.) But as long as at least one constructor is accessible, you can extend. You may have to explicitly invoke a super() constructor to access the base class constructor you want. E.g.:

[ March 15, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Your terminology is a little bit of an oxymoron.
A default constructor is intrinsically implicit.
An "explicit default constructor" makes no sense.
You might mean "explicit no args" constructor.


Tony Morris
Java Q&A (FAQ, Trivia)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
True, "default" has been overloaded to mean too many different things. Even just in the context of constructors - there's the constructor which is provided by default, and there's the super constructor which is invoked by default. Not quite the same thing. In the original code, "private a() {}" is an explicit declaration of the no-arg constructor for class a, which is invoked by default from class b's constructors (unless you provide an alternate super() invocation). So it's explicit in one sense, and a default in another. That's way too confusing for general usage though - better to never, ever use the term "default constructor" since it's not really clear in which sense it's a "default".
sabeer
Ranch Hand

Joined: Mar 15, 2005
Posts: 41
It is no difference between the class declared as final and the class has private constructors in context-of extending these classes.

e.g (1)
final class a{} //final class
class b extends a {} // compiler error
e.g (2)
pubic class c{
private c(){} //private constructor
}
class d extends c{} // compile error

So, in this case both (1) and (2) are same.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[sabeer]: It is no difference between the class declared as final and the class has private constructors in context-of extending these classes.

They do have the same net effect (unless we consider nested classes). But they do give you different error messages.
[ March 15, 2005: Message edited by: Jim Yingst ]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
A "default constructor" is well defined as one that is implicit.
If the enclosing class has public access scope, the default constructor has public access scope, for all other access scopes, the default constructor has package access scope.

JLS 2e 8.8.7

If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Ah, OK - you're right. Didn't realize that. The term gets misused by so many people, I just learned to avoid it entirely. Thanks.
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
hi all,

if a class dosen;t have any access specifier, what is the default access specifier for it.....??
and as said in the thread above, what is a package access scope???

i've one more doubt....
they say that a class can only be extended if the extending class is in the same package or if we import the class to be extended..am i right???

then if we write two classes in same file(one class extending the other) and define one variable with default access in the base class, can this be accessed by the derived class???

i more doubt...
is it true that default cnstrtr takes the acess specification from its enclosing class in every case???

Sorry if too irritating but plz answer as i need to clarify these doubts..
thanx
amit
valli Suresh
Greenhorn

Joined: Mar 15, 2005
Posts: 5
e.g (1)
final class a{} //final class
class b extends a {} // compiler error
e.g (2)
pubic class c{
private c(){} //private constructor
}
class d extends c{} // compile error

So, in this case both (1) and (2) are same.

------------------------------------------------------------------------------

llly ,

public class a {
public a ( int i) { };
}

public class b extends a {} // compile error

as the default constructor created for class b doen't have a no-arg const. in its super class . This can be solved by

public class a {
public a (int i) {};
}

public class b extends a {

public b {
super (i) };
Agandhi
Greenhorn

Joined: Oct 17, 2003
Posts: 6
hi all,

if a class dosen;t have any access specifier, what is the default access specifier for it.....??
and as said in the thread above, what is a package access scope???

If a class doesn't have any access modifier attached to it then it will have DEFAULT access modifier. The package scope is nothing but you will have access to the constuctors and methods of other classes if accessing class is in a same package.

i've one more doubt....
they say that a class can only be extended if the extending class is in the same package or if we import the class to be extended..am i right???

You are right if the extending class has a default access modifier to it. But say the extending class has a public access modifier then you can extend that class even if it's in a different package.

then if we write two classes in same file(one class extending the other) and define one variable with default access in the base class, can this be accessed by the derived class???

Yes you can access the variables defined in the parent class. Once a child class extends the parent class, chld class can do everything which a parent class can do, but it's not true the other way round.

i more doubt...
is it true that default cnstrtr takes the acess specification from its enclosing class in every case???

A default constructor always has a default access in a class and it doesn't inherit anything by extending a class. You can changes the access modifier of a constructor then it;s not a deault constructor anymore.

Thanks ..
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
thanx Agandhi.... :-)
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
No access modifier implies default access.
Some other synonymous terminology is "package access", "package protected access", "package private access".
They are all the same thing.
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
here u come Tony .....actually this is what i ws looking for. as its really confusing when you read sth like privated protested together....

thanx
 
jQuery in Action, 2nd edition
 
subject: Explicit default constructor