• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

ExamLab 2nd exam 58th

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

This question is 58 from ExamLab's 2nd exam.

clasA.java
--------------

package pkga;
public class clasA{
protected void doThis(){}
}



===================================


clasB.java
----------------

package pkgb;
import pkga.clasA;
public class clasB extends clasA{
public static void main(String args[]){
/* Insert Here */
ca.doThis();
}
}

Correct answers mentioned are ClasB ca = new ClasB(); and ClasB ca = new ClasB(){protected void doThis(){}};
I didnt get the point that how to call a protected method using its instance in a different package? Second answer looks correct but not the first one. Can any one explain please?

 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch, Kaladhar.

The only restriction is that you can't call a protected method through a reference of the superclass from outside the superclass package. The way the JLS puts it is that you can only call protected methods outside the package from within the class which is responsible for the implementation. The thing that might be confusing you in this case is that classB doesn't override the doThis() method. However, it's up to classB's implementor to do so (by not overriding it, classB is deciding to use the same implementation as the superclass.) The point is that you are calling the method on a reference of classB, from classB.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kaladhar, please Use Code Tags the next time you post any source code.

Now just to elaborate on a point that Ruben mentioned, if you try to access the protected member via inheritance, then you can access it otherwise not. So if you try to do this



 
Kaladhar Ak
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ruben and Ankit,

Thanks for reply. But in the question, doThis() is referenced outside the superclass package with superclass reference (from to one of the correct answers). Isn't it wrong?

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

Kaladhar Ak wrote:Hi Ruben and Ankit,

Thanks for reply. But in the question, doThis() is referenced outside the superclass package with superclass reference (from to one of the correct answers). Isn't it wrong?

Regards
Kaladhar



You can't call it with a ClasA reference. You must have a ClasB reference. As you said, correct answers are ClasB ca = new ClasB(); and ClasB ca = new ClasB(){protected void doThis(){}};
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:



Hay Ankit, class A must be public here.
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Treimin Clark wrote:

Ankit Garg wrote:



Hay Ankit, class A must be public here.



Yes, you got me
 
Kaladhar Ak
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ruben,Ankit

Thanks for replies. It is clear now.

 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Kaladhar Ak " please check your private messages for an important administrative matter. You can check them by clicking the My Private Messages link above.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic