• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Protected methods

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This is a question form a Mock exam:

True or False: If class Y extends class X, the 2 classes are in different packages; and class X has a protected method called abby() then any instance of Y may call the abby() method on any instance of Y?

I think the answer is True, but according to the test simulator it is False.

What do you think?

Thanks
Katrin
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Outside a package if you want to access protected method of a public class, then it can only be done through inheritance.

you can't access protected method of public class through instance out side its package.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what the book says "A default member may be accessed only if the class accessing the member belongs to the same package, whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package"

Classes A and B are in different packages, aPackage and bPackage, respectively. Class B imports Class A. If class B extends class A, and class A has a private method x, you can access that method x only through an instance of class B, not Class A.

B b = new B();
b.x(); //yes, this will compile and run
A a = new A();
a.x(); //no this will not compile with an error message "x() has protected access in aPackage.A"


I thought otherwise until I tried it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic