Core_Java_Notes
Core_Java_Notes
Introduction
Core Java is the foundational platform for learning Java programming. It is essential for
building applications across industries. This document aims to cover fundamental Java
concepts, their applications, and best practices to help students or developers grasp the
core concepts in a structured and effective manner. The following sections will provide
detailed coverage of key aspects such as Object-Oriented Programming (OOP), collections,
exception handling, multithreading, and file handling.
Object-Oriented Programming (OOP) is the core paradigm behind Java, encapsulating real-
world concepts into objects. OOP focuses on four major principles: Inheritance,
Polymorphism, Abstraction, and Encapsulation.
1.1 Inheritance
Inheritance is the mechanism in Java that allows one class (subclass/child class) to inherit
the properties and methods of another class (superclass/parent class). This promotes code
reusability.
Example of Inheritance:
java
CopyEdit
class Animal {
void eat() {
System.out.println("Animal eats");
void bark() {
System.out.println("Dog barks");
1.2 Polymorphism
Polymorphism allows different classes to be treated as instances of the same class through
inheritance. It supports method overriding and method overloading.
Example of Polymorphism:
java
CopyEdit
class Animal {
void makeSound() {
@Override
void makeSound() {
System.out.println("Dog barks");
1.3 Abstraction
Abstraction hides the complexity of the system and only exposes the necessary details to
the user. This is achieved through abstract classes and interfaces.
Example of Abstraction:
java
CopyEdit
abstract class Animal {
void sound() {
System.out.println("Bark");
1.4 Encapsulation
Encapsulation is the concept of wrapping data (variables) and methods into a single unit
called a class. It hides the internal state of an object and requires all interaction to be
performed through methods.
Example of Encapsulation:
java
CopyEdit
class Account {
balance += amount;
return balance;
}
Chapter 2: Collections Framework
The Collections Framework provides a set of classes and interfaces for storing and
manipulating groups of data as a single unit. Java provides various collections like List, Set,
and Map.
2.1 Lists
A List is an ordered collection that can contain duplicate elements. The most commonly
used list classes are ArrayList and LinkedList.
java
CopyEdit
import java.util.*;
animals.add("Cat");
animals.add("Dog");
System.out.println(animals);
2.2 Sets
A Set is a collection that does not allow duplicate elements. HashSet is the most common
implementation of Set.
java
CopyEdit
import java.util.*;
animals.add("Cat");
animals.add("Dog");
System.out.println(animals);
Java provides a robust mechanism for handling runtime errors, called exception handling. It
helps prevent the application from crashing and allows developers to define custom error
conditions.
The try block contains the code that may throw an exception, the catch block handles the
exception, and the finally block contains the code that will always execute.
java
CopyEdit
try {
} catch (ArithmeticException e) {
} finally {
}
Chapter 4: Multithreading
Multithreading in Java allows multiple threads to run concurrently, which improves the
performance of CPU-intensive tasks.
The Thread class is the main entry point for multithreading in Java. You can extend the
Thread class and override its run() method.
Example of Multithreading:
java
CopyEdit
System.out.println("Thread is running...");
thread.start();
Java provides classes for reading from and writing to files. These are part of the java.io
package.
Using FileReader and BufferedReader for reading files and FileWriter and
BufferedWriter for writing files.
Example of File Handling:
java
CopyEdit
import java.io.*;
try {
writer.write("Hello, world!");
writer.close();
} catch (IOException e) {
Conclusion
Mastering Core Java is critical for Java developers as it forms the foundation for working
with advanced concepts and technologies. By understanding OOP principles, collections,
exception handling, multithreading, and file handling, developers can write more efficient,
maintainable, and robust applications. Continuous practice, project-based learning, and
real-world applications are key to mastering Core Java.