Jump to content

  • Log In with Google      Sign In   
  • Create Account


[java] newbie ques. Protected modifier?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 CodyVa   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 September 2000 - 09:34 AM

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.

Sponsor:

#2 ARCHIGAMER   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 September 2000 - 11:29 AM

it depends..if the varible is in a method or before the main statement...etc...read up on scope or post the code

#3 CodyVa   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 September 2000 - 02:32 PM

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.



#4 CodyVa   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 September 2000 - 02:59 PM

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.

#5 loserkid   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 September 2000 - 03:22 PM

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

#6 Xai   Crossbones+   -  Reputation: 1328

Like
Likes
Like

Posted 01 September 2000 - 03:46 PM

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.



#7 CodyVa   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 September 2000 - 06:51 PM

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!

#8 CodyVa   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 September 2000 - 10:09 PM

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?

#9 Jim_Ross   Members   -  Reputation: 122

Like
Likes
Like

Posted 02 September 2000 - 05:57 AM

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.

#10 CodyVa   Members   -  Reputation: 122

Like
Likes
Like

Posted 06 September 2000 - 10:58 AM

But What if i don''t want to exit the method? What do i do then?!
I want to getinput again!

#11 TheEarl   Members   -  Reputation: 122

Like
Likes
Like

Posted 10 September 2000 - 08:19 PM

When u catch an exception, unless u do a System.exit() or a return, then the next statement after the exception block will be executed... for example:

public class Foo
{
public static void main(String[] args)
{
try
{
throw new RuntimeException("ARRGGHH");
}
catch(RuntimeException e)
{
}
System.out.println("No problems, heh, heh.");
}
}

Right, the above code throws a RuntimeException, the catch, catches it, does nothing and the code runs onto the System.out.println statement.

Hope this helps, if you want to try the example out, stick it in a file called Foo.java




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS