473,395 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Question about protected modifier

AIK
I have the following two classes:

package birdpack;

public class Bird {

Bird() {}

protected int nFeathers;

}

in bird.java

and

package duckpack;
import birdpack.Bird;

public class Duck3 extends Bird {

Duck3() {}

void foo() {
Bird b = new Bird();
int temp;
temp = b.nFeathers;
}

}

in Duck3.java

When I compile Duck3 I get this error: nFeathers has protected access in
birdpack.Bird. Why is this? I thought the point of a protected modifier is
to allow children of a class to access parent's protected feature even if
the child class is in a different package?

Jul 17 '05 #1
1 3957
"AIK" <as*******@hotmail.com.error> wrote in news:u5BUc.457$euc.91
@news04.bloor.is.net.cable.rogers.com:
I have the following two classes:

package birdpack;

public class Bird {

Bird() {}

protected int nFeathers;

}

in bird.java

and

package duckpack;
import birdpack.Bird;

public class Duck3 extends Bird {

Duck3() {}

void foo() {
Bird b = new Bird();
int temp;
temp = b.nFeathers;
}

}

in Duck3.java

When I compile Duck3 I get this error: nFeathers has protected access in
birdpack.Bird. Why is this? I thought the point of a protected modifier is to allow children of a class to access parent's protected feature even if
the child class is in a different package?


Yes, that is the point, but there are rules to follow.
You can see the rules in section 6.6.2 of the Java Language Specification
at
http://java.sun.com/docs/books/jls/f...doc.html#33916

The rules appear to be designed in part so that a subclass may maintain the
protected fields of its superclass (of the same instance). When you access
a superclass field of a different instance, you are no longer providing
maintenance of your object. (My language here is inexact; The JLS is
exact but can be difficult to penetrate).

If you had written
temp = super.nFeathers ;
I think that you would meet the rules. You would be accessing the
nFeathers associated with the parent of the Duck3 that you are maintaining.

By creating a new Bird and attempting to access its nFeathers field, you
would be accessing a protected field of an object that is NOT implemented
by this code.

Why does Duck3 (which is already a Bird) need to create another Bird
anyway?
Bird b = new Bird();
is a strange thing to have in Duck3.
--
Ian Shef
These are my personal opinions and not those of my employer.
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Jason Heyes | last post by:
Is this class evidence of poor design? class Rectangle { double width, height; public: double get_width() const { return width; } double get_height() const { return height; } void...
41
by: GC | last post by:
Within a class, do you use the private keyword for your internal member variables or do you leave it off like you would for variables within a function?
2
by: Dan Cimpoiesu | last post by:
Hello What does mean when a protected modifier is apllied to a class. I want to inherit a protected class defined in another class, I saw that the microsoft programmers achieved that. But I...
3
by: James Carnley | last post by:
I am moving over from Java and have found something strange in the way C# works. Can anyone explain this to me? I have 4 classes which I all want to share one Print method yet each class will...
5
by: | last post by:
hello there, what is the difference between Shared and Protected Shared? where can I read more about theses kind of variables (or whatever they are....sorry, don't know the word in eng.) ...
4
by: B. Chernick | last post by:
I'm reviewing the 70-305 course and I'm trying to do the Chapter 5 lab. I've created the Calls.aspx page and the dsCalls xsd. I've set the DataSource of dlstcalls to...
4
by: Grigs | last post by:
Hello, I have a fairly simple class that I created and it works great. Basically it remembers a certain amount of strings. When a new one comes in the oldest drops off. The problem is, when I...
13
by: Tom Baxter | last post by:
Hi everyone, Has anyone looked at section 18.1.1 of the C# spec? It indicates 'new' and 'protected' are valid modifiers on struct declarations. First, how can 'protected' be valid on a struct,...
8
by: Mayur H Chauhan | last post by:
All, For my knowledge, if I declare Class as follow, then it thows compilation error. Protected Class Book End Class Even same for...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.