Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

disabling method from an extended class? 1

Status
Not open for further replies.

fenris

Programmer
May 20, 1999
824
CA
I have a parent class with a number of methods.

For all the child classes except one, they all use the methods in addition to some of their own. I have one class that doesn't require one of the methods of the parent class and it doesn't make sense for that particular child to have access to that method. Is there anyway that I can "block" the child from accessing that method of the parent class.

To clarify this, I make an abstract class called 3dShapes. It has abstract methods of getLength(), getWidth() and getHeight(). Now these methods make sense when you are deal with cubes and such, but the width or height method doesn't make sense when it comes to a sphere.

I hope this is clear.....
 
Your dilemna is probably an indication of a basic design flaw. In the example you give, the 3DShapes class shouldn't have a getWidth() or getHeight() method because, like you said, this doesn't make sense for all 3D shapes (um, not sure how you would figure the length of a sphere either). Having these methods in a class like this is like having a deploySunroof() method in a base car class (not ALL cars have sunroofs!).

Without knowing the specifics of your problem, it sounds like you need to remove some attributes/methods from your base class, add a couple of intermediate classes that both extend the base class and then have all but one of your original subclasses extend one of the new classes and the remaining subclass extend the other new class. Similar to below where 3DShape is your base class and Rectangle, Square and Sphere were the classes that originally extended it. (the diagram is a bit messy in here)

3DShape
|
--------------------------------
| |
CircularShape BoxLikeShape
| |
------- ------------
| | |
Sphere Square Rectangle
 
Thank you very much for your answer, it was very informative and the type of answer that I was looking for. This is the kind of stuff that I know I am lacking. I wasn't sure how to approach this problem. I come from a very strong procedural background and am into speed, getting the code to run as fast as possible, not the illegal stuff ;) . I had no trouble learning the syntax and how to create A class that can do something like numerical root finding. But when it comes to things like creating object hierarchies that make sense is where I have toruble.

How does having intermediate classes affect the performance of the java application in general?
 
Hi :)
In answer to your last question, from experience I'd say not much at all. Some of my classes are subclassed maybe 7 8 times or so and theres no remarkable difference in effectivity of the system. (these classes all subclass some java class, which in turn often is several steps away from the Object class.)

On the other hand, it lets me organize my thinking into groups and sub-groups, making it really easy to code and reuse code in totally different programs. Also, its easier for other programmers to work on separate parts of the program (classes) without real problems (this, of course, is in small systems... my client server system with 150+ classes is a pain... but you get my point.)

So I guess once you learn to plan your classes and their relations in an object-oriented way you save time in various parts of the coding phase but not that much in actual performance.

Finally, if you want speed, try to analyze where in the code it gets bogged down, then write that part in c. Java lets you use compiled C code methods... I've done this rarely, but it does make things faster.

Hope this helps explain things.
Cheers
Mattias
 
Thanks for your response, I appreciate it. I know how to code but not in an OO way. That is for me the hardest part but I am starting to get the hang of it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top