Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

doubt

 
Ranch Hand
Posts: 44
  • 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.
 
Greenhorn
Posts: 24
  • 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
  • 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?
 
Ranch Hand
Posts: 377
  • 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.
 
Ranch Hand
Posts: 47
  • 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
  • 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.
 
Greenhorn
Posts: 19
  • 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
  • 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
  • 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
 
Ranch Hand
Posts: 75
  • 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
 
    Bookmark Topic Watch Topic
  • New Topic