Why part 2 does not work: The
paint() method in class Pro is protected. That means that you can only access it in the class itself, in subclasses and in classes in the same package. You are trying to access it, in line 16, from the class ProM, which is not a subclass of Pro and which is not in the same package as Pro. So you are not allowed to access it there.
Why Part 3 works: Class Son is a subclass of class Father, and class Father is a subclass of class Pro. Note that Son is also a subclass of class Pro (altough not directly). Because of this, class Son is allowed to access the
paint() method of class Pro, so you can call it from the
pint() method in line 10.
vipul hasija wrote:because if the function paint is default or protected or public then it can only be inherited by son class...
I don't understand what you mean by this. If a method has default, protected or public access, then it can be accessed by subclasses, and it can be overridden in subclasses.
vipul hasija wrote:if part2 code is not working then it means that function paint inherited in father class is private...
No, it isn't private in the Father class. You can't access protected methods from class ProM because it is not a subclass of Pro and it is not in the same package as Pro.
See:
Controlling Access to Members of a Class