File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Overridding in plain english Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overridding in plain english" Watch "Overridding in plain english" New topic
Author

Overridding in plain english

Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
hello everyone,
I have just tried to search the forum on overridding, but they don't explain things to me very well. Also the API is gobble-dee-gook to me.
I know that overloading is done in the same class but with a different signature like different arguments. Hence constructors are overloaded.
But overridding gets me everytime, ok, bear with me, overridding is done in a sub class, but the method has to have the exact same method signature, is this true?
so a method in super class would be:
public void aMethod(int i){}
so in the sub class it would be:
public void aMethod(int i){}
I cannot make the sub method more private, true?
but can i change the return type?
and does the arguments still have to be an int, or any that can be promoted to int?
Davy


How simple does it have to be???
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
1) Correct, you cannot reduce the visibility of a method when overriding. There's a substitution rule that says anyplace a base class would work, any derived class should also work. If I relied on a public method on a base class, and you gave me a derived class that made the method private I would not be able to see the method any more, breaking the substitution rule. So the compiler prevents you from reducing the visibility.
2) Return types are technically not part of the signature. I've never tried changing the return type on an override so I'm not sure what would happen. See if it compiles. It would break the substitution rule - if I relied on getting an int from the base class and you gave me a derived class that returned a String, I'd be in trouble.
3) Argument types in the method declaration must exactly match to do an override. If you have different arguments you "overload" the method name instead of overriding the method. But the objects passed can be derived classes. At run time the JVM picks the method with parameter type that best matches the declaration of the argument being sent, that is the most specific type match. Overloading with derived classes can get a bit tricky.
I talked about when I expect a base class and you give me a derived class. Are you familiar with how that looks in code? Here's my class:

And you can call it like this:

I know you'll give me something compatible with BaseClass, but I don't know how many or what kind of derived classes you might make. You can make any number of new derived classes without even telling me and my code will still work.
I'm talking about "my" code and "your" code because it kinda helps think about information hiding. I only know that you have something like BaseClass, I don't have to know all the details of what you really send me. Even if one person writes all the code, it's good to limit what one class "knows" about another. I had a teammate once who took information hiding personally - she thought I really didn't want her to know what I was doing. Had to tell her she was welcome to read the code, but not to use that knowledge in writing her own classes.
Hope that was useful!
[ February 03, 2004: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Thanks Stan for your explanation, i will try to sort some kind of program to test this.
Davy
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
I have made a small program below, which compiles, and i tested it with some changes:

i think i get it now.
Davy
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
You might also appreciate a read of the "How my Dog learned Polymorphism" article in the JavaRanch Campfire Stories.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Cheers Dirk,
I ahve read the polymorphism dog story, but the damn dog is cleverer than me, the dog can pick it up quicker.
But i keep checking it out once in a blue moon, just to remind myself. I have seen in other threads about polymorphism and found these interesting, but i think I got the poly morphism thing, like
X is a super class y is a sub class
X x = new Y();
x sees the super variables but the sub methods,
have i got the jist of it?
Davy
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Perhaps "x can see the accessible items defined in X." is better. And then at runtime, any instance method overridden by Y is used.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overridding in plain english