Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private Modifer

 
Deon Smith
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question I have of the code below is, why is Alpha a not of type Alpha but of type Beta?

Objects of type Alpha can inspect or modify the iamprivate variable and can invoke privateMethod, but objects of other types cannot. For example, the Beta class defined here:

[ Message edited to preserve formatting of code using the [code] and [/code] UBB Tags. -ds ]
[ July 21, 2002: Message edited by: Dirk Schreckmann ]
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object "a" is an instance of class Alpha. Therefore, no instance of class Beta is able to access a private member of class Alpha.
If you want to access a private member of class Alpha, then you will have to do it from an instance of class Alpha.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Deon Smith:
The question I have of the code below is, why is Alpha a not of type Alpha but of type Beta?

That is not the case. a is of type Alpha. Why did you believe that a were of type Beta?
I'd recommend that you carefully reread the lesson from Sun's Java Tutorial where you got this example (Controlling Access to Members of a Class).
[ July 21, 2002: Message edited by: Dirk Schreckmann ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic