This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hiding instance variables

 
Stephen Black
Ranch Hand
Posts: 52
1
Chrome MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 1 subclass and 1 superclass with which i am learning design patterns
Anyhow the snag ive run into is this
I have a variable of the same name in my super and sub classes

When I declare it like this String PizzaType="Cheese"; in my subclass the compiler
complains saying that I am hiding fields.

So I think of course you already know about the variable because it's declared in the superclass so I
will just like to change the value please Mr compiler so I put down PizzaType="Cheese"; without declaring it

And the compiler complains that it cant find symbol "PizzaType".
I even try super.PizzaType="Cheese";

I can't win! Ok I have just discovered that everything works well when you use the constructor to change the value
for example
public class PeperoniPizza extends Pizza
{
public PeperoniPizza()
{ PizzaType=" Peperoni"; }
}
But I would still like to know why what I was trying wouldn't work until I used the constructor

Thanks for help



 
Maneesh Godbole
Saloon Keeper
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on what I could gather from your post
1) Pizza already has PizzaType defined
2) It's access modifier is private.

Could you please confirm?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are misunderstanding inheritance. If the superclass has a type, eg "cheese", "pepperoni", then the subclasses all have that field too. You do not need to supply it twice. You supply all the public getXXX methods or whatever which the superclass needs to publish that field, and you use those methods in the subclass.
 
Stephen Black
Ranch Hand
Posts: 52
1
Chrome MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 Yes pizza type is defined in the supertype
2 NO It's access modifier was public

Maneesh Godbole wrote:Based on what I could gather from your post
1) Pizza already has PizzaType defined
2) It's access modifier is private.

Could you please confirm?
 
Maneesh Godbole
Saloon Keeper
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen Black wrote:
2 NO It's access modifier was public

In that case this doesnt make sense
Stephen Black wrote:And the compiler complains that it cant find symbol "PizzaType".
I even try super.PizzaType="Cheese";

Could you post your super as well as child class? (Just dont forget to UseCodeTags ;) )
 
Campbell Ritchie
Sheriff
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen Black wrote:1 Yes pizza type is defined in the supertype
2 NO It's access modifier was public . . .
That is a mistake. It should be private and accessed via public methods if that is necessary.
 
Junilu Lacar
Bartender
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen Black wrote:I have 1 subclass and 1 superclass with which i am learning design patterns

Out of curiosity, what pattern(s) are you learning through this exercise? The only thing I can make out is that you might be learning that inheritance breaks encapsulation.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have pizza type = "cheese", then I find it hard to see that a Pepperoni pizza object “IS‑A” pizza object.

Otherwise you have a PepperoniPizza class which happily has a type field "cheese".
 
Husam Alsulimani
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First point
PizzaType variable must be private for encapsulation and access it through public getter and setter methods
if you declare it public you violate encapsulation rules

second point
if you have a variable in super class you don't need to declare it again in the subclass because subclass already inherent this variable

I assume you are learning decorator pattern?
if this is the case you need more classes
The following code explain the concept

This interface or you can use abstract class

this is your super calss which define a normal pizza type implements the Pizzainterface or extend Abstract class if you goes with this choice

This is abstract class represent a base class for the topping exists


This class define one of the toppings available it must extends the decorator class and you need sub class for each topping this is the main idea behind the decorator pattern which is extends
behavior with out recompile

This is another type of topping


This is the client calss which use this code


Hope this helps
 
Stephen Black
Ranch Hand
Posts: 52
1
Chrome MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I get it now
I thought subclasses inherited everything from the superclass
but I did have some things marked private and I now know that any
private variables get donated to charity rather than get inherited
All is public and is working fine

But from now on I WILL use setter and getter methods to encapsulate properly
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic