简体   繁体   中英

Calling a overridden method of subclass from superclass error?

I have following two set of Java code. The first one works but the second one doesn't?

package animal;

import javax.swing.JOptionPane;
import java.util.ArrayList;

public class Animal {

    public static void main(String[] args) {
        Animal createAnimals = new Animal();
        createAnimals.printInfo();
        String userInput = createAnimals.userInputHandle();
        Fishes a = new Fishes();

        switch (userInput) {
            case ("shark"):
                a.printInfo();
        }
    }

    private void printInfo() {
        JOptionPane.showMessageDialog(null, "Welcome to Animal Kingdom.");
    }

    private String userInputHandle() {
        String userInput;
        userInput = JOptionPane.showInputDialog("Select animal from the "
                + "following list"
                + "\n1.Dog\n2.Cat\n3.Snake\n4.Frog"
                + "\n5.Human\n6.Shark\n7.Sea Gulls");
        userInput = userInput.toLowerCase();

        return userInput;
    }
}

class Fishes extends Animal {

    public void printInfo() {
        JOptionPane.showMessageDialog(null, "Shark belongs to Fish subclass of Animal kingdom.");

    }
}

and second set of code is

package animal;

import javax.swing.JOptionPane;
import java.util.ArrayList;

public class Animal {

    public static void main(String[] args) {
        Animal createAnimals = new Animal();
        createAnimals.printInfo();
        String userInput = createAnimals.userInputHandle();

        ArrayList<Animal> animalList = new ArrayList<Animal>();

        animalList.add(new Fishes());

        switch (userInput) {
            case ("shark"):
                animalList.get(0).printInfo();
            case ("sea gulls"):
        }
    }

    private void printInfo() {
        JOptionPane.showMessageDialog(null, "Welcome to Animal Kingdom.");
    }

    private String userInputHandle() {

        String userInput;
        userInput = JOptionPane.showInputDialog("Select animal from the "
                + "following list"
                + "\n1.Dog\n2.Cat\n3.Snake\n4.Frog"
                + "\n5.Human\n6.Shark\n7.Sea Gulls");
        userInput = userInput.toLowerCase();

        return userInput;
    }
}

class Fishes extends Animal {

    public void printInfo() {
        JOptionPane.showMessageDialog(null, "Shark belongs to Fish subclass of Animal kingdom.");

    }
}

Here the overridden method printInfo is not invoked with animalList.get(0).printInfo() instead class animal's method is invoked? Why this is happening?

#printInfoAnimalprivate ,不能被子类覆盖,您应该将其设置为protected

private methods are not overridden in sub-class . change

   private void printInfo() { 

to

  public  void printInfo() {

or

  protected  void printInfo() {

In Animal Class .

Try casting the object into the subclass ie (Fishes)animalList.get(0).printInfo();

Note before casting it is always a good idea to check what type an object is using an if statement using the instanceof comparator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM