Search code examples
javapolymorphismsubclasssuperclass

Subclass abstract method not being called


I will post some code and then ask my question, because I think it needs some explaining. So here is a basic representation of my superclass:

public abstract class ModeOfTransport {
    public abstract void updateView(String name);
}

Here are some examples of my subclasses:

public class Bus extends ModeOfTransport {
    @Override
    public void updateView(String stopName) {
        System.out.println("BUS");
    }
}

public class Train extends ModeOfTransport {
    @Override
    public void updateView(String stopName) {
        System.out.println("TRAIN");
    }
}

I have a switch that is meant to be able to decide which version of updateView() is called:

switch(transportType) {
    case BUS:
        handleInfo(new Bus());
        break;
    case TRAIN:
        handleInfo(new Train());
        break;
    default:
        break;
}

private void handleInfo(ModeOfTransport transportType) {
    transportType.updateView(name);
}

My goal is to print out either TRAIN or BUS. At the moment, it prints nothing! I'm guessing because it is calling the abstract method in the parent class, which has no body.

Can anyone shed any light on this for me?

Also, as a side question, does this fall under polymorphism or inheritance?

Thanks!


Solution

  • I've made it work. I have no idea what you're doing wrong, but perhaps you'll figure it out from this source code.

    package transport;
    
    /**
     * ModeOfTransport description here
     * @author Michael
     * @link http://stackoverflow.com/questions/14097153/java-subclass-abstract-method-not-being-called/14097167#14097153
     * @since 12/30/12 10:46 PM
     */
    public abstract class ModeOfTransport {
        public abstract void updateView(String name);
    }
    
    package transport;
    
    /**
     * Bus description here
     * @author Michael
     * @link http://stackoverflow.com/questions/14097153/java-subclass-abstract-method-not-being-called/14097167#14097153
     * @since 12/30/12 10:47 PM
     */
    public class Bus extends ModeOfTransport {
        @Override
        public void updateView(String name) {
            System.out.println("BUS");
        }
    }
    
    package transport;
    
    /**
     * Train description here
     * @author Michael
     * @link
     * @since 12/30/12 10:48 PM
     */
    public class Train extends ModeOfTransport {
        @Override
        public void updateView(String name) {
            System.out.println("TRAIN");
        }
    }
    
    
    package transport;
    
    /**
     * TransportVisitor description here
     * @author Michael
     * @link http://stackoverflow.com/questions/14097153/java-subclass-abstract-method-not-being-called/14097167#14097153
     * @since 12/30/12 10:49 PM
     */
    public class TransportVisitor {
        public static void main(String[] args) {
            TransportVisitor visitor = new TransportVisitor();
            String [] types = { "TRAIN", "BUS" };
            for (String type : types) {
                visitor.updateView(type);
            }
        }
    
        public void updateView(String transportTypeName) {
            switch(transportTypeName) {
                case "BUS":
                    handleInfo(new Bus());
                    break;
                case "TRAIN":
                    handleInfo(new Train());
                    break;
                default:
                    break;
            }
        }
    
        private void handleInfo(ModeOfTransport transportType) {
            transportType.updateView((String) null);
        }
    }