aspose file tools*
The moose likes Java in General and the fly likes Overriding methods, but at the same time providing access to the superclass methods. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Overriding methods, but at the same time providing access to the superclass methods." Watch "Overriding methods, but at the same time providing access to the superclass methods." New topic
Author

Overriding methods, but at the same time providing access to the superclass methods.

Kevin Tysen
Ranch Hand

Joined: Oct 12, 2005
Posts: 255


What I want to do is override getDogs() and getDogEnumerator() so that a list of only the expensive Dogs are returned. The way to find out if a Dog is expensive is by checking the Dog[] expensiveDogs. That is no problem. I know how to do that. But I also want to provide a way to get to the list of Dogs (including all Dogs, expensive or not) provided by the superclass DogInfo. I'll call that method getAllDogs().
Suppose I don't know anything about the code in the superclass methods. So there is a chance that, the superclass method getDogEnumerator() calls getDogs(), and there is also a chance that getDogs() calls getDogEnumerator(). So how can I write the code for getAllDogs()? Think about it: If I call super.getDogs() and if the superclass calls getDogEnumerator() in the getDogs() method, the overriden getDogEnumerator() method will be called and only the expensive Dogs will be returned. If I call super.getDogEnumerator() and if the superclass calls getDogs() in the getDogEnumerator() method, the overriden getDogs() method will be called and only the expensive Dogs will be returned. If I know that getDogEnumerator() calls getDogs(), and that getDogs() does not call getDogEnumerator(), then I can write:

and if I know that getDogs() calls getDogEnumerator() and that getDogEnumerator() does not call getDogs(), then I can do this.


What's a poor Java programmer to do?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4460
    
    8

If I were you I'd take a step back and question your decision to override the methods - possibly to extend DogInfo at all. It seems to me that you're trying to redefine what getDogs() and getDogEnumerator() mean, not just how they're implemented. Which seems to me to break the idea of inheritance.

Without knowing more, I think I'd take one of two approaches:

- If you want something to keep track of a bunch of expensive dogs, you can do that with a separate instance of DogInfo:

- If you really need to use the approach you're using for some reason: instead of redefining getDogs() and introducing a new method getAllDogs(), leave getDogs() alone and introduce a new method getExpensiveDogs(). That would avoid the problem you're running into.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding methods, but at the same time providing access to the superclass methods.