This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Superclass encapsulation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Superclass encapsulation" Watch "Superclass encapsulation" New topic
Author

Superclass encapsulation

Stephen Black
Ranch Hand

Joined: Aug 13, 2009
Posts: 50

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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Aug 13, 2009
Posts: 50

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Superclass encapsulation
 
Similar Threads
Doubt in Design Pattern "Factory"
Pizza crust and tomato sauce recipe
Private member variable inherited??
Hiding instance variables
Head First Design Patterns - Factory Method Question (?)