• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

access modifier

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package pkg0;
public class father
{
protected byte f=10;
}
package pkg1;
import pkg0.Father;
class son extends Father
{
public void sayitOut()
{
System.out.println("f is " + f);
Father f=new Father();
System.out.println("while F is" +F.f);
}
public static void main(String[] args)
{
new son().sayitOut();
}
}

Answer: Compile error. pkg1.Son.java : line 08 - Cannot access protected member f in pkg0.Father.

But my expected answer: Compile error. pkg1.Son.java : line 06 - Cannot resolve symbol f.
I think we can access protected in the subclass which is in different package,then why that came as the correct one.
please explain me...

i found this source in http://www.irixtech.com/dynamic-quiz/java/scjp/87/begin

thanks in advance
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Preetha



The line 06 (print(f)) won't give a compilation error. that is because, the data membre, f, is declared as protected in Father class. So it will become as a private mamber in the subclass, Son. The member method, sayitOut() can directly access that one. so it wont give any error.

About line08(print(F.f)), the method is trying to access the protected data member of class Father, from a separate class, Son. This will results the compilation error.

Hope this will clear for you.
 
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The protected data member can be accessed outside the packages only by inheritance, not thru instantiation(ie using object reference).

In line 6, the protected memeber is accessed using inheritance and in line 8, it is trying to access thru a Father instance which is not allowed.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree with Lakshman,

you can also retrive by using create subclass Object.
 
vipin jain
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
means
Son s=new Son();
System.out.println("while F is" +s.f);
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Parent class reference cannot be used in child class to access protected members if child class is in the some other package.

Line 6 wont give any error because, protected members are inherited across packages.
 
Honk if you love justice! And honk twice for tiny ads!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic