[java] newbie ques. Protected modifier?

Started by
9 comments, last by CodyVa 23 years, 7 months ago
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.
Advertisement
it depends..if the varible is in a method or before the main statement...etc...read up on scope or post the code
I wish there was a button on my monitor to turn up the intellegince. Theres a button called 'brightness' but it doesn't work
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.

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.
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
==============================================I feel like a kid in some kind of store... ============================================== www.thejpsystem.com
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.

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!
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?
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.
But What if i don''t want to exit the method? What do i do then?!
I want to getinput again!

This topic is closed to new replies.

Advertisement