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?
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".
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