File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why I cannot extend a class from java.lang.Package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why I cannot extend a class from java.lang.Package" Watch "Why I cannot extend a class from java.lang.Package" New topic
Author

Why I cannot extend a class from java.lang.Package

adiratha abhivachan
Greenhorn

Joined: Mar 15, 2005
Posts: 8


why the above code gives the below compilation error?
E:\java\Testaac.java:1: cannot resolve symbol
symbol : constructor Package ()
location: class java.lang.Package
class Testaac extends Package{
^
1 error

Tool completed with exit code 1

Thanks in advance
Veer Batra
Ranch Hand

Joined: Mar 12, 2001
Posts: 35
Which Package class you want to extend? There is no class named Package. If you change your code as under you see it compiles, because then it is able to find Package class.

adiratha abhivachan
Greenhorn

Joined: Mar 15, 2005
Posts: 8
Thanks for your reply.

You can find in the API documentation. Anyhow I am providing the partial content here

java.lang
Class Package

java.lang.Object
java.lang.Package
public class Package
extends Object

Package objects contain version information about the implementation and specification of a Java package. This versioning information is retrieved and made available by the ClassLoader instance that loaded the class(es). Typically, it is stored in the manifest that is distributed with the classes.

The set of classes that make up the package may implement a particular specification and if so the specification title, version number, and vendor strings identify that specification. An application can ask if the package is compatible with a particular version, see the isCompatibleWith method for details.

Specification version numbers use a syntax that consists of positive decimal integers separated by periods ".", for example "2.0" or "1.2.3.4.5.6.7". This allows an extensible number to be used to represent major, minor, micro, etc. versions. The version specification is described by the following formal grammar:
Veer Batra
Ranch Hand

Joined: Mar 12, 2001
Posts: 35
Sorry, I am able to see java.lang.Package class. So forget what I said earlier.
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Your error says it cannot resolve "constructor Package ()" in "class java.lang.Package".

It's looking for the default constructor in class java.lang.Package and it doesn't have one.


E:\java\Testaac.java:1: cannot resolve symbol
symbol : constructor Package ()
location: class java.lang.Package
class Testaac extends Package{
^
1 error
[/QUOTE}]
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
but its not neccesary to define the default constructor .....
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Package appears to not have a public constructor. You cannot subclass a class that does not have a visible constructor. There is always a call to the superclass constructor in your subclass constructor. By default a call to super() is placed in. If the superclass does not have a no-args constructory you must explicitly place in a call to super(whatever args).
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
hi Steven,
plz look into code hereunder....


if i'm not mistaken or misunderstood your point,
you can see that we dont have any visible constructor for any class but it wrks fine.....
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Steven Bell:
Package appears to not have a public constructor. You cannot subclass a class that does not have a visible constructor. There is always a call to the superclass constructor in your subclass constructor. By default a call to super() is placed in. If the superclass does not have a no-args constructory you must explicitly place in a call to super(whatever args).


If any class is not specifying any constructor explicitly then compiler insert a default ( this is no argument ) constructor & its access specifier is same as class access specifier . So I think this Package class has implicit public default constructor ...
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I actually think it has an explicit private constructor and is not meant to be subclassed.

Amit, in your example there is a default constructor A(). That is being called by Compiles default constructor Compiles(). (How else would you be able to do new A() or new Compiles()).

rath is right about the implicite adding of a constructor, but I think the Package class was written with a
private Package()
constructor. Of course somebody could grab the source and check.
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
I still have the source code for java.lang.Package open. (jdk 1.5)

I found two constructors. One private, one default access. Both have args (no no-arg constructor).
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
yes , It has no no argument constructor .
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
There ya go. Correct me if I'm wrong, but you can't subclass that unless you are in the same package. Not sure if it will let you put your class in the java.lang package, never tried, doubt it though.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
If you are not in same package then you can't inherit a class that has default constructor .

you can extend any class has public or protected constructor anywhere only you need to add import statement or you can write fully qualified class name .
[ March 15, 2005: Message edited by: rathi ji ]
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I just tried it, default constructor can only be seen from within the same package. So unless you can put your class in the java.lang package you can't subclass Package. Although that seems to be by design to me.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
yes you are right , default class can only be inherited in same package only .

could any body please explain the difference b/w having public constructor & having protected constructor ... how they are going to affect inheritance ...

Thanks .
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
A public constructor can be called by anybody, a protected constructor can only be called by classes in the same package and subclasses.

If you are writing a class that you expect (or think it lickly) to be subclassed, but you don't want just anybody to be able to create one (or maybe only create it in certain ways) you might make a constructor protected.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Yes I got the answer , they are not different when we talk about inheritance . But one difference is :

suppose your class A has protected constructor , and class B is in difference package , then you can't create an object of class A in class B untill you will inherit it ( even if you have used import statement ) ...

But if A has public constructor , then you can create A's object in B class without inherit it ( ofcourse you have to used import statement or fully qualified class name ) ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why I cannot extend a class from java.lang.Package
 
Similar Threads
Pass by ref- pgm err
problem in Compiling servlets
Another confused question about package
inheritance problem?
How can I use abstract class methods