0

Accessing attributes from an object or getters and setters

I've written a basic programme which creates a few objects in class B of a class A...here it's suggested to use private access for attributes and access them using getters and setters from outside the class which I am doing but is accessing and sending and attribute value from the created object to another class C considered the wrong way...should I use a getAttribute method or just stick with my using obj.attribute or is either fine.

8th May 2018, 10:37 PM
Steven Moore
Steven Moore - avatar
4 Réponses
+ 3
I am a little confused by your question. Can you post a link to the code or make it public?
8th May 2018, 10:45 PM
cyk
cyk - avatar
+ 1
sorry I'm on my phone but the codes on my PC...but I've created 2 classes...and my main class with main method...created say 2 objects obj1 and obj2 of class 1 inside my main method..then accessed an attribute set in class1 e.g name (which is set when I create the objects)so obj1.name and sent that value to class2 to be output using println (in a nutshell) so I'm accessing the attribute name of class1 via the main class and sending it to class2 rather than using a getMethod and setting the attribute to private as recommended...so I'm leaving the attribute public...the attributes don't hold anything sensitive but it might be something I don't want set to public so should I avoid accessing the attribute via the object I create and make it private and use a getMethod. I can post the code tomorrow if it still doesn't sound clear. thanks
8th May 2018, 11:24 PM
Steven Moore
Steven Moore - avatar
+ 1
Oh! Yes. Private is always recommended in Object Oriented Programming. This has to do with the principle of encapsulation that makes access to the variables inside a class "controlled"... Basically, the idea is that variables inside a class not be directly accessible from other classes so we declare them private and control their access through the public getters and setters. You can read more about encapsulation online but yeah, making the variables private is the way to go in OOP
8th May 2018, 11:30 PM
cyk
cyk - avatar
0
thanks, I think I understand about private access etc and I will do that when possible/required but a good (example) of my code would be this public static void main(String[ ] args) { Vehicle v1 = new Vehicle(); Vehicle v2 = new Vehicle(); v1.createVehicle("Red","Honda"); v2.createVehicle("Black","Ford"); } so I'm creating various instances all with different info and my equivalent need would be to send the value of v1.colour or other attributes to another class/method to be used in output....so I would have something like. myOtherMethod(v1.colour); rather than myOtherMethod(v1.getColour()) also I have set my attributes on object creation like v1.createVehicle(colour,speed etc) which sets the attributes rather than v1.setColour(),setSpeed() etc sorry again and hope thats clearer...it may be that I'm not using the best techniques to suit my needs but I only started a few days back with some basic php background.
9th May 2018, 12:04 AM
Steven Moore
Steven Moore - avatar