Master Java Fundamentals: OOP, Collections, Concurrency, and Design Patterns
This comprehensive guide covers Java's core concepts—including object‑oriented principles, differences from C++, polymorphism, static/final modifiers, abstract classes versus interfaces, generics, reflection, exception hierarchy, key data structures like ArrayList, LinkedList, HashMap, ConcurrentHashMap, serialization, String handling, and essential design patterns such as Singleton, Factory, and Abstract Factory—providing interview‑ready knowledge for developers.
Java Fundamentals
This article is part of a technical interview series focusing on Java basics and interview questions, summarizing essential topics for preparation.
Object‑Oriented Three Main Features
Features: Encapsulation, Inheritance, Polymorphism.
Encapsulation: Abstract entities are encapsulated into objects, with private attributes and public accessor methods.
Inheritance: Subclasses extend new fields or functionalities while reusing parent class members; supports single inheritance and multiple interface implementations.
Polymorphism: Achieved via method overriding in subclasses or interface implementation.
Java vs C++ Differences
C++ supports multiple inheritance and manual memory management with pointers; Java uses single inheritance, interfaces for multiple inheritance, no direct pointer access, and automatic memory management via the JVM.
Polymorphism Implementation
Polymorphism relies on dynamic binding, linking method calls to implementations at runtime.
Static vs Dynamic Binding: Static binding occurs at compile time (e.g., method overloading); dynamic binding occurs at runtime (e.g., method overriding, interface implementation).
static and final Keywords
static: Can modify fields and methods. Static fields belong to the class, loaded once, shared by all instances, and accessed via the class name. Static methods are loaded with the class, can only call other static members, and cannot use this.
final: Used for variables, methods, and classes. Final variables cannot be reassigned after initialization (primitive values immutable; reference variables cannot point to a different object). Final methods cannot be overridden. Final classes cannot be subclassed.
Abstract Classes and Interfaces
Abstract Class: Declared with abstract, cannot be instantiated, can contain constructors and both abstract and concrete methods, supports single inheritance.
Interface: A collection of abstract methods (implicitly public abstract), supports multiple inheritance, cannot have constructors, and from Java 8 can contain default and static methods.
Similarities: Both cannot be instantiated and can define abstract methods that subclasses must implement.
Differences: Abstract classes can have constructors, concrete methods, and instance fields; interfaces cannot. Abstract classes support single inheritance, interfaces support multiple inheritance.
Generics and Type Erasure
Generics provide parameterized types for classes, interfaces, and methods. Java implements generics via type erasure: generic type information is removed during compilation, leaving only raw types (e.g., List).
Reflection Principles and Use Cases
Reflection allows runtime inspection and invocation of any class's fields, methods, and constructors.
Obtaining a Class instance:
1. ClassName.class // bytecode reference
2. Class.forName("full.class.Name") // by fully qualified name
3. obj.getClass() // from an object instanceCommon use cases include building generic frameworks, dynamic proxies (AOP), and custom annotations.
Java Exception Hierarchy
Throwable is the superclass of all errors and exceptions, divided into Error (system/internal errors) and Exception (application-level). Exceptions include RuntimeException (unchecked) and CheckedException (must be caught or declared).
Data Structures
ArrayList: Backed by an array, provides fast random access, unsuitable for frequent insertions/deletions in the middle.
LinkedList: Implemented as a doubly‑linked list, ideal for dynamic insertions and deletions; can also act as a stack, queue, or deque.
Thread‑safe alternatives include Vector, Collections.synchronizedList, and CopyOnWriteArrayList.
HashMap Details
Structure: array + linked list + red‑black tree. Load factor defaults to 0.75; when size exceeds 75% of capacity, the map expands to double its size.
Insertion steps:
Check if the internal table is null; if so, initialize.
Compute the hash of the key and determine the index via (n‑1) & hash.
If the bucket is empty, create a new node.
If a collision occurs, compare keys; if equal, replace the value.
If keys differ, add the node to the bucket's linked list or tree (convert to tree when length > 8).
After insertion, check if resizing is needed.
The hash function mixes the high and low 16 bits of the key's hashCode to reduce collisions.
JDK 1.7 uses an array‑linked‑list structure; JDK 1.8 adds red‑black trees for buckets with many entries.
ConcurrentHashMap
Provides thread‑safe access via segment locks in JDK 1.7 and a lock‑free (CAS) approach with synchronized blocks in JDK 1.8.
Serialization and Deserialization
Serialization converts an object’s state to a byte stream for storage or transmission; deserialization reconstructs the object from the byte stream. Mismatched serialVersionUID causes deserialization failures.
String Immutability
Strings are stored in a character array marked final, making them immutable. StringBuffer is synchronized (thread‑safe) but slower than StringBuilder.
Design Patterns
Singleton: Ensures a class has only one instance; implementations include eager, lazy (double‑checked locking), static inner class, and enum.
Factory Method: Defines an interface for creating objects, letting subclasses decide which class to instantiate, promoting decoupling.
Abstract Factory: Provides an interface for creating families of related objects without specifying concrete classes.
Constructors and Initialization Blocks
Constructors can be overloaded; a default constructor is provided only if no explicit constructors exist. Static initialization blocks execute once when the class is loaded, before any instance is created.
Keywords
thisrefers to the current object instance; it cannot be used in static contexts.
Method Overloading vs Overriding
Overloading defines multiple methods with the same name but different signatures within the same class. Overriding provides a new implementation for a method inherited from a superclass, using the @Override annotation.
Object Class Methods
toString : Returns a string representation; usually overridden.
equals : Checks logical equality; default behaves like ==.
hashCode : Returns a hash code; must be consistent with equals.
finalize : Called before garbage collection; rarely overridden.
clone : Performs a shallow copy; class must implement Cloneable.
getClass : Retrieves runtime class information.
notify / wait : Used for thread communication.
Primitive Types and Wrapper Classes
Primitive types hold raw values; wrapper classes provide object representations and utility methods.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Developer
Alibaba's official tech channel, featuring all of its technology innovations.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
