This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

import statement

 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also at line 11. superclassVarX is not public in SuperclassX so cannot be accessed from out side package.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question doesn't say anything about an import statement because it would be giving away the anwer...
 
Rahul Nair
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rahul Nair
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic