Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

CodyVa

[java] newbie ques. Protected modifier?

This topic is 6609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can anybody explain this to me, i worked with some examples and compiled them as from the documentation, and only 1 compile time error happened instead of 2... If i declare something private, it can only be accessed from a method inside that class? is this right? a protected member (in a base class) can only be accessed in the same class, or subclass right? only if it is in the same package right? otherwise it won''t work? cause it''s not in the same package. is this right? But the doc''s said a sub class of the base class can''t touch the base protected member, it has to attempt to cast it to the sub class? is that right? Also saying the base class can''t access a sub class protected members? I understand alot of stuff but i''m confused with what the compiler says to me when i do the examples and what the docs say? can anybody clear this up for me please? I''m not sure either how being in a different package messes anything up (i do understand diff packages can only instance public classes and call public methods) any help is definatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement
package points;



public class Point {
protected int x, y;

void warp(threePoint.Point3d a) {
if (a.z > 0) // compile-time error: cannot access a.z
a.delta(this);
}
}

and the threePoint package declares:


package threePoint;
import points.Point;



public class Point3d extends Point {
protected int z;

public void delta(Point p) {
p.x += this.x; // compile-time error: cannot access p.x
p.y += this.y; // compile-time error: cannot access p.y
}

public void delta3d(Point3d q) {
q.x += this.x;
q.y += this.y;
q.z += this.z;
}
}



that was the code... the gave me...is this right?
when i put it just like that..it didn''t work..only got one error.

Share this post


Link to post
Share on other sites
I dont'' understand before or after Main either? you mean the Main method that''s called when it starts?

k, I think i understand that...

could you base it on the code that i posted above?
thanks for any help.

Share this post


Link to post
Share on other sites
Hi there;

You are getting an error because variables declared with the access "protected" means ONLY subclasses of the superclass can access those variables/methods. So the vars with "protected" in front of them are "private" to all other classes that declare instances of that class. Even if you declare instances of Point in the Point3D class you still can't access them. (i.e delta(Point p) { p.x -- can't access x because it's "private") But "this.x" (in Point3D) is legal cause you inherited the x and y vars from Point.

So I hoped I cleared things up for you, a little()
If you need more clarification just ask.


Edited by - loserkid on September 1, 2000 10:27:31 PM

Share this post


Link to post
Share on other sites
OK a function must be a MEMBER function to even have a this pointer. So here''s something to look at:

YOU WROTE:
public void delta(Point p) {
p.x += this.x; // compile-time error: cannot access p.x
p.y += this.y; // compile-time error: cannot access p.y
}

DID YOU MEAN THIS:
public void Point3D::delta(Point p) {
p.x += this.x;
p.y += this.y;
}

and notice also that the code above creates a copy of the parameter Point P, and therefore the changes in line
p.x += this.x;
will NOT affect the original object. It seems like you logic is a little backwards, or you need to use a reference parameter like this:

public void Point3D::delta(Point &p)

but actually I am thinking in C++ ... so Java or whatever you are using may be different.

hope that was at all helpfull.

Share this post


Link to post
Share on other sites
I think i got those examples from 6.6.7 of the java api doc''s

I understand it now
protected is same as friendly inside the package
but outside the package..
the protected members can only be access by objects of that type
that''s why you have to cast it to Point3d, and can''t access a Point.x (p.x) I think...

I looked on sun.com''s docs and found another "thing" about protected modifier..and it explained it..but thanks!

Share this post


Link to post
Share on other sites
Java exceptions are used for terminating such as disconnecting connections and stuff???
so i shouldn''t use exceptions for stuff like displaying a message to tell a user to put anohter number in if the user put in illegal input?

Share this post


Link to post
Share on other sites
quote:
Original post by CodyVa

Java exceptions are used for terminating such as disconnecting connections and stuff???
so i shouldn''t use exceptions for stuff like displaying a message to tell a user to put anohter number in if the user put in illegal input?


I do that all the time. When i need a number input, i try to parse a number from the text box, I catch a numberformat exception and just print a dialog saying "numbers only", then return from the current method.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!