This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Name Hiding

 
farrukh mahmud
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

class Homer
{
char doh(char c)
{
System.out.println("doh(char)");
return 'd';
}

float doh(float f)
{
System.out.println("doh(float)");
return 1.0f;
}

}
class Milhouse {}
class Bart extends Homer
{
void doh(Milhouse m)
{}

}
class Hide
{
public static void main (String args[])
{
Bart b=new Bart();
b.doh(1);
b.doh('x');
b.doh(1.0f);
b.doh(new Milhouse());

}
}
if a Java base class has a method name thats overloaded several times, redefining that method name in the derived class will not hide any of the base class versions?
can any one explain me with the above example.




------------------
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When methods are defined with the same name, but different signature, they are just name overloading, nothing can hide anything else. It is irrelevant it is in a derived class or not.
1) American people drive on the right side.
2) You are right on this topic.
3) He will be right back.
English word right is overloaded here, we understand the difference by the context. Compiler understands the method overloading by the signature, which serves the same purpose as context, but more reliable, since compiler is not as intelligent as you are.
Thanks!
Roseanne
Join our SCJD Study Group when certified
[This message has been edited by Roseanne Zhang (edited March 02, 2001).]
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If derived class defines a non-private method with the same name and same signature as the non-private method in base class, then it is method overriding, polymorphism comes into play. Late binding/Runtime binding will happen.
Never mix these two words overriding and overloading, please.
[This message has been edited by Roseanne Zhang (edited March 02, 2001).]
 
farrukh mahmud
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i know the difference between overloading and over riding but what is name hiding in java. the example which i posted a day before in which I wana know about (how we do) name hiding. In bruce eckel book (Thinking in java) he write this example under the heading of name hiding (in chapter 6. page no is 292 i think so). Well if this example is not relevant to name hiding then please tell me how we can implement name hiding in java. i shall be very thankful to you
------------------
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiding or shadowing occurs when the super class has a method marked private. The sub class can not see or use or override the private methods of the super class. However the sub class CAN create a method with the exact same signature as the supers private class and it can use this class. This is not a case of polymorphism (overriding), so we use the term hiding the parent class.
If we were actually overriding the super's class we would still be able to get at the super version of the class by explicitly calling super.method(). Not so in this case. If you try super.method() on a private class you get a compile error.
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cindy
I think your explanation is incorrect. If super class defined a private method, it hides itself, nobody else can see it or use it. That is exactly what private means. It does not need a subclass to define a same name & signature method to be hided!!!
If the subclass use the same name and signature to define another method, it does not hide anything, it is just itself and totally irrelevant with the super class same name method. In addition, it should be only considered a bad coding style, even it is legal.
Thanks!
Roseanne
[This message has been edited by Roseanne Zhang (edited March 03, 2001).]
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read everything about hiding here!
Copied from JLS2:
The body of a class declares members (fields and methods and nested classes and interfaces), instance and static initializers, and constructors (�8.1.5). The scope (�6.3) of a member (�8.2) is the entire declaration of the class to which the member belongs. Field, method, member class, member interface, and constructor declarations may include the access modifiers (�6.6) public, protected, or private. The members of a class include both declared and inherited members (�8.2). Newly declared fields can hide fields declared in a superclass or superinterface. Newly declared class members and interface members can hide class or interface members declared in a superclass or superinterface. Newly declared methods can hide, implement, or override methods declared in a superclass or superinterface.

If the class declares a field with a certain name, then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in superclasses, and superinterfaces of the class. The field declaration also shadows (�6.3.1) declarations of any accessible fields in enclosing classes or interfaces, and any local variables, formal method parameters, and exception handler parameters with the same name in any enclosing blocks.
If a field declaration hides the declaration of another field, the two fields need not have the same type.

.......
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#250255
[This message has been edited by Roseanne Zhang (edited March 03, 2001).]
 
niraj singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Method name hiding is specifically relevant to static methods in a derived class. If you have a static method in a super class, say -
static public void myMethod(int i, float j){ }
and then you again declare a method with the same method signature in the child class as above, then no overriding takes place, but shadowing (or hiding) occurs. Static methods cannot be overridden( no polymorphic calls take place), but they are hidden. So, to call the static method of the super class, you have to specifically call the method with classname, -
superclass.myMethod();
Niraj
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since then I wrote a "whimsicle" article in the JavaRanch Newsletter that covers this topic called The Games Objects Play
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic