Please join us at the new JavaWorld Q&A Forums. Your existing login will work there. The discussions here are now read-only.


JavaWorld Talkback >> 958484

Pages: 1
greid
stranger


Reged: 03/30/07
Posts: 1
procedural versus object oriented thinking
      #45115 - 03/30/07 04:39 AM

The example "If X was an int, but now must be a long" causing compile errors is a bit contrived. It would cause a problem because it is a primitive value rather than an object. If X was an Account, and the Account implementation changed that might not be a problem at all due to encapsulation of Account. Also the reference to getters and setters implying procedural thinking is also a bit contrived. They "can" be used procedurally (see below), however use of getters and setters do not "imply" procedural thinking. In a situation where you are trying to "convert" a procedural thinker to an object-oriented thinker you should rightly watch out for examples of procedural thinking - but a getter/setter can be used without breaking encapsulation rules. Take the example where we have a getAge() method that returns an int. The object that owns the method might have a "birth date" attribute and calculate the age when invoked, so use of a getter or setter does not necessarily mean that you are accessing or updating an attribute directly and breaking encapsulation. Public attributes definitely break encapsulation as you are exposing the implementation. Getters and setters can also hide other information such as persistence (e.g. distributed objects, caching etc.), behavior delegated to another object and so on. Public attributes supply none of this behavior of course. In summary, the thing to avoid is using an object as a simple state container, externalizing behavior related to that state (procedural thinking), and using getters/setters to read or modify the state.

Post Extras: Print Post   Remind Me!   Notify Moderator  
MartinHunter
stranger


Reged: 01/09/08
Posts: 1
Re: procedural versus object oriented thinking [Re: greid]
      #82837 - 01/09/08 07:07 PM

Hi, an interesting point. I'm not sure whether your example of getAge() in the above post is actually an accessor method - it might have "get" in the method name (therefore adheres to the "getter" naming convention), but it actually contains real business logic.

By contrast, I think an "accessor method" IS about simple access to privately declared local variables.

I posted something in 2006 as well regarding the accessor issue - http://www.martinhunter.co.nz/articles/OnTheUseAndMisuseOfAccessorMethodsInOO.pdf - explains more detail than I can here.

Thanks, Martin.


Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
0 registered and 1 anonymous users are browsing this forum.

Moderator:   

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is disabled
      UBBCode is enabled

Rating:
Topic views: 9168

Rate this topic

Jump to

Contact us JavaWorld

Powered by UBB.threads™ 6.5.5