• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about Encapsulation

 
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have a question about Encapsulation. I know the concept of the encapsulation as..

Anytime we have behavior in an application that you think is likely to change you want to move that behavior away from parts of the application that won't change
frequently.In other way we try to encapsulate it.

Now lets see example of it

I have a class Painter having a method paint()

Painter
--------------------------
paint()

but, paint() implemention may be varied depending upon object. Hence I want to encapsulate it.


let's say now I can no in this way

Painter
---------------------------------
setPaintStyle(PaintStyle pt);

Please check whether I did use encapsulation properly?

here is the code












and at a Last My main class is


 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Afraid I don't think so.

What you are doing is creating two fields with the same name (one in PaintStyle, one in Painter), so you can get confusion between the field in the suprclass and the field in the subclass.

Does Painter really extend PaintStyle: can you say a Painter "IS-A" PaintStyle? If no, then you shouldn't write "extends PaintStyle".
You have a field in the PaintStyle class of PaintStyle type; that doesn't look right. You should have some sort of other detail, eg String styleName in the PaintStyle classYou can put the names and styles into a Map, if you know how Maps work.

Then you want a PaintStyle field in the Painter class.

I haven't got the time to give a more complete answer at the moment.
 
Rahul Ba
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Campbell. I agree on whatever you said, but does it implies encapsulation?
 
Rahul Ba
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have a question about Encapsulation. I know the concept of the encapsulation as..

Anytime we have behavior in an application that you think is likely to change you want to move that behavior away from parts of the application that won't change
frequently.In other way we try to encapsulate it.

Now lets see example of it

I have a class Painter having a method paint()

Painter
--------------------------
paint()

but, paint() implemention may be varied depending upon object. Hence I want to encapsulate it.


let's say now I can no in this way

Painter
---------------------------------
setPaintStyle(PaintStyle pt);

Please check whether I did use encapsulation properly?

here is the code












and at a Last My main class is



Now I have updated painter class, which was previously extending PaintStyle class.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better, but still incomplete. I would suggest you set up your pt in a constructor, otherwise you are risking problems with null references.
I would suggest you have a paint() method in the PaintStyle class (maybe abstract) and use polymorphism.

Add a paint() method to the Painter class, like this:
 
Rahul Ba
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I would suggest you set up your pt in a constructor, otherwise you are risking problems with null references.



Hi, you said this statement....I am hearing this first statement and very enthu. to know how will it risk to null references?
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have an object of the Painter class, until the setPaintStyle method is called, the pt reference points to null. If you attempt to use it, you will have no information available, and may suffer an Exception.
If you set up pt in a constructor, it will only be null if somebody writes

new Painter(null)
 
reply
    Bookmark Topic Watch Topic
  • New Topic