![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Encoder
|
Protected features
The book Core-Java by Sun Microsystem says:-
A subclass methods can peek inside the protected field of superclass objects only, not of other super-class objects. But I have an example below which violates this rule. public class Test
{
public static void main(String[] args)
{
Employee a = new Employee("Susam");
Manager b = new Manager("Pal",10000);
b.display(a);
}
}
class Employee
{
public Employee(String name)
{
this.name = name;
}
protected void disp()
{
System.out.println("protected method of Employee: disp()");
}
protected String name;
}
class Manager extends Employee
{
public Manager(String name, int salary)
{
super(name);
this.salary = salary;
}
public void display(Employee a)
{
System.out.println("protected instance field of Employee: name: " + a.name);
a.disp();
}
private int salary;
}Here we can see that the display() method of Manager class takes another Employee object and accesses the protected features of the Employee object. Anyone can explain this? |
|
|
|
|
|
#2 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>Anyone can explain this?
Protected access in Java means public to the package, and only accessible outside of the package by classes that participate in the implementation of a class. Because your classes are all in the same package for the entire program, protected is a synonym for public. All of this can be found in the language specification. Of course, good OO style says that all data should be private and only accessible through a public or protected interface. That way you have better control over your internals even of someone decides to extend the class and play.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#3 |
|
Encoder
|
Narue, that was not my question! please go through the rule i have mentioned above, the code and then the question!
i want to know whether the rule is incorrect or am i making a mistake in understanding the rule. |
|
|
|
|
|
#4 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>Narue, that was not my question!
Then ask a better question. The "rule" you gave was ambiguous. If it's a quote from the book and not you paraphrasing then I would say to look for clarifying information in the same general area as that quote and post it here. >i want to know whether the rule is incorrect or am i making a mistake in understanding the rule. As stated, the rule is wrong, and my last post explains why. But you're probably also making a mistake in understanding the rule. However, I don't have access to that book at the moment so I can't double check you.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#5 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
As stated in a recent thread and confirmed by DaWei, protected, public, and so on are on a class to class basis, not object to object.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#6 |
|
Encoder
|
Narue, I am sorry. I paraphrased it. I am quoting the exact lines this time:-
"For example, if the superclass Employee declares the hireDay field as protected instead of private, then the Manager methods can access it directly. However, the Manager class methods can only peek inside the hireDay field of Manager objects, not of other Employee objects. This restriction is made so that you can't abuse the protected mechanism and form subclass just to gain access to the protected fields." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|