link should help understanding why line 7 and 8 throws compile time error.
say() method is proected in the Super class so the class in Package 2 "does" inherit it AND we can call it directly on the SubTwo object via s2.say() or some other method in SubTwo can call say() directly BUT we can't refer to say() on references s or s1 as say() ...
when an method is declared protected ,it can be accessed only through inheritance since out of the package an protected method is avaliable only to its subclasses.
Since SubTwo is in package other than that of Super, it cant access protected members of Super. If I will make their package same, then it wil compile. So then what is the difference between default access and protected access. Isnt the protected access behaving as default access here...?
A default member may be accessed only if the class accessing the member belongs to the same package,whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package. [ June 24, 2005: Message edited by: Srinivasa Raghavan ]
here's the difference between default access and protected access: -----------------------------------------------------------------
Default access: accessible to classes inside the same package.
Protected access: accessible to classes inside the same package and subclasses in other packages.There's one additional rule for protected access.The subclasses which are in other packages cannot access the class through the class's reference.They have to define a reference of their own type(subclass type) and use it to call the superclass's protected method
"The subclasses which are in other packages cannot access the class through the class's reference.They have to define a reference of their own type(subclass type) and use it to call the superclass's protected method "
Whay is that rule in place ? What good does it do ?