Oop Homework Compilation
Oop Homework Compilation
(Expanded Guide)
a. System.out.print()
System.out.print("A");
System.out.print("B"); // Output: AB
b. System.out.println()
System.out.println("Hello");
System.out.println("World");
c. System.out.printf()
1
3. Static and Non-static Blocks
a. Static Block
static {
System.out.println("Static block");
}
{
System.out.println("Instance block");
}
If you place a mutable object like a List in an immutable class without copying it, it breaks immutability.
class Student {
private final List<String> subjects;
a. Association
2
b. Aggregation (Has-A)
• Weak ownership.
• Objects can exist independently.
class Department {}
class University {
Department dept; // Aggregation
}
c. Composition
• Strong ownership.
• Lifespan of part depends on whole.
class Engine {}
class Car {
private final Engine engine = new Engine();
}
Association No No Teacher–Class
• Command line arguments allow users to pass data to the main() method at program start.
• main(String[] args) receives these arguments.
Run in terminal:
3
7. Regex (Regular Expression)
a. Basic Syntax:
b. Java Usage:
Yes, you can. Every class in Java extends Object (directly or indirectly), so any class instance can be
passed where Object is expected.
However:
• Accessible:
• Inside the same class
• In the same package
• In subclasses (even outside the package)
4
class Parent {
protected int number = 42;
}
• Use when you want subclasses to have access but still restrict full external usage.
a. Shallow Copy
class Student {
StringBuilder name;
Student(Student s) {
this.name = s.name; // shallow copy
}
}
b. Deep Copy
class Student {
StringBuilder name;
Student(Student s) {
this.name = new StringBuilder(s.name); // deep copy
}
}