#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    4
    Rep Power
    0

    Should Add function be in Superclass;


    Code:
    abstract class Vehicle {
    	public abstract void add(Vehicle b);
    }
    
    class Car extends Vehicle {
    	String varVechicle;
    	public void add(Vehicle b) { }
    }
    
    class Motorcycle extends Vehicle {
    	int varMotorcycle;
    	public void add(Vehicle b) { }	
    }
    Basically we need every Vehicle sub class to implement an Add function. If we do this we would need to implicity downcast the Vehicle Object in every respective add method and see to that its of 'this' type.

    Does it make good design sense to add public abstract void add(Vehicle b); in Vehicle class. Any better way of doing what i want to do?
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2007
    Posts
    1,940
    Rep Power
    3117
    I don't really like the downcasting. Also, how do you add a Vehicle to a Vehicle. This doesn't seem to make sense in the real world. If you could explain further someone might have another suggestion.

    Maybe something like using the VistorPattern would work, but again, not sure what you are doing.

    Comments on this post

    • NovaX agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2005
    Location
    Bay Area, California
    Posts
    841
    Rep Power
    1681
    Technically speaking, I believe that you can use generics to add a generic method that each implementation provides a subtyped contract. See the Generics FAQ for an explanation of this use in Java's enum.

    From a design perspective, I agree with jzd.. please just don't do it. It doesn't fit the domain model of a vehicle (roles, responsibilities, collaborations) from what I can tell. You should also favor composition over inheritance whenever possible. Its far better to make the interactions simpler and have more classes than have overly complex logic and interactions.
    Core design principles when developing software systems.
    See my open-source project as an example of professional code.
    ---
    The opinions expressed do not represent those of my employer.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    886
    Are these supposed to be some sort of containers for tracking actual vehicles and their types?
    I no longer wish to be associated with this site.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    4
    Rep Power
    0

    adding vehicles;


    We could imagine adding two vehicles as adding the 'total wheels' of both the vehicles. Not every combination of vehicles can be added to each other, (for e.g. a car and bike could be added however it doesn't make sense for a car and ship to be added).

    Again, this is purely a conceptual doubt and NOT a real world problem that i am facing. By seeing the replies i dont think its good design sense to do this.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2008
    Posts
    62
    Rep Power
    13
    Definitely Vehicle should have an add method if all the vehicles need it. IMHO, this is actually the reason for which abstract methods exists.
    To find out everything about design patterns check the Design Patterns Reference on oodesign.com.

    Find and share the best tips, tutorials and howto's for php web developers on the newly started php-html.net community.

IMN logo majestic logo threadwatch logo seochat tools logo