What Is Composition vs Inheritance in Java and Which Should You Prefer?
Prefer composition to inheritance in most Java code. See the has-a versus is-a difference, a fragile base class example and when extends is still right.
2,877 posts about Java: questions with answers, tutorials and articles, newest first.
Prefer composition to inheritance in most Java code. See the has-a versus is-a difference, a fragile base class example and when extends is still right.
Equal objects must have equal hash codes. This guide covers the equals and hashCode contract, the bugs that break it, and a correct implementation.
Every Java class inherits from Object. See what it gives you and how to override toString, equals and hashCode so objects print, compare and hash correctly.
Method overriding lets a subclass replace an inherited method, and the JVM picks the version at runtime. This guide covers the rules and the difference from overloading.
Polymorphism lets one name or reference type behave in many ways. Compare overloading and overriding with runnable Java examples and a clear comparison table.
Inheritance lets a subclass reuse and specialise a superclass using extends. See super, overriding, single inheritance and when composition is the better choice.
Encapsulation hides a class's data behind methods so it can validate input and change internals safely. See getters, setters, immutability and records in Java.
Java has four access levels: private, default, protected and public. Learn what each allows, with a comparison table and examples for fields, methods and classes.
The static keyword makes a member belong to the class instead of an object. See static variables, methods, blocks and nested classes with runnable examples.
A class is a blueprint and an object is an instance built from it. Build a small class, create objects with new, and learn constructors, this and references.
Format numbers and money for any country with NumberFormat and Locale. Covers currency symbols, decimals, rounding, compact numbers, parsing and thread safety.
Instant stores a moment on the UTC timeline; ZonedDateTime adds a zone and local clock. See conversions, DST pitfalls and when to choose each.
Use BigDecimal for money and exact decimals, not double. See why binary floats drift, how to build values safely, round, divide and compare.
A practical tour of java.lang.Math and random number generation in Java: rounding rules, overflow-safe maths and how to pick a number in a range.
Wrapper classes turn Java primitives into objects, and autoboxing converts between them automatically. Learn the caching trap, null pitfalls and speed costs.
Use String.format and printf to build aligned, padded and rounded text in Java. Covers %s, %d, %f, flags, locales and common exceptions.
StringBuilder builds large text in linear time while String += in a loop is quadratic. Learn capacity, key methods and when to pick other tools.
Use equals for content, equalsIgnoreCase to ignore case and compareTo for ordering. See why == is wrong and how to avoid NullPointerException.
Java is strictly pass by value, even for objects. Learn how references are copied, why swap fails and why Strings never change inside a method.
Recursion in Java is a method calling itself on a smaller input. See how the call stack works, classic examples and how to avoid StackOverflowError.