• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

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

CodyVa

[java] newbie ques. Protected modifier?

10 posts in this topic

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.
0

Share this post


Link to post
Share on other sites
it depends..if the varible is in a method or before the main statement...etc...read up on scope or post the code
0

Share this post


Link to post
Share on other sites
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.

0

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.
0

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
0

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.

0

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!
0

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?
0

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.
0

Share this post


Link to post
Share on other sites
But What if i don''t want to exit the method? What do i do then?!
I want to getinput again!
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites