3 - Java OOP (I)
3 - Java OOP (I)
class ClassName {
// variables
// methods
}
// l1 object
Lamp l1 = new Lamp(); // access method turnOn()
// l2 object l1.turnOn();
Lamp l2 = new Lamp();
Types of Java
methods
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
}
}
Types of
Constructor
No-Arg Constructor
private Constructor() {
// body of constructor
}
No-Arg Constructor
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
Parameterized Constructor
Similar to methods, we can pass parameters to a constructor. Such
constructors are known as a parameterized constructor.
Parameterized Constructor
// create a string
String type = "java programming";
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
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
// create a string
String example = "This is a string";
To solve this issue, the escape character \ is used in Java. Now we can
include double-quotes in the string as:
Access
Modifier
In Java, this keyword is used to refer to the current object inside a method
or a constructor.
Variables
Classes
Methods
In Java, a method that calls itself is known as a recursive method. And, this
process is known as recursion.