• Post Reply Bookmark Topic Watch Topic
  • New Topic

Private access problem  RSS feed

 
Mani Sudi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there

I'm having trouble with my class "Book" and it is the subclass of the LoanItem. Everytime that I try to compile the class I get this error message "BorrowNumber has private access in LoanItem". This is the method to that get this error:


This is my LoanItem class:



Can someone correct me and explain why I keep getting this error
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mani Sudi wrote:Hello there

Hello there.

First: please UseCodeTags (←click - I've added them for you this time).

I'm having trouble with my class "Book" and it is the subclass of the LoanItem. Everytime that I try to compile the class I get this error message "BorrowNumber has private access in LoanItem". This is the method to that get this error:

Actually, it isn't. It's a snippet of that method, which presumably is part of a subclass; otherwise, the 'super' reference is wrong.

Please supply the whole method (or, at the very least, its signature and context).

Winston
 
Mani Sudi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Mani Sudi wrote:Hello there

Hello there.

First: please UseCodeTags (←click - I've added them for you this time).

I'm having trouble with my class "Book" and it is the subclass of the LoanItem. Everytime that I try to compile the class I get this error message "BorrowNumber has private access in LoanItem". This is the method to that get this error:

Actually, it isn't. It's a snippet of that method, which presumably is part of a subclass; otherwise, the 'super' reference is wrong.

Please supply the whole method (or, at the very least, its signature and context).

Winston


This is the code for the whole book class:

 
Tim Cooke
Marshal
Posts: 4039
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's simplify things a little so we can discuss exactly what the problem is you are facing. As Winston says you're not being entirely clear with your code snippets but I think I get the gist. Are we talking about something like this?

The borrowNumber variable you are using in the Book class is not defined in the Book class so the compiler has a look in the parent class for it, finds a variable of that name but cannot access it due to its private accessor declaration. This is what the compiler is telling you.

There are a number of other items in your code that are up for review if you wish? But first let's see if this answers your specific question and makes sense to you?

Edit: You beat me to it with your reply. Please take Winston's advice and UseCodeTags (<- click this link for how to do it). The code is very hard to read without them so you may not get as much help as you might otherwise.
 
Mani Sudi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:Let's simplify things a little so we can discuss exactly what the problem is you are facing. As Winston says you're not being entirely clear with your code snippets but I think I get the gist. Are we talking about something like this?

The borrowNumber variable you are using in the Book class is not defined in the Book class so the compiler has a look in the parent class for it, finds a variable of that name but cannot access it due to its private accessor declaration. This is what the compiler is telling you.

There are a number of other items in your code that are up for review if you wish? But first let's see if this answers your specific question and makes sense to you?

Edit: You beat me to it with your reply. Please take Winston's advice and UseCodeTags (<- click this link for how to do it). The code is very hard to read without them so you may not get as much help as you might otherwise.


It makes some sense, so how do I make it public on my LoanItem Class?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It makes some sense, so how do I make it public on my LoanItem Class?

You don't make it public, you virtually never make variables public.
You might consider making it protected or if your sub class only needs read access to it you might decide to add a getBorrowNumber() method to return the value of borrowNumber.

BTW why does your setBorrowNumber() method ignore the value passed in and set borrowNumber to 0?
 
Mani Sudi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
It makes some sense, so how do I make it public on my LoanItem Class?

You don't make it public, you virtually never make variables public.
You might consider making it protected or if your sub class only needs read access to it you might decide to add a getBorrowNumber() method to return the value of borrowNumber.

BTW why does your setBorrowNumber() method ignore the value passed in and set borrowNumber to 0?


Because the BorrowNumber in the LoanItem class has to be set to 0.
 
Tim Cooke
Marshal
Posts: 4039
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well to answer your question, you just change the access modifier from private to public. But, and it's an important but, doing this isn't going to actually help you achieve any useful functionality. Let's see the revised code with the modifier changed and explore it:

This code will now compile.

However, what are we actually doing here? someMethod() in Book calls on to setBorrowNumber() in LoanItem passing in a member variable that belongs to LoanItem already. But LoanItem does nothing with it, just sets the borrowNumber variable to 0.

It looks like you've gone a bit too far too quickly and have ended up in a bit of a muddle. I suggest you take it back to basics and get each individual feature working and tested before moving on to the next. What is it exactly are you trying to achieve with this code? Are just playing around for learning? Or is this some sort of assignment? Either is fine, I'm just curious.
 
Tim Cooke
Marshal
Posts: 4039
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mani Sudi wrote:Because the BorrowNumber in the LoanItem class has to be set to 0.

You already have a method in there called resetBorrowNumber() that does this. By convention when I see a method called setBorrowNumber(int borrowNumber) I expect it to take the parameter I pass it and save that value in the object. Naming is an important aspect of writing good quality code so your variables and methods should have intention revealing names.
 
Mani Sudi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:Well to answer your question, you just change the access modifier from private to public. But, and it's an important but, doing this isn't going to actually help you achieve any useful functionality. Let's see the revised code with the modifier changed and explore it:

This code will now compile.

However, what are we actually doing here? someMethod() in Book calls on to setBorrowNumber() in LoanItem passing in a member variable that belongs to LoanItem already. But LoanItem does nothing with it, just sets the borrowNumber variable to 0.

It looks like you've gone a bit too far too quickly and have ended up in a bit of a muddle. I suggest you take it back to basics and get each individual feature working and tested before moving on to the next. What is it exactly are you trying to achieve with this code? Are just playing around for learning? Or is this some sort of assignment? Either is fine, I'm just curious.


Your right, I need to set the borrow number to a number and not 0. It's part of a assignment. You want to have a look at the assignment?
 
Mani Sudi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mani Sudi wrote:
Tim Cooke wrote:Well to answer your question, you just change the access modifier from private to public. But, and it's an important but, doing this isn't going to actually help you achieve any useful functionality. Let's see the revised code with the modifier changed and explore it:

This code will now compile.

However, what are we actually doing here? someMethod() in Book calls on to setBorrowNumber() in LoanItem passing in a member variable that belongs to LoanItem already. But LoanItem does nothing with it, just sets the borrowNumber variable to 0.

It looks like you've gone a bit too far too quickly and have ended up in a bit of a muddle. I suggest you take it back to basics and get each individual feature working and tested before moving on to the next. What is it exactly are you trying to achieve with this code? Are just playing around for learning? Or is this some sort of assignment? Either is fine, I'm just curious.


Your right, I need to set the borrow number to a number and not 0. It's part of a assignment. You want to have a look at the assignment?


I'm still get the same messages, it's not working for me.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:Well to answer your question, you just change the access modifier from private to public.

@Mani Sudi
Please don't do this. It's really bad practice to make your variables public. Do as I said in an earlier post, either make it protected or have a getXXX method.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mani Sudi wrote:I'm still get the same messages, it's not working for me.

ItDoesntWorkIsUseless (←click). We need to know exactly what you changed, and what the message was.

Winston
 
Mani Sudi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mani Sudi wrote:
Mani Sudi wrote:
Tim Cooke wrote:Well to answer your question, you just change the access modifier from private to public. But, and it's an important but, doing this isn't going to actually help you achieve any useful functionality. Let's see the revised code with the modifier changed and explore it:

This code will now compile.

However, what are we actually doing here? someMethod() in Book calls on to setBorrowNumber() in LoanItem passing in a member variable that belongs to LoanItem already. But LoanItem does nothing with it, just sets the borrowNumber variable to 0.

It looks like you've gone a bit too far too quickly and have ended up in a bit of a muddle. I suggest you take it back to basics and get each individual feature working and tested before moving on to the next. What is it exactly are you trying to achieve with this code? Are just playing around for learning? Or is this some sort of assignment? Either is fine, I'm just curious.


Your right, I need to set the borrow number to a number and not 0. It's part of a assignment. You want to have a look at the assignment?


I'm still get the same messages, it's not working for me.


I done it, thank you for your help.
 
Tim Cooke
Marshal
Posts: 4039
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mani Sudi wrote:You want to have a look at the assignment?

No, not especially. Was just curious is all. The Ranch has a couple of policies with regards to homework assignments you might want to be aware of. Firstly: DoYourOwnHomeWork (<- click). Secondly: We will gladly help you with your homework but you must first ShowSomeEffort (<- click). In this case you have indeed shown some effort, and for that you have got some help. Easy peasy!

My advice for steady progress is to take small measured steps and make sure you've got each part right before moving on to the next. The beauty of this technique is that when you encounter a problem it's most likely caused by something you've done in the last 10 mins which is much easier to manage than trying to remember what you did yesterday.

I would also advise that you learn to write Unit Tests for every step you take. JUnit 4 is the standard tool for Unit Testing Java code. I'm sure your school teacher will gladly help you with getting started with JUnit, but if not then there are endless tutorials to be found online. The Ranch has a Testing FAQ but the beginner articles are a little dated now so the net is probably your best bet. If you have any questions on testing then drop by the Testing Forum.
 
Tim Cooke
Marshal
Posts: 4039
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mani Sudi wrote:I done it, thank you for your help.

You're very welcome.

I would strongly recommend you take on board Tony's advice with respect to making the class member public. This is considered bad practice as you are exposing an implementation detail of your class to the world. you should be exposing just the behaviour.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!