Java II
Present by Wai Lin
Class & Method
A class is a blueprint that defines an object’s data (fields) and actions (methods)
public class Person {
String name; // 1. Fields (variables)
int age;
void speak() { // 2. Method (behavior)
[Link](name + " is speaking");
}
}
Person p1 = new Person();
[Link] = "Mg Mg"; [Link] = 0;
[Link]();
Constructor
constructor is a special method that runs automatically when an object is created and initializes its data
public class Person {
String name;
int age;
// Constructor
public Person(String n, int a) {
name = n;
age = a;
Person p1 = new Person("Mg Mg", 20);
[Link]();
Encapsulation
Encapsulation hides data and controls how it is accessed or modified using getter/setter methods.
public class Person {
private int age;
public int getAge() { return age;}
public void setAge(int age) {
if (age < 0) { // Example of data protection
[Link]("Invalid age!");
return;
[Link] = age;
}}
Person A = new Person();
[Link](12);
[Link]();
Inheritance
Inheritance allows one class to reuse another class’s fields and methods.
public class Employee extends Person {
double salary; // new field
void work() {
[Link](name + " is working");
Employee e = new Employee();
[Link] = "Aye Aye";
[Link] = 500000; // Own field
[Link](); // Inherited method
[Link](); // Own method
Polymorphism
many Inheritance of one parent class
class Person {
void speak() {
[Link]("Person speaking");
} }
class Employee extends Person {
@Override
void speak() {
[Link]("Employee speaking");
} }
class Student extends Person {
@Override
void speak() {
[Link]("Student speaking");
} }
Interface
An interface is a contract that defines what methods a class must have, but does NOT provide implementation.
public interface Walkable {
void walk(); // abstract method (no body)
}
public class Person implements Walkable {
@Override
public void walk() {
[Link]("Person is walking");
}
}
Collections Framework
Collections Framework gives you ready-made tools to store many objects and work with them easily.
Why use Collections?
✔ Dynamic size (unlike arrays)
✔ Built-in methods for searching, sorting, filtering
✔ Easy to manage data
✔ Standard and widely used in real applications
Main Interfaces
● List → Ordered, allows duplicates (ArrayList, LinkedList)
● Set → No duplicates (HashSet, TreeSet)
● Map → Stores key-value pairs (HashMap, TreeMap)
Collections Framework
Collections Framework gives you ready-made tools to store many objects and work with them easily.
List<String> names = new ArrayList<>();
[Link]("Mg Mg");
[Link]("Aye Aye");
[Link]("Mg Mg"); // duplicates allowed
Set<Integer> ids = new HashSet<>();
[Link](1);
[Link](2);
[Link](1); // ignored
Map<String, Integer> ages = new HashMap<>();
[Link]("Mg Mg", 20);
[Link]("Aye Aye", 25);
[Link]("Mg Mg", 22); // updates value, keys must be unique
Annotations
special markers you put in code to give information to the compiler or JVM
@Override
Tells the compiler a method overrides a parent method.
@Deprecated
Marks a method/class as outdated.
@SuppressWarnings
Hides specific warnings from the compiler.
//spring boot annotations
@RestController
@GetMapping("/users")
Stream API
modern way — without writing loops
It is used to:
● filter data
● sort data
● transform data
● count data
● collect data into a list
Because instead of writing:
for (int n : numbers) {
if (n > 10) {
[Link](n);
You can write:
[Link]()
.filter(n -> n > 10)
.toList();
Stream API
modern way — without writing loops
It is used to:
● filter data
● sort data
● transform data
● count data
● collect data into a list
Because instead of writing:
for (int n : numbers) {
if (n > 10) {
[Link](n);
You can write:
[Link]()
.filter(n -> n > 10)
.toList();
Build Tools
1. Maven
● Uses [Link]
● Standard in most companies
● Great for Spring Boot
2. Gradle
● Uses [Link]
● Popular in Android development
3. Ant
● Older tool
● Still used in some legacy projects
● Uses [Link]
● Does not have built-in dependency management (needs Ivy)
Maven
All project dependencies are inside [Link]
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.0.0</version>
</dependency>
Maven can:
● compile
● test
● package
● clean
● run apps
mvn clean install
mvn package
mvn test
Resources
[Link]
free certificate course
[Link]