2

I am just starting to get into Object Oriented Programming in Java. I am curious about what the difference (if any) is between the 2 pieces of code below.

public class BuyAHouseInc
{

    // Instance variables
    private String firstName;
    private String surname;
    private String address;
    private int budget;

    // method to set the first name in the object
    public void setFirstName(String firstName)
    {
        this.firstName = firstName; // stores the first name
    }

    // method to retrieve the first name from the object
    public String getFirstName()
    {
        return firstName; // return value of first name to caller 
    }

    // method to set the surname in the object
    public void setSurname(String surname)
    {
        this.surname = surname; // stores the surname
    }

    // method to retrieve the surname from the object
    public String getSurname()
    {
        return surname; // return the value of surname to caller
    }

    // method to set the address in the object
    public void setAddress(String address)
    {
        this.address = address; // stores the address
    }

    // method to retrieve the address from the object
    public String getAddress()
    {
        return address; // return the value of address to caller
    }

    // method to set the budget in the object 
    public void setBudget(int budget) 
    {
        this.budget = budget; // store the budget
    }

    // method to retrieve the budget from the object
    public int getBudget()
    {
        return budget; // return the value of address to caller
    }
}

This is the 2nd piece of code;

public class BuyAHouseInc
{    
    public void displayClient(String firstName, String surname, String address, int budget)
    {
        System.out.println("Client Name: " + firstName + " " + surname);
        System.out.println("Address: " + address);
        System.out.println("Budget: " + "€" + budget);
    }
}

I prefer the 2nd piece of code here because its clearer to understand but I have been reading a lot on methods and objects and I can't figure out what the actual differences are. Are set and get methods secure ways of entering values?

  • 2
    The second code has nothing to do with the first code... – OneCricketeer Feb 13 '16 at 17:31
  • You might want to read about [encapsulation](http://www.tutorialspoint.com/java/java_encapsulation.htm) to understand why you'd use the first code – OneCricketeer Feb 13 '16 at 17:33
  • Here is a good link about POJOs https://en.wikipedia.org/wiki/Plain_Old_Java_Object , and your displayClient method looks a toString method http://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java – chrislovecnm Feb 13 '16 at 17:40
  • Set and get methods use encapsulation, aka private variables can only be accessed from within the class. I'm aware of this, but when both codes are complied they run the same to me. –  Feb 13 '16 at 17:55
  • @eamonn-keogh If you are seeing the same behavior, you're running code that you have not shown here. The first code block does not print anything to standard out, but the second *only* prints to standard out. – munk Feb 13 '16 at 18:01
  • To my ear, what you're really asking is "why do I have to bother with object-oriented programming"? The answer is: you don't. Object-oriented programming is a very widespread programming paradigm that's super useful for a wide variety of applications. It also happens to be one that Java conforms to. My suggestion to you is patience, continue studying and hopefully you'll be a good developer some day. – Henrik Feb 13 '16 at 18:01
  • @Henrik Thats not the impression I'm trying to give off at all. I DO want to fully grasp OOP its only that I started studying Object oriented programming just 2 weeks ago. I'm still trying to wrap my head around it. –  Feb 13 '16 at 18:12

4 Answers4

2

Let's start with what you think is the simpler code:

public class BuyAHouseInc
{    
    public void displayClient(String firstName, String surname, String address, int budget)
    {
        System.out.println("Client Name: " + firstName + " " + surname);
        System.out.println("Address: " + address);
        System.out.println("Budget: " + "€" + budget);
    }
}

We could instantiate this class and use it like so:

public static void main(String[] args) {
    BuyAHouseInc buyAHouseInc = new BuyAHouseInc();
    buyAHouseInc.displayClient("jane", "doe", "123 main street", 100000);
}

The effect of this main method is to display the information on your screen. That's everything instances of this class can do. You can't share the information, or reuse it.

The first piece of code you show lets you create an object with fields that store data that you can reuse. The getters and setters are written so you can access those fields to use elsewhere in your program.

We can also add the displayClient method to this class, like so:

public class BuyAHouseInc {
    private String firstName;
    private String surname;
    private String address;
    private int budget;

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getFirstName() {
        return firstName;
    }
    ...
    public void displayClient() {
        System.out.println("Client Name: " + this.firstName + " " + this.surname);
        System.out.println("Address: " + this.address);
        System.out.println("Budget: " + "€" + this.budget);
    }
} 

So then I might be able to write a program like this:

public class Solution {
    public static void main(String[] args) {
        BuyAHouseInc jane = new BuyAHouseInc("jane", "doe", "123 main street", 100000);
        BuyAHouseInc john = new BuyAHouseInc("john", "doe", "123 main street", 50000);

        System.out.println("The following clients can afford to buy a house");

        if (canAffordTheHouse(jane)) {
            jane.displayClient();
        }
        if (canAffordTheHouse(john)) {
            john.displayClient();
        }
    }

    public static boolean canAffordTheHouse(BuyAHouseInc client) {
        return client.getBudget() > 50000;
    }
}
munk
  • 12,340
  • 8
  • 51
  • 71
0

If you're asking about getter / setter vs direct access, then there are many advantages of getter / setter over direct access. Basically:

  1. It's more secure as variable implementations should be kept private and non accessible by public sources.
  2. It allows for additional functionality in the get / set call and allowing different behavior for whom is getting / setting.
  3. It allows for different access levels (public, protected, etc.)
  4. It is, however, the exact same speed as accessing directly.

Here is another answer that shows what I said in more detail.

Community
  • 1
  • 1
Cole
  • 2,641
  • 1
  • 16
  • 34
  • Encapsulation by private fields makes the code easier to reason about, but does not provide security in the usual sense. Also, the behavior of a method isn't determined by the caller (#2). – munk Feb 13 '16 at 17:59
0

You could combine the blocks of code

public class BuyAHouseInc
{    

    // Instance variables
    private String firstName;
    private String surname;
    private String address;
    private int budget;

    public void displayClient()
    {
        System.out.println("Client Name: " + this.firstName + " " + this.surname);
        System.out.println("Address: " + this.address);
        System.out.println("Budget: " + "€" + this.budget);
    }

    // method to set the first name in the object
    public void setFirstName(String firstName)
    {
        this.firstName = firstName; // stores the first name
    }

    // method to retrieve the first name from the object
    public String getFirstName()
    {
        return firstName; // return value of first name to caller 
    }

    // method to set the surname in the object
    public void setSurname(String surname)
    {
        this.surname = surname; // stores the surname
    }

    // method to retrieve the surname from the object
    public String getSurname()
    {
        return surname; // return the value of surname to caller
    }

    // method to set the address in the object
    public void setAddress(String address)
    {
        this.address = address; // stores the address
    }

    // method to retrieve the address from the object
    public String getAddress()
    {
        return address; // return the value of address to caller
    }

    // method to set the budget in the object 
    public void setBudget(int budget) 
    {
        this.budget = budget; // store the budget
    }

    // method to retrieve the budget from the object
    public int getBudget()
    {
        return budget; // return the value of address to caller
    }

}

Alternatively, you could pass a whole object of BuyAHouseInc into the display function.

public void displayClient(BuyAHouseInc b)
{
    System.out.println("Client Name: " + b.getFirstName()+ " " + b.getSurname());
    System.out.println("Address: " + b.getAddress());
    System.out.println("Budget: " + "€" + b.getBudget());
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0
public void displayClient(String firstName, String surname, String address, int budget) 
{ 
//........ 
} 

is simply another method. Enclosed in { and } defines what it does when a call to displayClient() method is called. displayClient() requires 3 arguments before it can perform it's task. The arguments are what's inside the () in public void displayClient(String firstName, String surname, String address, int budget). The 2nd piece of code can be put within the public class BuyAHouse block or { }. Your setters() and getters() are also similar to displayClient() but has fewer arguments.

What's inside { } of public class BuyAHouse are members or methods. These methods has access to the class variables

    private String firstName;
    private String surname;
    private String address;
    private int budget;

That's why on most of the syntax of setters(), you can see that it's setting/assigning/storing (whatever you like) values to your class variables. So basically set() methods are used to modify the value of the variables firstname, surname,address and budget

getters() are used to return the value of the variables.

For instance,

String name; //this has no string value yet

//function definition - you tell what you want this method to do
public void setMyName(String yourName){
    name = yourName; //you store the value of yourName to name
}

    //method call
    setMyName("whatever name you like"); // whatever name you like will be passed to the yourName variable
heisenberg
  • 1,784
  • 4
  • 33
  • 62