Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

Hmmm....I apparently have the wrong idea about extending classes

Thread view: 
Noodles Jefferson - 16 Jan 2006 02:47 GMT
I was under the impression that you extended classes so that you could
have the same variables as the class you're extending from.

But when I try this:

public class A {

 private String x = "Hi";
 private String y = " there!";

}

public class B extends A {

 public void printIt() {

 System.out.print(x + y);

 }

}

public class C {

  public static void main(String[] args) {
 
      C drive = new C();
      drive.runIt();
 
  }
 
  public void runIt() {
 
      B b = new B();
      b.printIt();
 
  }

}

I get this:

C:\Documents and Settings\HP_Owner\My Documents\JunkDrawer\JavaPrograms
\javaExperiments\ExtendingExample\B.java:6: x has private access in A
 System.out.print(x + y);
                  ^
C:\Documents and Settings\HP_Owner\My Documents\JunkDrawer\JavaPrograms
\javaExperiments\ExtendingExample\B.java:6: y has private access in A
 System.out.print(x + y);
                      ^
2 errors
Process javac exited with code 1

So my thinking was wrong but now I'm kind of like WTF. Why is it saying
in A instead of B? Isn't the purpose in extending so that you can use
the variables in the subclass?

Signature

Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "Informer" -- Snow

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.

SMC - 16 Jan 2006 03:11 GMT
> I was under the impression that you extended classes so that you could
> have the same variables as the class you're extending from.
[quoted text clipped - 7 lines]
>
> }

"protected" is what you want.

Signature

Sean

Be careful about reading health books. You may die of a misprint. --Mark
Twain

Noodles Jefferson - 16 Jan 2006 03:21 GMT
> > I was under the impression that you extended classes so that you could
> > have the same variables as the class you're extending from.
[quoted text clipped - 9 lines]
>
> "protected" is what you want.

Oh duh. Thanks.

Signature

Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "Informer" -- Snow

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.

iamfractal@hotmail.com - 16 Jan 2006 08:40 GMT
SMC skrev:

Snip.

> "protected" is what you want.

Don't use, "protected," unless you can't use the default access
modifer.

.ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition.
opalpa@gmail.com opalinski from opalpaweb - 16 Jan 2006 13:00 GMT
What are you talking about?  protected and the default access modifier
-- package private, have different meaning.  I don't understand your
advice.

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Paulus de Boska - 16 Jan 2006 13:49 GMT
public members are accessible from any method.
private members only from methods in the same class.
protected members are accessible from methods in extending classes,
subclasses (in addition to methods in the class itself)
package members, when there's no specifier written, are accessible from
methods in classes that are in the same package.

---
Paul Hamaker, SEMM
http://javalessons.com
Torkel Franzen - 16 Jan 2006 15:06 GMT
> protected members are accessible from methods in extending classes,

 Protected members are freely accessible in all classes in the same
package, and are also inherited by extensions, and accessible with
certain restrictions in extensions in other packages.
opalpa@gmail.com opalinski from opalpaweb - 16 Jan 2006 15:30 GMT
Really?  Protected members are accessible by package classes that don't
extend?  I didn't know that.

Why in the world is that?

I'm gonna try this out.  It boggles me why that would be.

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
opalpa@gmail.com opalinski from opalpaweb - 16 Jan 2006 15:36 GMT
If I was making a language I would change that.  Thanks for the info.

One other aspect I would change is the ability to access inner class's
private members from outer classes.  Like, I think the following should
not be allowed:

package experiment;
class InnerClassPrivatesExposedBug {
 private static class Inner {
   private char datum;
 }
 InnerClassPrivatesExposedBug() {
   Inner inner;
   (inner = new Inner()).datum = 'F';
   System.out.println("inner's datum: " + inner.datum);
 }
 public static void main(String args[]) {
   new InnerClassPrivatesExposedBug();
 }
}

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Roedy Green - 16 Jan 2006 20:31 GMT
On 16 Jan 2006 07:30:50 -0800, "opalpa@gmail.com opalinski from
opalpaweb" <opalpa@gmail.com> wrote, quoted or indirectly quoted
someone who said :

>Really?  Protected members are accessible by package classes that don't
>extend?  I didn't know that.
>
>Why in the world is that?
>
>I'm gonna try this out.  It boggles me why that would be.

Protected is a bit more public that default.  The idea is if you
extend a package you know more what you are doing that if you merely
use it.

see http://mindprod.com/jgloss/scope.html

By the way, has Sun standardized on  a proper name for default scope?
Even if there is not a keyword, there still needs to be a name for it
in documentation on the scope. "default" is not a name; it is an
adjective.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

opalpa@gmail.com opalinski from opalpaweb - 16 Jan 2006 20:58 GMT
They call it "package private".

http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

This table shows things off pretty concisely:

Specifier     Class     Package     Subclass     World
private     Y     N         N         N
no specifier     Y     Y         N         N
protected     Y     Y         Y         N
public         Y     Y         Y         Y

This is what I thought was going on, and personally I want this:

Specifier     Class     Package     Subclass     World
private     Y     N         N         N
no specifier     Y     Y         N         N
protected     Y     N         Y         N
public         Y     Y         Y         Y

I would change protected line's package visibility.   I thought that's
what it was too.  For what... ten years I thought that is what it was.

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Oliver Wong - 16 Jan 2006 22:24 GMT
> By the way, has Sun standardized on  a proper name for default scope?
> Even if there is not a keyword, there still needs to be a name for it
> in documentation on the scope. "default" is not a name; it is an
> adjective.

   "default" could be both a name and an adjective, just like "public" and
"protected" and "private" are also both names and adjectives.

   - Oliver
iamfractal@hotmail.com - 16 Jan 2006 14:15 GMT
opalpa@gmail.com opalinski from opalpaweb skrev:

> What are you talking about?  protected and the default access modifier
> -- package private, have different meaning.  I don't understand your
[quoted text clipped - 3 lines]
> opalpa@gmail.com
> http://www.geocities.com/opalpaweb/

(After Paulus's excellent email, this is probably unnecessary, but as I
was asked ...)

All I meant was a designer should restrict access as much as possible.
So if Noodles was designing a base class to be extended by other
classes in other packages in the system, then he should mark the
methods as, "protected."

If, however, Noodles was designing a base class to be extended only by
other classes in the same package, then he should mark the methods with
the default access modifier.

.ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition.
opalpa@gmail.com opalinski from opalpaweb - 16 Jan 2006 15:04 GMT
Paulus provided a list of definitions.   Perhaps my question was not
clear because I know those definitions well (his email did not give me
more info).

I don't think of package private as being along a gradient with the
other access modifiers.

The situation you are describing iamfrac, is a little strange, say we
have a package X with class C in it, and consider the following two
cases:

1. C is a public class
2. C is a package private class

In case 2, classes outside of package X cannot extend C so protected
and package private have different meaning.  protected would be more
restrictive, allowing only subclass of C within X to use a method.

In case 1, we are making a public class and want to only allow
extending classes in X to access certain methods?  That does not make
sense to me.  There are a few different designs that come to mind, one
is to have a package private subclass in X that has those methods and
uses protected on them.

---

Using package private for protected in a class allows non-subclass in
package to call methods which they should be restricted from calling.

In short I disagree with your advice, finding it overly simplistic and
a potential root of problems.

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
opalpa@gmail.com opalinski from opalpaweb - 16 Jan 2006 15:52 GMT
Verified fact that package private is on gradient with other access
modifiers.  Learned something pretty big today.  Thanks for posting
your messages.

This new fact and ability to access private members of inner classes
are my least two favorite things about the Java language.  I think the
language is excellent, but I would change these things in a language I
would create.

My other peeves are:
* "!" reads poorly; i would prefer "not"
* import static is file scope and not class scope
* enhanced for syntax should use "in" instead of ":"
* 1 gig heap ceiling

All in all, not too bad, but still peeves.

Thanks

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Thomas Hawtin - 16 Jan 2006 17:18 GMT
> Verified fact that package private is on gradient with other access
> modifiers.  Learned something pretty big today.  Thanks for posting
> your messages.

JDK 1.0 had private protected which did what protected looks as if it
might do. It was removed in 1.1, and never really worked anyway
(apparently). I'm not keen on using protected or package private (except
types) in most cases. Certainly variables should be kept private.

> This new fact and ability to access private members of inner classes
> are my least two favorite things about the Java language.  I think the

Inner classes accessing privates of the outer classes is necessary. The
other way around seems completely unnecessary to me.

> language is excellent, but I would change these things in a language I
> would create.
>
> My other peeves are:
> * "!" reads poorly; i would prefer "not"

&& as and, and: or and-then and || as or, or: or or-else or something.
I'd ditch the likes of ~, &, |, ^, <<, %, / on integers, /=, etc.,
altogether.

> * import static is file scope and not class scope

You aren't suggesting putting more than one outer class in the same
file, are you? I suppose it'd work applicable to members and constructors.

> * enhanced for syntax should use "in" instead of ":"

I want to keep in for my InputStreams, Readers and similar! I certainly
don't want Microsoft-style grammar sensitive keyword hacks. They could
have added a foreach keyword, but it seems to work as it is.

> * 1 gig heap ceiling

Is there?

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

opalpa@gmail.com opalinski from opalpaweb - 16 Jan 2006 17:56 GMT
About 1% of the time I have multiple outer classes in same file.  The
better example is when I want "import static" to be in an inner class
only.

I believe it is possible that "import static", "lack of private
protected", and "inner class privates being exposed" might be a reality
of implementation.  In other words those are desireable aspects however
the additional effort needed to carry them out put them outside of
feasible.  I don't know.  I got my fingers crossed for the future.
Maybe sun will put a copy of compiler into open source one day and I
can pay someone to make these mods -- or maybe do it myself.  Don't
know.

I don't know how grammar sensitive keyword hacks cause problems.  I'll
look into this.  Even this one small change?

Interesting blog, btw.

and let me get more info on the 1 gig heap thing.  Last I read about it
that is what I understood.  Although personally I seem to approach the
boundry I've yet to need to breach it and am completely knowledable
about it.

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Thomas Hawtin - 16 Jan 2006 18:33 GMT
> I believe it is possible that "import static", "lack of private
> protected", and "inner class privates being exposed" might be a reality
[quoted text clipped - 4 lines]
> can pay someone to make these mods -- or maybe do it myself.  Don't
> know.

Isn't the Eclipse JDT compiler open source? But if you did add a random
feature to it, it'd no longer be Java.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Roedy Green - 16 Jan 2006 20:34 GMT
On Mon, 16 Jan 2006 17:31:04 +0000, Thomas Hawtin
<usenet@tackline.plus.com> wrote, quoted or indirectly quoted someone
who said :

>&& as and, and: or and-then and || as or, or: or or-else or something.
>I'd ditch the likes of ~, &, |, ^, <<, %, / on integers, /=, etc.,
>altogether.

If you do that, much of the class library could not be written in
Java, e.g .fiddling byte sex. dealing with unsigned bytes.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Oliver Wong - 16 Jan 2006 22:29 GMT
>> This new fact and ability to access private members of inner classes
>> are my least two favorite things about the Java language.  I think the
>
> Inner classes accessing privates of the outer classes is necessary. The
> other way around seems completely unnecessary to me.

   Isn't inner-classes-accessing-privates-of-the-outer-classes emulated by
the compiler creating virtual protected accessors for those proviate fields
anyway? That's what some of Eclipse's warnings would seem to have you
believe.

   - Oliver
Noodles Jefferson - 16 Jan 2006 22:49 GMT
In article <1137400803.935472.234170@f14g2000cwb.googlegroups.com>,
iamfractal@hotmail.com took the hamburger, threw it on the grill, and I
said "Oh wow"...

> SMC skrev:
>
[quoted text clipped - 4 lines]
> Don't use, "protected," unless you can't use the default access
> modifer.

How are you supposed to extend classes without it? Surely you don't want
your variable public.

Signature

Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "Informer" -- Snow

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.

iamfractal@hotmail.com - 17 Jan 2006 09:34 GMT
Noodles Jefferson skrev:

> In article <1137400803.935472.234170@f14g2000cwb.googlegroups.com>,
> iamfractal@hotmail.com took the hamburger, threw it on the grill, and I
[quoted text clipped - 15 lines]
> Noodles Jefferson
> mhm31x9 Smeeter#29 WSD#30

Note that, "protected," does not exclusively denote that a method can
be overridden by a subclass.

The default access modifier also denotes that a method can be
overridden by a subclass.

(So does, "public.")

The following is perfectly acceptable (untested).

class A {

  void sayCheese() {
     system.out.println("A says cheese");
  }
}

class B {

  void sayCheese() {
     system.out.println("B says cheese");
  }
}

.ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition.
iamfractal@hotmail.com - 17 Jan 2006 09:37 GMT
Ooops, that class B should have extended class A ..

class B extends A {

  void sayCheese() {
     system.out.println("B says cheese");
  }

}

.ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition.
Monique Y. Mudama - 17 Jan 2006 17:46 GMT
> SMC skrev:
>
[quoted text clipped - 4 lines]
> Don't use, "protected," unless you can't use the default access
> modifer.

If you use the default access modifier, the only way for subclasses to
access the variable/method/whatever is to be in the same package.  I
would say, be very careful about which modifiers you choose,
especially if you're publishing an API.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Paulus de Boska - 16 Jan 2006 11:00 GMT
To find out more about access specifiers you can look here :
http://javalessons.com/cgi-bin/fun/java-tutorials-main.cgi?ses=ao789
then lookup 33.Access under Java Fundamentals.
---
Paul Hamaker, SEMM
http://javalessons.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.