Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt

 
Fedry Kemilau
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


this code should not compile, right?
Because I think import statement must come after package statement, and cannot be in the middle of the codes. (Correct me if I were wrong)
But the answer given is producing output 8.
 
Dave Walsh
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried it ? It doesn't compile: "Imports (if any) must come after package and before class definition".

Who told you the output is 8 ?
 
Fedry Kemilau
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was doing John Meyers' Mock Exam. It does not compile when I tried.
I just want to be sure that the reason i gave is correct, isn't it?
 
Manfred Klug
Ranch Hand
Posts: 377
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

if you have such code, it is often the case that you have to put the classes in different files. If you do it right (may the package statements be with you), the code will compile and produce the output 8.
 
christian combarel
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I didn't try to compile this code but I would say it will fail because x2 is protected and you can't have access to it through a reference but only through inheritance. (I suppose the 2 classes exist in two differents files)
 
Manfred Klug
Ranch Hand
Posts: 377
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by christian combarel:
I didn't try to compile this code
Then you should do it. It makes the discussion much easier.
 
sumi selva
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I compiled the below code and didnt get any compile error.It runs fine.
But as per K&B, it should produce a compile error.

Code
------
package A;
public class Father{
static public int x1 = 7;
static protected int x2 = 8;
static int x3=9;
static private int x4 =10;
}

package B;
import A.Father;

public class Son extends Father{

public static void main(String [] arg){
Father f = new Father();
System.out.println( f.x2 );
}
}

May I know why this compiles fine?


Thanks
Sumi
 
sumi selva
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I got the answer.
If the variable is a static protected one it can be accessed through dor operator in another package.
If it is a protected one without static modifier, it can be accessed only thr inheritance in another package.

Thanks,
Sumi
 
sumi selva
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
One more thing to add..

Given in K&B - Chap 2
"when a subclass-outside-the-package inherits
a protected member, the member is essentially private inside the subclass, such
that only the subclass and its subclasses(both in the same and different package)can access it."

Thanks,
Sumi
 
Alexsandra Carvalho
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello sumi,

In respect "If the variable is a static protected one it can be accessed through dor operator in another package."

I have tested and really is that. But...it is new for me. Where did you read about this? It was in K&B? What chapter/page?

Thanks,
Alexsandra
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic