my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes does this code is method hiding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "does this code is method hiding" Watch "does this code is method hiding" New topic

does this code is method hiding

Abdul Aatif

Joined: Feb 08, 2012
Posts: 23
public class Test{
private void m1(){

class Test2 extends Test{

private void m1(){


Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

Yep,It is.

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924

no it is not. the subclass does not know m1 exists in the super class. in superclass m1 is declared as private. test2 does not know the existence of m1.

if you have an accessible static method in superclass and you have method with same name and args in subclass, that would have been called method hiding

public class Test{
public static void m1(){

class Test2 extends Test{

public static void m1(){ //method hiding. not overriding. static methods cannot be overidden.

Kevin Florish
Ranch Hand

Joined: Jan 06, 2009
Posts: 179
Just to elaborate on the answer given by Gurpeet anything that is declared private is only accessible within the class it is declared in.

private - only accessible within its own class.

no modifier / package-private /(the default) - accessible only within its own package.

protected - accessible within its own package and also by a subclass of its class in other packages.

public - accessible to all other classes everywhere.

Java5 Tutorials | Java6 Tutorials | HTML/CSS Tutorials | JavaScript and jQuery Tutorials
I agree. Here's the link:
subject: does this code is method hiding
jQuery in Action, 3rd edition