V.2.0. - OOP in Java - Get Your Hands Dirty With Code
V.2.0. - OOP in Java - Get Your Hands Dirty With Code
(Don’t just copy and paste the codes, type the codes like there’s no
tomorrow)
You might prefer to call them Accessors and Mutators, but Getters and
Setters fits the Java naming convention.
One of the basic goals of object-oriented programming is to hide the
implementation details of a class inside from outside world.
A get accessor (also called a getter) is a method that retrieves a field value,
whereas a set accessor (setter) is a method that sets a field value. Instance
variable values, usually.
If the field is named “color”, for example, the getter and setter methods are
named “getColor” and “setColor”.
class Car {
String brand;
String color;
boolean sunRoof;
String getBrand() {
return brand;
}
String getColor() {
return color;
}
boolean hasSunRoof() {
return sunRoof;
}
String beeping() {
if (brand.equals("BMW")) {
return "Beep Beep...I am BMW!";
} else if (brand.equals("Mercedes Benz")) {
return "Beep Beep...I am Mercedes Benz!";
} else {
return "Beep Beep...I am Poor Brand!";
}
}
}
Here we are, just humming along without a care in the world leaving our data
out there for anyone to see and even touch i.e. we are exposing our data! We
are leaving our instance variables exposed!
In the hands of the wrong person, a reference variable (in here, “myCat”) is
quite a dangerous weapon. They can flat your cat like below:
As a general rule, you should avoid creating “public” fields. Instead, you can
make all your fields (instance variables) “private”. Then you can selectively
grant access to the data those fields contain by adding to the class special
methods called accessors.
Note: We will not talk about access modifiers (public, private, protected, default)
in details now.
class Car {
private String brand;
private String color;
private boolean sunRoof;
String beeping() {
if (brand.equals("BMW")) {
return "Beep Beep...I am BMW!";
} else if (brand.equals("Mercedes Benz")) {
return "Beep Beep...I am Mercedes Benz!";
} else {
return "Beep Beep...I am Poor Brand!";
}
}
}
Why on earth we are not just make brand, color and sunRoof as public
and skip the accessors?
If all your accessors do is set and return the values, without doing any data
validation or other processing, you may as well skip them. The point of making
your fields “private” is to carefully control access to them. If you’re going to use
accessors for all your fields, you might as well make the fields “public”. Instead,
carefully consider which fields really should be accessible to the outside world
(another class) — and provide accessors only for those fields that really need
them.
class CarRunner {
public static void main(String[] args) {
Car bmw = new Car();
bmw.setBrand("BMW");
bmw.setColor("White");
bmw.setSunRoof(true);
CODE Exercise
Steps to follow:
class Triangle {
float base;
float height;
float getArea() {
return (this.base * this.height) / 2;
}
B) Write code to calculate the area of a triangle which has base 3cm and height
4cm
class triangleRunner {
public static void main(String[] args) {
Triangle triangleOne = new Triangle();
triangleOne.setBase(3);
triangleOne.setHeight(4);
System.out.println(triangleOne.getArea());
}
}
If you can make the above code working please create below classes:
CODE Exercise
For example: Car: door, seat; Fruit: color, taste; Course: title, credit
CODE Exercise
Let’s return to the “User” class that we developed in the previous chapters. This is
the User class:
class User {
// your code goes here
}
(A) Create methods to set the “firstName” and “lastName” property value.
Remember to use the right access modifier.
(B) Now, create a method “getFullName” to return the “firstName” and “lastName”.
(C) Create a new user object with the name of “anUser”, set its name to ‘Jason
Bourne’ and make it return its name.
CODE Exercise
(C) Create a method with the name “ruffOrDie” which can return “Meow…Meow” if
its height more than 9. If not than return “Biday Pitibi”.
(D) Now, create a new cat object with the name “myCat” and make it ruff or let it
die as flat!