• Post Reply Bookmark Topic Watch Topic
  • New Topic

java private member  RSS feed

 
vipul hasija
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
acessing private member outside class gives an error..

but while implementing it in two ways
1.) using object of a class in other class
2.) using inheritence (in other function)

gives two different errors...why is it so..?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Two different errors, because there are two different situations. Do you have some example code, and what are exactly the error messages that you get? Do those error messages not sound logical to you; if so, then why not?
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you got any getXXX or setXXX methods?
If you are using inheritance, you should have all the manipulation of the private members in the superclass, so the subclass does not need access to them.
 
vipul hasija
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Roo
{
private String getText()
{
return "FULL";
}
}
1.class Poo extends Roo
{
public static void main(String args)
{
System.out.print(getText());
}
}
error: cannot find symbol
symbol : method getText()
2.class Foo
{
public static void main(String args)
{
Roo r= new Roo();
System.out.print(r.getText());
}
}
error:has private access in Roo
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error "cannot find symbol" in the first example means that Java doesn't know what "getText" is. Private methods exist only inside the class that they are defined - so in this case the getText method only exists in class Roo. When you try to use it in class Poo, Java doesn't even know that it exists at that point.

The error "getText has private access in Roo" in the second example means that Java does know that the class Roo has a method called getText, but you cannot call it, because it is private. Note that in the second example you are directly using class Roo, unlike in the first example.

The second error message could have been applicable also to the first one, but the Java compiler looks at it a little differently. Anyway, the important thing is that you get an error message when you try to do something that is not correct according to the rules of the language. It is up to the implementation details of the compiler which exact error message you get. If you are studying for an exam, for example for the OCJP, then you don't need to know details like this - the exam is not going to ask you which exact error message you would get with examples like these.
 
vipul hasija
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:The error "cannot find symbol" in the first example means that Java doesn't know what "getText" is. Private methods exist only inside the class that they are defined - so in this case the getText method only exists in class Roo. When you try to use it in class Poo, Java doesn't even know that it exists at that point.

The error "getText has private access in Roo" in the second example means that Java does know that the class Roo has a method called getText, but you cannot call it, because it is private. Note that in the second example you are directly using class Roo, unlike in the first example.

The second error message could have been applicable also to the first one, but the Java compiler looks at it a little differently. Anyway, the important thing is that you get an error message when you try to do something that is not correct according to the rules of the language. It is up to the implementation details of the compiler which exact error message you get. If you are studying for an exam, for example for the OCJP, then you don't need to know details like this - the exam is not going to ask you which exact error message you would get with examples like these.


thanks......
 
Timothy Oldbean
Greenhorn
Posts: 17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Just testing how the [ code] [ /code] works. Take out the space before code and /code to make it work for you too!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!