The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2000



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


RE:making the method public

Posted by Kishori Sharan on October 04, 2000 at 9:51 AM

A simple example may clear your doubt.

interface II {

public void iMethod ( ) ;

class CC implements II {
private void iMethod ( ) {
} ;

class Test {

public static void main(String[] args) throws Exception {
II obj = new CC ( ) ;
obj.iMethod ( ) ;


If you were allowed to make your iMethod ( ) in class CC private event then obj.iMethod ( ) would have compiled in class Test because obj is an interface of type II and in II iMethod() is public . Since obj can have a reference of any object which implements the interface II the call to iMethod() would be invalid at run time if you restrict the access to iMethod down the hierarchy. It is so because you are allowed to do upcasting. Because you can put the object of child class in the variable which is reference to a parent class.


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us