COS202 Lecture 7
COS202 Lecture 7
LECTURE 7
In the example above, we created a static method, which means that it can be accessed without
creating an object of the class, unlike a public method, which can only be accessed by objects.
Note: The dot (.) is used to access the object's attributes and methods.
Example 7.2
1 public class Car {
2
3 public void fullThrottle() {
4 System.out.println(“The car is going as fast as it can!”);
5 }
6
7 public void speed(int maxSpeed) {
8 System.out.println(“Max speed is: ” + maxSpeed + “km/h”);
9 }
10
11 public static void main(String[] args) {
12 Car myBenz = new Car();
13 myBenz.fullThrottle();
14 myBenz.speed(240);
15 }
16 }
Java Modifiers
In most examples of Java programs written in this course, you are quite familiar with the public
keyword that appears almost all the time. The public keyword is an access modifier, meaning
that it is used to set the access level for classes, attributes, and methods.
We divide modifiers into two groups:
● Access Modifiers - control the access level
● Non-Access Modifiers - do not control access level, but provide other functionality
Access Modifiers
Table 7.1: Access Modifiers for Classes, Attributes, and Methods
Modifier Description
For Classes
default The class is only accessible to classes in the same package. This is used when
you don't specify a modifier.
default The code is only accessible in the same package. This is used when you don't
specify a modifier.
protected The code is accessible in the same package and subclasses.
Non-Access Modifiers
Table 7.2: Non-Access Modifiers for Classes, Attributes, and Methods
Modifier Description
For Classes
abstract The class cannot be used to create objects (To access an abstract class, it must
be inherited from another class.
abstract It can only be used in an abstract class and can only be used on methods. The
method does not have a body, for example, abstract void run();. The body is
provided by the subclass (inherited from).
static Attributes and methods belong to the class, rather than an object
Example 7.3
1 public class Main {
2 final int x = 10;
3 final double PI = 3.14;
4
5 public static void main(String[] args) {
6 Main myObj = new Main();
7 myObj.x = 50;
8 myObj.PI = 25;
9 System.out.println(myObj.x);
10 }
11 }
The example above will generate an error because it cannot assign a value to a final variable.
Java Encapsulation
The meaning of Encapsulation is to make sure that "sensitive" data is hidden from users. To
achieve this, you must:
● Declare class variables/attributes as private
● Provide public get and set methods to access and update the value of a private variable
The private variables can only be accessed within the same class (an outside class has no access
to them). However, it is possible to access them if we provide public get and set methods. The
get method returns the variable value, and the set method sets the value.
The syntax for both is that they start with either get or set, followed by the name of the variable,
with the first letter in uppercase.
Example 7.4a
Person.java
1 public class Person {
2 private String name; // private = restricted access
3
4 public String getName() {
5 return name;
6 }
7
8 public void setName(String newName) {
9 this.name = newName;
10 }
11 }
The get method returns the value of the variable name. The set method takes a parameter
(newName) and assigns it to the name variable. The this keyword is used to refer to the current
object. However, as the name variable is declared as private, we cannot access it from outside
this class.
Example 7.4b
1 public class Main {
2 public static void main(String[] args) {
3 Person myObj = new Person();
4 myObj.name = “John”;
5 System.out.println(myObj.name);
6 }
7 }
As we try to access a private variable, we get an error displayed. If the variable were to be
declared as public, we would expect “John” to be displayed as output.
Instead, we use the getName() and setName() methods to access and update the variable:
Example 7.4c
1 public class Main {
2 public static void main(String[] args) {
3 Person myObj = new Person();
4 myObj.setName(“John”);
5 System.out.println(myObj.getName());
6 }
7 }
Benefits of Encapsulation
● Better control of class attributes and methods
● Class attributes can be made read-only (if you only use the get method), or write-only (if
you only use the set method)
● Flexible: the programmer can change one part of the code without affecting other parts
● Increased security of data
CLASS WORK
1. What is the output of the following code?
1 public class Main {
2 public void myTuna() {
3 System.out.println(“Let’s swim”);
4 }
5
6 public static void main(String[] args) {
7 myTuna();
8 }
9 }