my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Hiding instance variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hiding instance variables" Watch "Hiding instance variables" New topic
Author

Hiding instance variables

Stephen Black
Ranch Hand

Joined: Aug 13, 2009
Posts: 50

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

Joined: Jul 26, 2007
Posts: 10426
    
    8

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?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
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

Joined: Aug 13, 2009
Posts: 50

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

Joined: Jul 26, 2007
Posts: 10426
    
    8

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

Joined: Oct 13, 2005
Posts: 39084
    
  23
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

Joined: Feb 26, 2001
Posts: 4474
    
    6

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
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

Joined: Apr 08, 2013
Posts: 3
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

Joined: Aug 13, 2009
Posts: 50

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
 
GeeCON Prague 2014
 
subject: Hiding instance variables