Results 1 to 4 of 4
Thread: Inheritance logic problem
- 08-28-2010, 07:01 AM #1
Member
- Join Date
- Jul 2010
- Location
- Wollongong, NSW, Australia
- Posts
- 6
- Rep Power
- 0
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.
- 08-28-2010, 07:57 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
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
- 08-28-2010, 07:59 AM #3
Member
- Join Date
- Jul 2010
- Location
- Wollongong, NSW, Australia
- Posts
- 6
- Rep Power
- 0
hey thanx for the reply, that sounds great, ill look into it and see how I go :)
- 08-28-2010, 08:23 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
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 08:40 AM.
Similar Threads
-
Inheritance problem
By ZuperZombie in forum Advanced JavaReplies: 0Last Post: 04-02-2010, 03:55 PM -
Inheritance Problem
By g2beastie in forum New To JavaReplies: 4Last Post: 03-25-2010, 08:23 PM -
inheritance problem
By er1c550n20 in forum New To JavaReplies: 2Last Post: 03-10-2010, 06:01 PM -
Problem with Inheritance
By KronikAlkoholik in forum New To JavaReplies: 4Last Post: 08-25-2009, 12:13 AM -
problem with <logic:iterate> tag looping
By tsaswathy in forum Web FrameworksReplies: 0Last Post: 09-27-2008, 12:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks