What does this mean | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What does this mean

public class Box{ private Object object; public void set(Object object){ this.object = object; } public Object get(){ return object; }

28th Mar 2020, 9:09 PM
Kostandin Vllahu
2 Answers
+ 4
That you have a Box class with a property of type Object (called object) that you can set as you like by using the set() method and you can read its value by using the get() method. Why use getters and setters? Because your property is private, means that you can't access its value in any way if not by using getters and setters. So, why don't set it to public? You may want to prevent that variable for being misused in other parts of the code, it's called "information hiding" and it's one of the two main advantages of OOP (Object Oriented Programming), the other is inheritance.
28th Mar 2020, 10:11 PM
Maz
Maz - avatar
+ 3
Hello Kostandin Vllahu Are you familiar with classes and objects? And also with setter and getter? You have a class Box. It contains one private variable from type Object. Object is the super class of all classes. Because it is private you need setter and getter. With the setter you can defines/changes the object. And the getter returns this object.
28th Mar 2020, 10:02 PM
Denise Roßberg
Denise Roßberg - avatar