Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 28th, 2005, 7:22 AM   #1
susam_pal
Encoder
 
Join Date: Sep 2005
Location: India
Posts: 12
Rep Power: 0 susam_pal is on a distinguished road
Send a message via Yahoo to susam_pal
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?
susam_pal is offline   Reply With Quote
Old Sep 28th, 2005, 7:52 AM   #2
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>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.
Narue is offline   Reply With Quote
Old Sep 28th, 2005, 9:18 AM   #3
susam_pal
Encoder
 
Join Date: Sep 2005
Location: India
Posts: 12
Rep Power: 0 susam_pal is on a distinguished road
Send a message via Yahoo to susam_pal
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.
susam_pal is offline   Reply With Quote
Old Sep 28th, 2005, 10:44 AM   #4
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>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.
Narue is offline   Reply With Quote
Old Sep 28th, 2005, 4:15 PM   #5
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
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
Dameon is offline   Reply With Quote
Old Sep 29th, 2005, 6:14 AM   #6
susam_pal
Encoder
 
Join Date: Sep 2005
Location: India
Posts: 12
Rep Power: 0 susam_pal is on a distinguished road
Send a message via Yahoo to susam_pal
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."
susam_pal is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:49 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC