D
D
Den4_x2019-07-26 15:27:13
Java
Den4_x, 2019-07-26 15:27:13

Why, when inheriting a class, I cannot see the variable, although it is with the protected modifier?

class Math{
       protected static final double PI = 3.14;       
}

class SubMath extends Math{
    Math.PI; // выдает ошибку
}

If everything is very obvious here, then please do not scold me, I'm still learning =)
Thank you in advance for your ANSWER!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2019-07-26
@Den4_x

Because only declarations can be in the body of a class, and instructions should only be in the body of methods.

U
user49981, 2019-07-26
@user49981

нельзя испоьзовать 3.14 как переменную
class SubMath extends Math{
    3.14 // выдает ошибку
}

class Math{
       protected static final double PI = 3.14;       
}

class SubMath extends Math {
    public static double pi() {
        return Math.PI; // 3.14
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question