Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: How to make a Java class only visible inside the package & notvisible to other packages?

Reply
Thread Tools

Re: How to make a Java class only visible inside the package & notvisible to other packages?

 
 
Mark Space
Guest
Posts: n/a
 
      04-24-2008
www wrote:

> By trying different keywords,
>
> final class Person
> {


Yeah, what they said. Make a class package private by using no keyword
at all.

"class Person {"

is what you want.

> ...//code omitted
>
> public String getName(){...}
> }
>
> This solution has a problem, because I cannot extend Person anymore.


Get rid of the final.

>
> Another question is: since Person is declared as "final" and is only
> visible inside the package, then all the "public" methods inside Person
> is not "public" anymore, they equal to "protected" methods, correct?


Nope.

public interface People {
String getName();
}

public class PeopleProxy {
static public People getPeople() { return new Person(); }
}

class Person implements People {
String name;
public String getName() {return name;};
}


Now, Person is package private, but getName can be accessed through the
public interface People. I think. I didn't try it, but private
classes can be exported this way (Iterator is popular) so I think this
will work.
 
Reply With Quote
 
 
 
 
Mark Space
Guest
Posts: n/a
 
      04-26-2008
www wrote:

> oou all. I have taken Mark Space's idea and played on Eclipse. He
> is right.


Not too bad for just making stuff up. ^_^

_Learning Java_ by O'Reilly covers how this works in better detail than
I did. It also gives a practical example how to return a private class
of type Iterator so the user can use the public Iterator interface, but
the details of the implementation are still encapsulated.

I'd like you to consider reading this excellent book. It's been a big
help to me and I think it would benefit others as well. As I've been
helped on this list, I like to pay that help forward and try to assist
others learning also.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: How to make a Java class only visible inside the package & notvisible to other packages? Martin Java 0 04-24-2008 04:15 PM
How to make a module function visible only inside the module? beginner Python 6 08-19-2007 09:38 PM
How to make a hyperlink Visible or not visible in DataList Patrick Olurotimi Ige ASP .Net 7 06-15-2005 12:01 PM
Panel.Visible = True also make child controls visible. spamfurnace ASP .Net 1 05-24-2004 03:07 AM



Advertisments