A comprehensive collection of Java learning modules covering fundamental OOP concepts, data structures, and file operations. This repository contains practical examples and implementations of core Java features suitable for beginners and intermediate learners.
Location: Aggregation/src/
Demonstrates the "Has-A" relationship in object-oriented programming.
Classes:
Book.java- Represents a book with title and ISBNLibrary.java- Represents a library that contains multiple booksMain.java- Example demonstrating aggregation
Key Concepts:
- Loose coupling between objects
- A Library "has a" collection of Books
- If the Library is removed, Books still exist independently
- Aggregation represents a "whole-part" relationship
Example:
Library lib = new Library("Alex", 452, books);
lib.contain(); // Library contains books but doesn't own themLocation: Composition/src/
Demonstrates the "Part-Of" relationship where child objects cannot exist independently.
Classes:
Car.java- Represents a car with model, year, and engineEngine.java- Represents a car engineMain.java- Example demonstrating composition
Key Concepts:
- Tight coupling between objects
- A Car "has" an Engine that is part of it
- If the Car is destroyed, the Engine ceases to exist
- Child objects depend on their parent
Example:
Car car1 = new Car("BMW", 2025, "VME");
System.out.println(car1.engine.type); // Engine is part of carLocation: ArrayList/src/
Explores dynamic resizable arrays using the ArrayList collection.
Classes:
Main.java- Comprehensive ArrayList operations
Key Concepts:
- Resizable array that stores objects
- Type-safe using generics (e.g.,
ArrayList<String>) - Common methods:
add()- Add elementsremove()- Remove by value or indexset()- Modify elementsget()- Retrieve elementssize()- Get collection sizesort()- Sort elements using Collections
Example:
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.remove("Apple");
Collections.sort(fruits);Location: Enum/src/
Implements enumerations - a special data type for fixed sets of constants.
Classes:
Numbers.java- Enum for numbers (ONE, TWO, THREE)WeekDays.java- Enum for days of the week with status methodsMonths.java- Enum for months with parameterized constructors and day countsOperation.java- Enum for mathematical operationsOrderStatus.java- Enum for order statusesMain.java- Examples of enum usage
Key Concepts:
- Enums represent fixed sets of constants
- Improve code readability and maintainability
- More efficient than strings in switch statements
- Can have constructors, methods, and fields
- Iteration using
values()method
Example:
Numbers num = Numbers.ONE;
switch(num) {
case ONE: System.out.println(1); break;
case TWO: System.out.println(2); break;
}
// Parameterized Enum
System.out.println(Months.JANUARY.getDays()); // 31Location: Exception/src/
Demonstrates proper exception handling using try-catch-finally blocks.
Classes:
Main.java- Exception handling examples
Key Concepts:
- Exceptions interrupt normal program flow
- Common exceptions: ArithmeticException, NumberFormatException, etc.
- Try-catch-finally blocks for error handling
- Multiple catch blocks for specific exception types
- Exception hierarchy (RuntimeException, Exception, etc.)
Covered Scenarios:
- Division by zero
- Input type mismatch
- Specific exception types vs. generic Exception
Example:
try {
int x = Integer.valueOf(scan.nextLine());
} catch (NumberFormatException e) {
System.out.println("This is not an integer number");
}Location: Hashmaps/src/
Explores key-value pair storage using HashMap data structure.
Classes:
Main.java- HashMap operations and demonstrations
Key Concepts:
- Stores key-value pairs (HashMap<Key, Value>)
- Keys are unique; values can be duplicated
- Does not maintain insertion order
- Memory efficient
- Uses wrapper classes for keys and values
Common Methods:
put()- Add or override key-value pairsget()- Retrieve value by keyremove()- Remove key-value paircontainsKey()- Check if key existskeySet()- Get all keys for iterationsize()- Get number of pairsclear()- Remove all entries
Example:
HashMap<String, Integer> mp = new HashMap<>();
mp.put("apple", 50);
mp.put("banana", 32);
for(String key : mp.keySet()) {
System.out.println(key + " ; " + mp.get(key));
}Location: Wrapper Class/src/
Demonstrates converting primitive types to objects using wrapper classes.
Classes:
Main.java- Wrapper class implementations
Key Concepts:
- Wrapper classes convert primitives to objects
- Allows primitives to be used in collections and streams
- Types: Integer, Double, Character, Boolean, String, etc.
- Autoboxing - Automatic conversion from primitive to wrapper
- Unboxing - Automatic conversion from wrapper to primitive
- Useful for collection framework and static utility methods
Example:
// Autoboxing
Integer a2 = 5;
Double b2 = 5.5;
// Unboxing
int a3 = a2;
double b3 = b2;
// Casting
String s2 = Integer.toString(548);Location: Refrence Casting/src/
Covers upcasting and downcasting in object-oriented programming.
Classes:
Animal.java- Parent classMammal.java- Inherits from AnimalLion.java- Inherits from MammalMain.java- Casting examples
Key Concepts:
- Upcasting - Converting child reference to parent type
- Can access only parent class methods
- Method overriding applies with upcasting
- Child-specific methods not visible through parent reference
- Safe and implicit conversion
Example:
Animal animal2 = new Mammal(); // Upcasting
animal2.eat(); // Calls overridden method
animal2.sleep();
// animal2.control(); // Error - method not in AnimalLocation: Date and Time/src/
Working with dates and times using Java's modern time API.
Classes:
Main.java- Date and time operations
Key Concepts:
LocalDate- Date without timeLocalTime- Time without dateLocalDateTime- Combined date and timeInstant- UTC timestampDateTimeFormatter- Format dates and times- Date comparison methods:
isBefore(),isAfter(),isEqual()
Example:
LocalDate date = LocalDate.now();
LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm:ss");
String formatted = dateTime.format(formatter);Location: Anonymous Class/src/
Demonstrates anonymous inner classes for one-time use cases.
Classes:
Dog.java- Base classCat.java- Interface for implementing anonymouslyMain.java- Anonymous class examples
Key Concepts:
- Anonymous classes don't have a name
- Cannot be reused
- Used for one-time custom behavior
- Can extend a class or implement an interface
- Common in callbacks, TimerTask, Runnable
- Compiler generates name like
Main$1,Main$2, etc.
Example:
Dog german = new Dog() {
@Override
public void Sound() {
System.out.println("Rar Rar Rar");
}
};Location: Write and Read Files/src/
File I/O operations for reading and writing text files.
Classes:
Main.java- File operations
Key Concepts:
- FileWriter - Good for small/medium text files
- BufferedWriter - Better performance for large amounts of text
- PrintWriter - Best for structured data (reports, logs)
- FileOutputStream - Best for binary files
- BufferedReader - Efficient reading of text files
- FileReader - Basic file reading
- Try-with-resources for automatic resource management
Example:
try (FileWriter writer = new FileWriter("text.txt")) {
writer.write("I Love Pizza\nAnd I love meat");
System.out.println("Write Successfully");
} catch (IOException e) {
System.out.println("Error: can't write");
}- Java JDK 8 or higher installed
- IntelliJ IDEA or any Java IDE
- Basic understanding of Java fundamentals
-
Clone the repository:
git clone https://github.com/abdallahesham012/JavaFundamentals-.git cd JavaFundamentals -
Open the project in IntelliJ IDEA or your preferred IDE
-
Navigate to any module and run the
Main.javaclass:- Right-click on Main.java
- Select "Run 'Main.main()'"
-
View the console output for results
Suggested order for learning:
- Wrapper Classes - Understand primitive to object conversion
- ArrayList - Learn about dynamic collections
- HashMap - Explore key-value storage
- Enum - Understand constant sets
- Composition - Learn tight coupling relationships
- Aggregation - Learn loose coupling relationships
- Reference Casting - Understand inheritance hierarchies
- Anonymous Classes - Learn inner classes
- Exception Handling - Handle runtime errors
- Date and Time - Work with temporal data
- Write and Read Files - Persist data to files
- Object-Oriented Programming (OOP)
- Inheritance and Polymorphism
- Composition vs. Aggregation
- Collections Framework (ArrayList, HashMap)
- Enumerations (Enums)
- Exception Handling
- Wrapper Classes and Autoboxing
- Anonymous Inner Classes
- Date and Time API
- File I/O Operations
Feel free to contribute improvements, bug fixes, or additional examples. Fork the repository and submit a pull request.
Created by Abdallah Hesham as a learning resource for Java fundamentals and OOP concepts.
Happy Learning!