aspose file tools*
The moose likes Java in General and the fly likes Name Hiding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Name Hiding" Watch "Name Hiding" New topic
Author

Name Hiding

farrukh mahmud
Ranch Hand

Joined: Feb 13, 2001
Posts: 47

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.




------------------

Regards<BR>Farrukh Mahmud
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
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

Joined: Nov 14, 2000
Posts: 1953
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

Joined: Feb 13, 2001
Posts: 47
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

Joined: Sep 29, 2000
Posts: 8521
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.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
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

Joined: Nov 14, 2000
Posts: 1953
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

Joined: Feb 07, 2001
Posts: 36
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

Joined: Sep 29, 2000
Posts: 8521
Since then I wrote a "whimsicle" article in the JavaRanch Newsletter that covers this topic called The Games Objects Play
 
Don't get me started about those stupid light bulbs.
 
subject: Name Hiding