To override a class's variable in Java

Asked 1 years ago, Updated 1 years ago, 66 views

class Dad
{
    protected static String me = "dad";

    public void printMe()
    {
        System.out.println(me);
    }
}

class Son extends Dad
{
    protected static String me = "son";
}

public void doIt()
{
    new Son().printMe();
}

When you execute this code, "dad" is printed out. Can't we print out the son?

inheritance java override

2022-09-22 22:09

1 Answers

I can do it. If you do not define me in the lower class, me in the upper class is automatically overridden.


2022-09-22 22:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.