0% found this document useful (0 votes)
2 views

3 - Java OOP (I)

Uploaded by

Nguyen Huy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

3 - Java OOP (I)

Uploaded by

Nguyen Huy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Java Basic for Tester

Java OOP (I)

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 1


Agenda

 Java Class and Objects


 Java Methods
 Java Constructor
 Java Strings
 Java Access Modifiers
 Java this keyword
 Java final keyword
 Java Recursion
 Java instanceof Operator
1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 2
Java Class and Objects

Java is an object-oriented programming language. It is based on the


concept of objects.

These objects share two characteristics:


 state (fields)
 behavior (methods)
Bicycle is an object
States: current gear, two wheels, number of gear, etc
Behavior: braking, accelerating, changing gears, etc
Before you create objects in Java, you need to define a class. A class is a
blueprint for the object.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 3


Java Class and Objects

Here's how we can define a class in Java:

class ClassName {
// variables
// methods
}

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 4


Java Class and Objects

Java Objects: An object is called an instance of a class.

className object = new className();

Here, we are using the constructor className() to create the object.


Constructors have the same name as the class and are similar to methods.

// l1 object
Lamp l1 = new Lamp(); // access method turnOn()
// l2 object l1.turnOn();
Lamp l2 = new Lamp();

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 5


Java Class and Objects

Java Objects: An object is called an instance of a class.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 6


Java Class and Objects

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 7


Java Methods
In object-oriented programming, the method is a jargon used for function.
Methods are bound to a class and they define the behavior of a class.

Types of Java
methods

Standard Library User-defined


Methods Methods

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 8


Java Methods

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 9


Java Methods

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 10


Java Constructors

In Java, every class has its constructor that is invoked automatically when
an object of the class is created. A constructor is similar to a method but in
actual, it is not a method.

class Test {
Test() {
// constructor body
}
}

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 11


Java Constructors

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 12


Java Constructors

Types of
Constructor

No-Arg Default Parameterized


Constructor Constructor Constructor

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 13


Java Constructors

No-Arg Constructor

A Java constructor may or may not have any parameters (arguments). If a


constructor does not accept any parameters, it is known as a no-arg
constructor.

private Constructor() {
// body of constructor
}

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 14


Java Constructors

No-Arg Constructor

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 15


Java Constructors

Default Constructor
If you do not create any constructors, the Java compiler will automatically
create a no-argument constructor during run-time. This constructor is
known as the default constructor. The default constructor initializes any
uninitialized instance variables with default values.
Type Default Value
boolean False
byte 0
Int 0
double 0.0d
1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 16
Java Constructors

Default Constructor

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 17


Java Constructors

Parameterized Constructor
Similar to methods, we can pass parameters to a constructor. Such
constructors are known as a parameterized constructor.

private Constructor (arg1, arg2, ..., argn) {


// constructor body
}

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 18


Java Constructors

Parameterized Constructor

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 19


Java Strings

In Java, a string is a sequence of characters. For example, "hello" is a string


containing a sequence of characters 'h', 'e', 'l', 'l', and 'o'.
Unlike other programming languages, strings in Java are not primitive types
(like int, char, etc). Instead, all strings are objects of a predefined class
named String.

// create a string
String type = "java programming";

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 20


Java Strings

Java String provides various methods that allow us to perform different string
operations. Here are some of the commonly used string methods.

Methods Description
concat() joins the two strings together

equals() compares the value of two strings

charAt() returns the character present in the specified location

getBytes() converts the string to an array of bytes


indexOf() returns the position of the specified character in the string

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 21


Java Strings

Java String provides various methods that allow us to perform different string
operations. Here are some of the commonly used string methods.
Methods Description
length() returns the size of the specified string

replace() replaces the specified old character with the specified new character

substring() returns the substring of the string

split() breaks the string into an array of strings

toLowerCase() converts the string to lowercase

toUpperCase() converts the string to uppercase

valueOf() returns the string representation of the specified data


1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 22
Java Strings

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 23


Java Strings

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 24


Java Strings

Strings in Java are represented by double-quotes.

// create a string
String example = "This is a string";

Now if we want to include double-quotes in our string

// include double quote


String example = "This is the "String" class";

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 25


Java Strings

To solve this issue, the escape character \ is used in Java. Now we can
include double-quotes in the string as:

// use the escape character


String example = "This is the \"String\" class.";

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 26


Java Access Modifiers

In Java, access modifiers are used to set the accessibility (visibility) of


classes, interfaces, variables, methods, constructors, data members, and
the setter methods.
class Animal {
public void method1() {...}

private void method2() {...}


}
 method1 is public - This means it can be accessed by other classes.
 method2 is private - This means it can not be accessed by other
classes.
1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 27
Java Access Modifiers

Access
Modifier

Default Private Protected Public

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 28


Java Access Modifiers

Default Access Modifier


If we do not explicitly specify any access modifier for classes, methods,
variables, etc, then by default the default access modifier is considered.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 29


Java Access Modifiers

Private Access Modifier

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 30


Java Access Modifiers

Protected Access Modifier


When methods and data members are declared protected, we can access
them within the same package as well as from subclasses.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 31


Java Access Modifiers

Public Access Modifier


When methods, variables, classes, and
so on are declared public, then we can
access them from anywhere. The public
access modifier has no scope restriction.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 32


Java Access Modifiers

Access modifiers are mainly used for encapsulation. I can help us to


control what part of a program can access the members of a class. So that
misuse of data can be prevented.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 33


Java this Keyword

In Java, this keyword is used to refer to the current object inside a method
or a constructor.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 34


Java final keyword

In Java, the final keyword is used to denote constants.

Variables
Classes

Methods

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 35


Java final keyword

Java final Variable


In Java, we cannot change the value of a final variable.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 36


Java final keyword

Java final Method


In Java, the final method cannot be overridden by the child class.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 37


Java final keyword

Java final Class


In Java, the final class cannot be inherited by another class.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 38


Java Recursion

In Java, a method that calls itself is known as a recursive method. And, this
process is known as recursion.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 39


Java Recursion

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 40


Java instanceof

In Java, the instanceof keyword is a binary operator. It is used to check


whether an object is an instance of a particular class or not.

The syntax of the instanceof is:

result = objectName instanceof className;

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 41


Java instanceof

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 42


Thank you

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 43

You might also like