File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding access visibility rules Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding access visibility rules" Watch "Overriding access visibility rules" New topic
Author

Overriding access visibility rules

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 479
In a super class given method
public void aMethod(){}

In a subclass giving the only visibility is
public void aMethid(){}

Like that,
In a Super class,a method with protected---subclass,a method with protected/public is valid.giving private or default is error

In a Super class,a method with private--no overriding in subclass

In a Super class,a method with default-subclass,a method with protected/public/default is valid. giving private is error

I checked every of the above with my compiler.still I need to confirm from anyone because consider the following question.


Given the code below,and making no other changes,which access modifiers ar placed before myMethod() on line 3

class Super{
void myMethod(){//line 3

}
}

class Q extends Super{
void myMethod(){//line 7

}
}

Given options:
1.public
2.protected
3.private
4.none of the above

Given 1,2,3 answers are correct.since private feature is accessible only to its own class,selecting private is also valid.

But my answer is only giving private on line 3 would work.of course ,line 7 is not overridint the super class method.that is a separate method.How come giving public/protected on line 3 is valid for the above program.please help me and correct me if my beginning rules are not correct.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You are allowed to increase the visibility of overridden methods, but you can't decrease the visibility.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 479
Thanks for the reply keith. is the three answers correct pr wrong?.....please give more details depends on the above program
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The given answer 1,2,3 is incorrect; only line 3 is correct, for the reason you gave. Wherever you got this question from, it's not a trustworthy source; ignore it.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding access visibility rules