Loading…
Loading…
Topic
Articles and problems tagged Object-Oriented Programming.
Master Java wildcards: why List<Manager> is not a List<Employee>, how arrays differ, raw-type pitfalls, ? extends for reading, ? super for writing, the PECS rule, Comparable<? super T>, unbounded wildcards and wildcard capture with helper methods.
Learn why Java generics exist and how to use them: type parameters versus Object and casts, defining a generic class like Pair<T>, naming conventions, the diamond operator, generic methods, type inference and how to decode inference errors.
Learn how to throw exceptions in Java with the throw statement, pick the right standard exception, write your own checked or unchecked exception classes with the four conventional constructors, and attach extra data and causes.
Learn the remaining kinds of Java nested classes: local classes inside methods, capturing effectively final variables, anonymous inner classes and their syntax, var with anonymous types, double brace initialization, the getEnclosingClass trick, and static nested classes and records.
Understand Java inner classes: why they exist, how an inner object accesses the private fields of its outer object, the hidden this$0 reference, OuterClass.this and outer.new Inner() syntax, static members in inner classes (Java 16), and how the compiler translates them.
Seven practical rules for Java class hierarchies: put common code in superclasses, avoid protected fields, inherit only for true is-a relationships, keep inherited methods meaningful, honor the substitution principle, prefer polymorphism over type tests, and use reflection sparingly.
Use Java reflection to inspect any class at runtime: Field, Method, Constructor and RecordComponent, getDeclaredFields vs getFields, Modifier and AccessFlag, sealed and record metadata, reading and writing fields with setAccessible, module restrictions, and a generic toString.
Start with Java reflection: what it is for, getting Class objects with getClass, Class.forName and .class literals, odd array type names, comparing Class objects, creating instances by name, a primer on checked exceptions, and loading resources with getResource and getResourceAsStream.
Java sealed classes and interfaces (final since Java 17) let you list exactly which classes may extend a type. Learn sealed, permits, final and non-sealed, the placement rules, exhaustive switch without default, records and enums in sealed hierarchies, and when to choose sealed types.
Java enums are full classes with a fixed set of instances. Learn to compare with ==, use name, valueOf, values, ordinal and compareTo, add fields, constructors and methods, write constant-specific behavior, switch over enums, and use EnumMap and EnumSet.
Learn when and how to use abstract classes in Java: declaring abstract methods as placeholders, mixing fields and concrete methods, why abstract classes can't be instantiated, abstract variables referring to concrete subclasses, the template method pattern, and abstract classes versus interfaces.
Java's wrapper classes Integer, Double, Boolean and friends explained: why ArrayList<int> isn't allowed, how autoboxing and unboxing work, the == trap and the Integer cache, NullPointerException on unboxing, parseInt and other static helpers, MIN_VALUE pitfalls, and performance costs.
Object is the superclass of every Java class. Learn what Object variables can hold, the difference between == and equals, how to write a correct equals method, the five rules of the equals contract, getClass vs instanceof in class hierarchies, and the classic mistakes to avoid.
Control and navigate Java class hierarchies: final classes and methods (and why constructors should never call overridable methods), casting object references safely, ClassCastException, pattern matching for instanceof, record patterns, and how protected access really works.
Understand polymorphism in Java: the is-a rule and substitution principle, declared vs actual types, why arrays are covariant and throw ArrayStoreException, the exact steps of a method call (overload resolution, static vs dynamic binding, method tables), covariant return types and override rules.
Learn Java inheritance step by step: defining subclasses with extends, what a subclass inherits, overriding methods and calling super.method(), the @Override annotation, subclass constructors with super(...), inheritance hierarchies, and why Java has no multiple inheritance of classes.
Seven practical rules for designing Java classes: keep data private, initialize everything, replace primitive clusters with classes, skip needless getters and setters, split classes with several responsibilities, choose good names, and prefer immutability, illustrated with records and enums.
Understand Java's default package access and why forgetting private breaks encapsulation, then master the class path: how the JVM and javac locate classes in directories and JAR files, -cp and --class-path, wildcards, the CLASSPATH variable, and fixing ClassNotFoundException.
Java records explained: declaring immutable data carriers in one line, components and accessors like x(), generated toString, equals and hashCode, adding methods and static members, canonical, compact and custom constructors for validation and normalization, and when to choose a record over a class.
What static means in Java: static fields shared by all objects, static final constants like Math.PI and System.out, static methods without this, when to use them, static factory methods (and why they beat constructors), and why main is static, plus Java 25 instance main methods.