0% found this document useful (0 votes)
2 views5 pages

COS202 Lecture 7

This document provides an overview of Java Object-Oriented Programming (OOP) features, focusing on class methods, modifiers, and encapsulation. It explains the differences between static and public methods, outlines access and non-access modifiers, and emphasizes the importance of encapsulation for data protection. Additionally, it includes examples and class work questions to reinforce understanding of these concepts.

Uploaded by

wwwtope947
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

COS202 Lecture 7

This document provides an overview of Java Object-Oriented Programming (OOP) features, focusing on class methods, modifiers, and encapsulation. It explains the differences between static and public methods, outlines access and non-access modifiers, and emphasizes the importance of encapsulation for data protection. Additionally, it includes examples and class work questions to reinforce understanding of these concepts.

Uploaded by

wwwtope947
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ACHIEVERS UNIVERSITY, OWO

COLLEGE OF NATURAL AND APPLIED SCIENCES


DEPARTMENT OF COMPUTER SCIENCE
COS 202 – COMPUTER PROGRAMMING II – 3 UNITS

LECTURER IN CHARGE - MR. ADEPOJU, S. E.

LECTURE 7

JAVA OOP FEATURES


Class Methods
You learned from the Java Method lecture (lecture 4) that methods are declared within a class
and used to perform certain actions when invoked. To call a method, write the method's name
followed by two parentheses () and a semicolon ;
The static or public keyword can be used independently in some methods and perform different
actions. Example 7.1 demonstrates the differences between static and public methods.
Example 7.1
1​ public class Main {
2​ // Static method
3​ static void myStaticMethod() {
4​ System.out.println(“Static methods can be called without creating objects”);
5​ }
6
7​ // Public method
8​ public void myPublicMethod() {
9​ System.out.println(“Public methods must be called by creating objects”);
10​ }
11​
12​ // Main method
13​ public static void main(String[] args) {
14​ myStaticMethod();
15​ // myPublicMethod(); This would compile an error
16​
17​ Main myObj = new Main();
18​ myObj.myPublicMethod();
19​ }
20​ }

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

public The class is accessible to any other class​

default The class is only accessible to classes in the same package. This is used when
you don't specify a modifier.

For Attributes and Methods

public The code is accessible to all classes

private The code is only accessible within the declared class

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

final Other classes cannot inherit from the class

abstract The class cannot be used to create objects (To access an abstract class, it must
be inherited from another class.

For Attributes and Methods

final Attributes and methods cannot be overridden/modified

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​ }

2.​ Which modifier fits the given description


“The code is only accessible within the declared class.”
“Other classes should not inherit from the class below.”

3.​ What is the meaning of Encapsulation in Java?

You might also like