aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes mock 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 "mock" Watch "mock" New topic
Author

mock

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi!
Consider the contents of following two files:
//File A.java
package a;
public class A
{
A(){ }
public void print(){ System.out.println("A"); }
}
//File B.java
package b;
import a.*;
public class B extends A
{
B(){ }
public void print(){ System.out.println("B"); }
public static void main(String[] args)
{
new B();
}
}
I don�t understand why no compile this code.
Thank you in advance.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
When you create en instance of B, the defualt constructor of class B calls the default constructor of class A but the constructor A() is not accessible outside the package a.
Val


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
And if I change A(){ } for public A(){}??
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
It should work !
try it !!
Val
PS: it seems to me that you are not even trying the code you post, you should do so !
[This message has been edited by Valentin Crettaz (edited September 20, 2001).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thank you Valentin
Brian Burridge
Greenhorn

Joined: Aug 22, 2001
Posts: 6
Hmm. I just compiled that code just fine. I was under the impression that if you have no modifier on a constructor, that it defaults to the modifier of the class, in this case public.
As I said, it compiled for me.
Brian N. Burridge
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Brian, it should not work, the compile error would be "No constructor matching A() in class a.A"
You have to put a public (or protected) modifier for the constructor of A !
Are your files A.java and B.java in two separate packages (i.e. directories) ???
It's really weird that it does compile !!
Val
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
It can also compile puting it in one package without access modifier on the second class. That means if the subclass and the superclass are in the same package and with only the superclass declared public.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
It was not the purpose of this question !!!
This question wanted to demonstrate that a subclass in a different package can't be instantiated unless the constructor of the superclass is protected or public !
You can put everything in one file and it does magically compile !!
Val
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Valentin ,
Thanks for the correction. Like I did not uderstand the question very well at the first. I compiled it again with the public and also with the protecked modifier in a constructor of A class and it does work. Like I said, thanks a lot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: mock