part2 following code will not work please explain why..?
father ineherit protected member function paint of Pro class
part3 if above code is not working then why below code is working..?
because if the function paint is default or protected or public then it can only be inherited by son class...
if part2 code is not working then it means that function paint inherited in father class is private...
please explain I getting mess up between these...
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.
vipul hasija wrote:please reply that part 3 is running only till father class is in same package if package differs then paint function of pro class inherited by father class will not be available to son class.
You can just try it out yourself.
I understand correctly what you mean, then your statement is wrong. Suppose we have class Pro in mypack with a protected paint() method, class Father in mypack, and class Son in the default package. Then from inside class Son you can still call the paint() method, because Son is a subclass of Pro (even though it is not a direct subclass).
The rules for protected are that protected members can be accessed from:
the class in which the member is defined itself
subclasses (either direct or indirect subclasses); it does not matter if the subclass is in the same package or not
classes in the same package; it does not matter if these are subclasses or not
Protected members cannot be accessed from any other place which does not fit at least one of those three criteria.