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

OO

Shaista patel
Greenhorn

Joined: May 08, 2002
Posts: 1
Hello guyz,
I just have a quick and maybe kinda dumb question. Suppose there is this public class Box which has methods like finding area, volume etc of a box. Now if I wanna create another class that is a subclass of this class containg methods like adding, subtracting the areas and volumes of two boxes how can I do that? I mean do i need to create two box objects? And how can I test this subclass? What I donno is how can I write methods for the subclass without creating two actual objects. Can anyone plz help me?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Shai,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change yer display name to comply with The JavaRanch Naming Policy.
Thanks Pardner!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
In your derived class, it looks like you'd want to consider the current Box object (this) and some parameter Box object. These would be the two objects to use in your calculations. For example, let's consider java.awt.Rectangle::add(Rectangle) "which adds a Rectangle to a Rectangle. The resulting Rectangle is the union of the two rectangles.

The method union(Rectangle) of this same class, behaves similarly while returning a new Rectangle object "that represents the union of the two rectangles".

Are you getting any ideas?
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi Shai,
if i would have been there i'd have created two Box objects and would have a test class say - BoxCalculator. i'd put add,subtract etc methods in this class and will pass two box objects (or multiple box objects) to the arguments of this methods.
i don't agree with Subclassing a Box class and do the required operations. the reason is if you say you want things in OO then you should ask youself "why" about the design you made.
the parent/child relationship between two class doesn't apply into the problem statement you have.
you should ONLY subclass a class if the child is going to modify(extend/restrict) behavior of the parent class. in your problem statement the objective you have is to know addition / subtraction etc of two Box objects in terms of area, volume etc. Now that functionality doesn't extend the Box class behavior. it is just using Box object. thats why we should have a second class BoxCalculator (or whatever u want to call it).
simply think,
u r child of your parent even if u might have properties/charcteristics similar to other parents.
don't just PLUG/HOOK up things to make them work easier. we should try to apply logical design. it just doesn't fit into my mind that i'll ever subclass Box class to do something like you want to do.
anyways, this were just my 2 cents!!! i love to learn design.
any other OO experts?
regards,
maulin
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
btw,
sorry for irrelavent "signature" statement about Servlets. i keep it as i usually have questions on servlet and dont have access to the sevlet environment.
regards
maulin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OO