• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Emanuele Ghe
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before you make predictions which options are right and wrong, please try to run the program yourself and see if you are right...
 
Afzal Rehman
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Emanuele Ghe
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic