Java – why convert up a class that doesn’t change the rewriting method?

See English answers > overriding member variables in Java 10

class BritishPerson {
    public String name = "A british name";

    public void salute() {
        System.out.println("Good Morning!");
    }
}

class ScottishPerson extends BritishPerson {
    public String name = "A scottish name "; //Variable overriding
    public String clanName = "MacDonald";

    public void salute() //Method overriding
    {
        System.out.println("Madainn Mhath!");
    }

    public void warcry() {
        System.out.println("Alba Gu Brath!");
    }
}

class Driver {

    public static void main(String[] args) {
        ScottishPerson scottishPerson = new ScottishPerson(); //Created as a subclass,can always be upcasted.
        BritishPerson britishPerson = new BritishPerson(); //Created as the superclass,throws an error when downcasted.
        BritishPerson britishPersonUpcasted =
                new ScottishPerson(); //Created as the subclass but automatically upcasted,can be downcasted again.

        //Checking the methods and parameters of scottishPerson
        scottishPerson.salute();
        scottishPerson.warcry();
        System.out.println(scottishPerson.name);
        System.out.println(scottishPerson.clanName);

        //Checking the methods and parameters of britishPerson
        britishPerson.salute();
        System.out.println(britishPerson.name);

        //Checking the methods and parameters of britishPersonUpcasted
        britishPersonUpcasted.salute();
        System.out.println(britishPersonUpcasted.name);
    }
}

Run the code, this is the output

Madainn Mhath!
Alba Gu Brath!
A scottish name 
MacDonald
Good Morning!
A british name
Madainn Mhath!
A british name

This is the chaos Upload scottishperson to britishperson and change the variable name to the variable name defined in the supercategory Discard only methods and variables that exist in subclasses, such as warcry() and clanname However, calling the method salute () on the upcasted class will still return a string based on the subclass implementation

Is it because when I create the object britishperson, I only initialize the britishperson class. When I create the object britishperson updated, I create the britishperson class and scottishperson class, resulting in the permanent overwrite of the salute () method?

Solution

Please check this question to better understand upload and download:

What is the difference between up-casting and down-casting with respect to class variable

I also gave an example of viewing upload behavior:

abstract class Animal 
{ 
    public void saySomething()
    {
        System.out.println("Some Animal sound");
    }

    public abstract void getTheBall();
}

class Horse extends Animal
{ 
    public void saySomething()
    {
        System.out.println("Neigh Neigh");
    }

    public void getTheBall()
    {
        System.out.println("I won't,Try a dog,I am a Horse!");
    }
}

class Dog extends Animal 
{ 
    public void saySomething()
    {
        System.out.println("woof woof,waon waon");
    }

    public void getTheBall()
    {
        System.out.println("huf huf,here it is!");
    }
}

public class Main 
{
    public static void main (String [] args) 
    {
        Dog dog = new Dog(); 
        Horse horse = new Horse();
        Animal animal = dog;
        Animal horseAnimal = new Horse();

        //upcasting
        Dog upcastedAnimal = upcastToDog(animal);
        dog.saySomething();
        dog.getTheBall();

        upcastedAnimal.saySomething();
        upcastedAnimal.getTheBall();

        horse.saySomething();
        horse.getTheBall();

        try {
            Dog upcastedDog = upcastToDog(horseAnimal);
        } catch (Exception ex){
            System.out.println(ex.getClass().getSimpleName() + ": ObvIoUsly a horse is not a dog!");
        }
    }

    public static Dog upcastToDog(Animal animal){
        return (Dog) animal;
    }
}

Output:

woof woof,waon waon
huf huf,here it is!
woof woof,here it is!
Neigh Neigh
I won't,I am a Horse!
ClassCastException: ObvIoUsly a horse is not a dog!

First, if you try to convert incompatible types, Java throws an exception

When a cast is possible, the override method is always called from the actual instance In your case, the instance is scottishperson, so even if you hold its reference in britishperson, the method will be called on scottishperson

You can run this example here https://repl.it/B83f/3

In JLS, "narrowing reference conversion" is covered here. As his name implies, only the reference is reduced or expanded (or increased or decreased) rather than the instance

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>