Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing in a sub-class to a method but having the super class as the parameter?

Tags:

java

oop

I have an abstract class Vehicle with 2 implemented subclasses RedVehicle and YellowVehicle.

In another class I have a List<Vehicle> containing instances of both subclasses. I want to be able to pass into a method a class type and then use that type to decide which set of objects I want to do something to in the List.

Since Class is generic I should parameterise it with something, however putting the parameter as the parent class Vehicle stops the calling code working since exampleMethod is now expecting a type of Vehicle, not a subclass of RedVehicle or YellowVehicle.

I feel there should be a clean way to do this so what would be the correct way to implement the functionality?

n.b. I don't necessarily have to pass in the Class type, if there are better suggestions I'd be happy to try those.

Calling code:

service.exampleMethod(RedVehicle.class); service.exampleMethod(YellowVehicle.class); 

Fields/Method:

//List of vehicles //Vehicle has 2 subclasses, RedVehicle and YellowVehicle private List<Vehicle> vehicles;  //Having <Vehicle> as the Class parameter stops the calling code working public void exampleMethod(Class<Vehicle> type)  {     for(Vehicle v : vehicles)     {         if(v.getClass().equals(type))         {             //do something         }     } } 
like image 918
Peanut Avatar asked Oct 24 '12 19:10

Peanut


People also ask

Can a class be a super class and a sub class at the same time give example?

Yes it can be!! For example if there are Class A, Class B and Class C then B can be subclass of A and supper class of C. So class B becomes Parent class as well as Child class.

Can I access the sub class methods using a super class object?

Does a superclass have access to the members of a subclass? Does a subclass have access to the members of a superclass? No, a superclass has no knowledge of its subclasses.

Can we pass an object of a subclass to a method expecting an object of the super class?

Answer: Yes, you can pass that because subclass and superclass are related to each other by Inheritance which provides IS-A property.

How do you use the super class method in subclass?

Uses of super keywordTo call methods of the superclass that is overridden in the subclass. To access attributes (fields) of the superclass if both superclass and subclass have attributes with the same name. To explicitly call superclass no-arg (default) or parameterized constructor from the subclass constructor.


2 Answers

Do this instead:

public <T extends Vehicle> void exampleMethod(Class<T> type)  
like image 99
mprivat Avatar answered Sep 29 '22 07:09

mprivat


Why don't you use the visitor pattern?

That way you

  • don't need type tokens
  • let dynamic dispatch handle the case distinction (instead of if(v.getClass().equals(type)))
  • are more flexible (following OCP)

In detail:

your abstract class Vehicle gets a method accept(Visitor v), with the subclasses implementing it by calling the appropriate method on v.

public interface Visitor {   visitRedVehicle(RedVehicle red);   visitYellowVehicle(YellowVehicle yellow); } 

Using a visitor:

public class Example {    public void useYellowOnly() {     exampleMethod(new Visitor() {         visitRedVehicle(RedVehicle red) {};         visitYellowVehicle(YellowVehicle yellow) {              //...action         });   }   public void exampleMethod(Visitor visitor){       for(Vehicle v : vehicles) {           v.accept(visitor);       }     } } 
like image 33
DaveFar Avatar answered Sep 29 '22 09:09

DaveFar