Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OO

 
Shaista patel
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic