• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Hiding instance variables

 
Ranch Hand
Posts: 52
1
MySQL Database Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 74031
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74031
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74031
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic