Class Car:
Code
package com.company;
public class Car {
private int doors;
private int wheels;
private String model;
private String engine;
private String colour;
public void setModel(String model) {
String validModel = model.toLowerCase();
if(validModel.equals("carrera") || validModel.equals("commodore")) {
this.model = model;
} else {
this.model = "unknown";
}
}
public String getModel() {
return this.model;
}
}
Code
main:
package com.company;
public class Main {
public static void main(String[] args) {
Car porshe = new Car();
Car holden = new Car();
porshe.setModel("carrera");
System.out.println("Model is " + porshe.getModel());
}
}
My question is this, I thought methods couldn't access info from another method? Cuz of encapsulation?
but in these methods it does... for example:
this.model = model;
} else {
this.model = "unknown";
}
^part of method setModel
yet the method getModel, accesses model from getModel....:
public String getModel() {
return this.model;