educative.io

Educative

Quiz on Classes and Inheritance

why my answer is true, some one explian me why the correct answer is false?thanks

There is no difference in a protected and public variable of a mother class in terms of accessibility and use for a derived class.

Is this statement correct?

Your Answer

###### A)

True

Correct Answer

###### B)

False

Course: Learn Java from Scratch - Free Interactive Course
Lesson: Quick Quiz! - Learn Java from Scratch

In object-oriented programming, a mother class is a class that serves as a blueprint for creating other classes (known as derived classes). When a class is derived from a mother class, it inherits all the properties and methods of the mother class.

In terms of variables, the derived class can access and use both protected and public variables defined in the mother class. The difference between the two lies in their accessibility from outside the class hierarchy.

A protected variable can only be accessed from within the same package or a subclass (i.e., a derived class), whereas a public variable can be accessed from anywhere. This means a protected variable has more restricted access than a public variable.

In other words, a protected variable can only be accessed by the mother class, any derived classes, and any classes in the same package, while a public variable can be accessed by any code that has access to the class, it is defined in.

Thus, the statement “There is no difference in a protected and public variable of a mother class in terms of accessibility and use for a derived class” is incorrect, and we would select the “False” option in a True/False choice.

Happy learning at Educative.

1 Like

very clear now the issue,
Thank you very much.

1 Like