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

Superclass encapsulation

 
Stephen Black
Ranch Hand
Posts: 52
1
Chrome MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the superclass I have
Instance variables set to private CHECK
Setter methods as needed CHECK
Setter methods marked as public CHECK

But when I try to use the setter in the subclass
I get an error saying that I have an invalid method declaration

Here is the supertype code which I have abbreviated


And here is the subtype on which I am trying to set the inherited instance variables to something useful


I know that if I just set the variables to public I can access them directly but apparently that is bad encapsulation
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code you have in your NYStyleCheesePizza class needs to be inside a method (or a constructor). The only things which can be outside methods (or constructors) are declarations, so the compiler is trying to interpret that code as declarations. And it fails (naturally, because they aren't declarations) and so it gives you error messages.
 
Stephen Black
Ranch Hand
Posts: 52
1
Chrome MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok only instance variables and methods and constructors (if needed) go into a class
Note to self "Make sure you only put instance variables methods and constructors in a class "
Got it Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic