Results 1 to 4 of 4
  1. #1
    Gloomy is offline Member
    Join Date
    Jul 2010
    Location
    Wollongong, NSW, Australia
    Posts
    6
    Rep Power
    0

    Default Inheritance logic problem

    Hello, ive got a few classes, Bus, Car, Train, and Truck, they all inherit from the class Vehicle.

    the Vehicle class contains members and methods which are common to all the subclasses like, type of vehicle(bus, car, train, truck), weight, price, owner, registration number and so on...

    the problem im having is with accessing the subclasses methods in main(), for example, one of the subclasses "Bus" contains its own members like Number of Seats, and Company Name, and they have set and get methods, .

    When I try to access or change the value of the NumberOfSeats or CompanyName, i do this in the main:

    Java Code:
    List<Vehicle> vehicleList = new ArrayList<Vehicle>();
    vehicleList.add(new Bus());
    vehicleList.add(new Truck());
    vehicleList.add(new Train());
    
    ((Bus)veh).setCompanyName(); /* setCompanyName doesnt belong to the Vehicle class */
    
    // is there a way I could just do
    
    vehicleList.get(0).setCompanyName()
    ;

    In alot of cases im finding myself doing this...

    Java Code:
    String subclassType = vehicleList.get(0).getVehicleType(); /* returns either Bus, Car, Train or Truck as a string */
    
    if(subclassType.equals("Truck")){
             ((Truck)veh).SomeTruckOnlyMethod();
    }else if(subclassType.equals("Bus")){
             ((Bus)veh).someBussOnlyMethod();
    }

    Is there a better way to do this? because it looks really messy with so many If statements because i have alot of subclasses which inherit from Vehicle, also I didnt really want all those subclasses methods to be in the super class so access them because alot of the subclasses dont need them, like companyName which is only for the Bus.

    Sorry for the long post, also ignore any syntax errors in the above code, i jsut typed that up in here to show you the basic way ive done it.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    You can try to use the Visitor pattern. The Visitor visits a Vehicle and the Vehicle tells the Visitor what it actually is. You'd probably have to make the Vehicle class and abstract class (or an interface). The pattern is also called the 'double dispatch' pattern because first the vistitor calls the visit(this) call on the (abstract) Vehicle and the Vehicle calls the appropriate method on the passed in Visitor (Bus, Train etc.). Finally the Visitor 'knows' what it initially visited and can do with the result what it wants.

    kind regards,

    Jos

  3. #3
    Gloomy is offline Member
    Join Date
    Jul 2010
    Location
    Wollongong, NSW, Australia
    Posts
    6
    Rep Power
    0

    Default

    hey thanx for the reply, that sounds great, ill look into it and see how I go :)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Gloomy View Post
    hey thanx for the reply, that sounds great, ill look into it and see how I go :)
    The pattern works fine, but only if you car inheritance doesn't change (too frequently); if you want to add another subclass of the Vehicle super class you have to add one more method to the visitor to handle that new class.

    kind regards,

    Jos

    p.s. I wrote a little article about this pattern once; here it is.
    Last edited by JosAH; 08-28-2010 at 09:40 AM.

Similar Threads

  1. Inheritance problem
    By ZuperZombie in forum Advanced Java
    Replies: 0
    Last Post: 04-02-2010, 04:55 PM
  2. Inheritance Problem
    By g2beastie in forum New To Java
    Replies: 4
    Last Post: 03-25-2010, 09:23 PM
  3. inheritance problem
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 03-10-2010, 07:01 PM
  4. Problem with Inheritance
    By KronikAlkoholik in forum New To Java
    Replies: 4
    Last Post: 08-25-2009, 01:13 AM
  5. problem with <logic:iterate> tag looping
    By tsaswathy in forum Web Frameworks
    Replies: 0
    Last Post: 09-27-2008, 01:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •