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

import statement

dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
// Filename; SuperclassX.java
package packageX;
public class SuperclassX
{
protected void superclassMethodX()
{
}
int superclassVarX;
}

// Filename SubclassY.java
1. package packageX.packageY;
2.
3. public class SubclassY extends SuperclassX
4. {
5. SuperclassX objX = new SubclassY();
6. SubclassY objY = new SubclassY();
7. void subclassMethodY()
8. {
9. objY.superclassMethodX();
10. int i;
11. i = objY.superclassVarX;
12. }
13. }
OPTION A:
Compilation error at line 5
OPTION B:
Compilation error at line 9
OPTION C:
Runtime exception at line 11
OPTION D:
None of these


In the above code in SubclassX file you need to import SuperclassX(at line 2). Am I correct? Please help me.


SCJP-1.5<br />SCWCD-1.4
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

Yes, you need to import SuperclassX, but you would get the error already in line 3. So answer D.
[ September 20, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Also at line 11. superclassVarX is not public in SuperclassX so cannot be accessed from out side package.
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Thanks Jesper,
I just want to confirm that you need import statement or not. Because the description does not say anything about import statement.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

The question doesn't say anything about an import statement because it would be giving away the anwer...
Rahul Nair
Ranch Hand

Joined: Dec 01, 2007
Posts: 138
Originally posted by dolly shah:
// Filename; SuperclassX.java
package packageX;
public class SuperclassX
{
protected void superclassMethodX()
{
}
int superclassVarX;
}

// Filename SubclassY.java
1. package packageX.packageY;
2.
3. public class SubclassY extends SuperclassX
4. {
5. SuperclassX objX = new SubclassY();
6. SubclassY objY = new SubclassY();
7. void subclassMethodY()
8. {
9. objY.superclassMethodX();
10. int i;
11. i = objY.superclassVarX;
12. }
13. }
OPTION A:
Compilation error at line 5
OPTION B:
Compilation error at line 9
OPTION C:
Runtime exception at line 11
OPTION D:
None of these


In the above code in SubclassX file you need to import SuperclassX(at line 2). Am I correct? Please help me.



I have one issue in above example..
line number 10 there is a local variable decleared.. But the RULE says Local Var must be initialized before use otherwise compiler complains...


So in this case why this is not happen(compiler complains about var initialization) ???
Sid Robin
Ranch Hand

Joined: Nov 24, 2007
Posts: 53
Hello Rahul ,

Your statement itself has answer . Compiler complains if you are using with out initialization but you are not using the variable anywhere in the code . Am i correct ?

Regards
siddu
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
SuperclassX.superclassVarX has a default access and hence it will be visible to all the classes within the same package and not to anyother classes outside the package including subpackages.
The statements
int i;
i = objY.superclassVarX;
Will cause errror because superclassVarX is not visible.
Mark the variabl superclassVarX public or protected and it will compile.
Futher superclassVarX being a memeber variable it will be assigned a default value of 0 because its of type int even if its not assigned any explict value and hence i will have a value 0.


And yes import statement is required to import SuperclassX which is defined in packageX or if you dont want import statements refer SuperclassX in SubclassY with its full qualified name as packageX.SuperclassX. as shown below



Thanks
Deepak


SCJP, SCWCD, SCBCD
Rahul Nair
Ranch Hand

Joined: Dec 01, 2007
Posts: 138
Originally posted by Siddu kuruba:
Hello Rahul ,

Your statement itself has answer . Compiler complains if you are using with out initialization but you are not using the variable anywhere in the code . Am i correct ?

Regards
siddu



Thanks for reply ...siddu

Ok.. I got it ...

we are initialize it in next line using the value of superclassVarX
but the variable is not accessed...

thats why it does't show above compiler error. .

Am i correct ??

Thanks siddu.

Rahul..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: import statement