File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Error on K&B exercise in Chap 10... 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 "Error on K&B exercise in Chap 10..." Watch "Error on K&B exercise in Chap 10..." New topic
Author

Error on K&B exercise in Chap 10...

Emanuele Ghe
Ranch Hand

Joined: Feb 04, 2009
Posts: 111
Hi,
this is the text of the exercise:



In my opinion the only correct answer is C because:
D isn't correct, line 7 is perfectly legal since variable "b" is protected and class "Fiz" extends "Foo".
E is correct, in fact line 8 has the same issues of line 6, but the compiler will not show "error on line 8", but just the one on line 6.

Am I right ?


SCJP6 with score 90%. I am conscious of my ignorance and ready to learn from everyone.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Before you make predictions which options are right and wrong, please try to run the program yourself and see if you are right...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Afzal Rehman
Ranch Hand

Joined: Dec 16, 2009
Posts: 39
Hi , Answer is C , D , E Because Just look you create an object from Foo, and variable a has
default acess which means you can't access from different package. so answer is C, and another which i think so you
are confused in Answer D, protected variable you can't see in Fiz class (protected will be act same like default because superclass object is being created here in this case) ,Answer E is simple a has Default access.

I think it will help you to understand ....


Many Thanks

Afzal


SCJP 6.0
Emanuele Ghe
Ranch Hand

Joined: Feb 04, 2009
Posts: 111
afzal rehman wrote:Hi , Answer is C , D , E Because Just look you create an object from Foo, and variable a has
default acess which means you can't access from different package. so answer is C, and another which i think so you
are confused in Answer D, protected variable you can't see in Fiz class (protected will be act same like default because superclass object is being created here in this case) ,Answer E is simple a has Default access.

I think it will help you to understand ....


Many Thanks

Afzal


Thank you, I've also ran the code and now I understood.

Also I've noticed that the compiler shows error on all the three lines, but I've noticed situations in which the compiler stops at the first error.

How can I distinguish these situations ?
Afzal Rehman
Ranch Hand

Joined: Dec 16, 2009
Posts: 39
There are four access levels
1- private
2- public
3- defaults
4- protected
but you need to see the situation in code what's going on.
Let see if you will see
1- private :- then it's simple it's not directly accessible outside of class. so keep in mind private is
private even they have inhertied subclasses but private will be accessible just inside of class.

2- public :- is accessible for everywhere means.

3- default := is accessible only on same package . you can't see the default outside of package. default is accessible in more then one class in same package.

4- protected :- protected act same like default access in a same file . but if you do inheritance then it will be accessible in different package. Let see this Example it would might clear more to you protected.
package proAcc;
public class A{
public static void main(String[] args){
B b = new B();
System.out.print(b.i);

}
}

class B{
protected int i=10;
}
but it will produce 10 even it is protected. but it's in same package
................ i hope this will help you out
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error on K&B exercise in Chap 10...