Question

In: Computer Science

In my object java class the question is that it is possible to have direct inputs...

In my object java class the question is that it is possible to have direct inputs and outputs within the setters and getters, it is highly discouraged to achieve full portability of the code. Explain why.

Solutions

Expert Solution

In the above question, it is correctly said that it is possible to have direct Access of input and output values within the setter and getter methods but when it comes to achieve full portability of the code if get highly discouraged to use setter and getter methods. This simply means that setter and getter methods in java class fail to achieve full portability of the code.

First of all let's discuss what actually setter and getter methods are:

one of the four fundamental concepts in Java is encapsulation, which is the mechanism of trapping the data members and methods together as a single unit. In this concept, the variable of a class will be hidden from other classes and can be accessed only through the methods of their current class. Therefore it is also known as data Hiding.

To achieve encapsulation in Java:

  • We can declare the variable of a class as private.
  • Provide a public setter and getter method to modify and view the variable value.

So in Java getter and setter methods to conventional methods that are used for updating and retrieving the value of an instance variable of a class.

Instance variables are the private variable of a class so to access them we provide setters and getter methods to read and write their value. The variable which provided this method will be completely hidden from the outside class.

  • The method that is used to modify the value of a private instance variable of a class is known as a setter method.
  • The method that is used to retrieve the value of a private instance variable is known as a getter method.

These methods are tend to build that allow different access levels. For example the get may be public but the set could be protected. They are actually implemented to hide the internal representation of the code while exposing the code using an alternative representation. But it allows the inheritors to change the code of how a Method can behave and it is exposed by overriding the getter and setter method from which we can express the instance variable. With the help of setter and getter methods, programmers can control over the important variables that are accessed and updated in a correct manner such that changing value of an instance variable, for specified its range.

On the other hand, the getter method is the only way for the outside world to read the particular variable value. They are used when integrating your code with some Framework-like hybrid, spring, etc. Basically internal details of a class must not be visible outside the world so we use this method but it is not necessary that every field in your class should have a heat method.

But when it comes to achieving full portability of code by setter and getter method get failed to achieve it.

First of all what is portability?

Portability of a code is a characteristic in which code is not tightly coupled to one specific platform which is coupled as early as possible to support platform specific API. While using setter and getter methods in a particular code that is going to the portable to another platform, you can't achieve full portability because setter and getter methods tend to hide internal state and require all interaction to be performed by these methods. So, third party programmers can't access internal code; they can only access that variable by these methods.

One of the disadvantages of the setter and getter methods is they provide external access to implementation detail. This is to protect complexity from the client and it is a very responsible thing to shield the source code of a program. An object method implements the behaviour and by access to these attribute, direct or indirect via getter and setter methods then the client must become aware about the internal structure of the code. So, we can say that the client is exposed to the complexity of the class. That's why an experienced object-oriented designer could probably eliminate 99% of the use of getter and setter methods in Java code without much difficulty.

In this way, We can directly access input and output data within these methods but can't achieve full portability of the particular code which is having a number of getter and setter methods.


Related Solutions

This is a question for my biochemistry class. I have copied and pasted it below. This...
This is a question for my biochemistry class. I have copied and pasted it below. This is all that was given to me. Diagram the flow of genetic material in a cell.
I have the following code for my java class assignment but i am having an issue...
I have the following code for my java class assignment but i am having an issue with this error i keep getting. On the following lines: return new Circle(color, radius); return new Rectangle(color, length, width); I am getting the following error for each line: "non-static variable this cannot be referenced from a static context" Here is the code I have: /* * ShapeDemo - simple inheritance hierarchy and dynamic binding. * * The Shape class must be compiled before the...
object oriented programming java Create a Student class that have two data members id (assign to...
object oriented programming java Create a Student class that have two data members id (assign to your ID) and name (assign to your name). Create the object of the Student class by new keyword and printing the objects value. You may name your object as Student1. The output should be like this: 20170500 Asma Zubaida
This is for my Advanced Java Programming class. The book we use is Murach's Java Servlet's...
This is for my Advanced Java Programming class. The book we use is Murach's Java Servlet's and JSP 3rd Edition. I need help modifying some code. I will post the code I was told to open that needs to be modified below. In this exercise, you'll enhance the Future Value application to store the amount and interest rate in the user's session. That way, the user can experiment with different numbers of years to see the value of his or...
Java: modify the given example code to make it possible to create a student object by...
Java: modify the given example code to make it possible to create a student object by only specifying the name, all other info may be absent. it may also be possible to add a tag with an absent value. use OPTIONAL TYPE and NULL object design pattern.   import java.util.HashMap; import java.util.Map; public class Student { private final String aName; private String aGender; private int aAge; private Country aCountry; private Map aTags = new HashMap<>(); public Student(String pName) { aName =...
Java: modify the given example code to make it possible to create a student object by...
Java: modify the given example code to make it possible to create a student object by only specifying the name, all other info may be absent. it may also be possible to add a tag with an absent value. import java.util.HashMap; import java.util.Map; public class Student { private final String aName; private String aGender; private int aAge; private Country aCountry; private Map<String, String> aTags = new HashMap<>(); public Song(String pName) { aName = pName; } public String getName() { return...
So this is my assignment. My question is, what makes an object a good insulator? Does...
So this is my assignment. My question is, what makes an object a good insulator? Does having a high specific heat alone make a good insulator? Water has a very high specific heat, would it make a good insulator for the material? Please list off a bunch of common household items with why they are good insulators! Objective: Construct an insulated device that is designed to retain heat. Heated water will be put into a beaker and the beaker will...
I have a quick question about this C ++ problem in my intro class. I have...
I have a quick question about this C ++ problem in my intro class. I have been able to get the first answer consistently when I test it, but my second seems to either be high or low no matter how I change it. Can you show me how you would do this problem with the setprecision(2) Paula and Danny want to plant evergreen trees along the back side of their yard. They do not want to have an excessive...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static void main(String[] args) { String bottle1_label="Milk"; float bottle1_volume=250; float bottle1_capacity=500; bottle1_volume=addVolume(bottle1_label, bottle1_volume,bottle1_capacity,200); System.out.println("bottle label: " + bottle1_label + ", volume: " + bottle1_volume + ", capacity: " +bottle1_capacity); String bottle2_label="Water"; float bottle2_volume=100; float bottle2_capacity=250; bottle2_volume=addVolume(bottle2_label, bottle2_volume,bottle2_capacity,500); System.out.println("bottle label: " + bottle2_label + ", volume: " + bottle2_volume + ", capacity: " +bottle2_capacity); } public static float addVolume(String label, float bottleVolume, float capacity, float addVolume)...
For My Programming Lab - Java: Write a Temperature class that will hold a temperature in...
For My Programming Lab - Java: Write a Temperature class that will hold a temperature in Fahrenheit and provide methods to get the temperature in Fahrenheit, Celsius, and Kelvin. The class should have the following field: • ftemp: a double that holds a Fahrenheit temperature. The class should have the following methods : • Constructor : The constructor accepts a Fahrenheit temperature (as a double ) and stores it in the ftemp field. • setFahrenheit: The set Fahrenheit method accepts...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT