javaworld

Comparing Java objects with equals() and hashcode()

In this Java Challenger you’ll learn how equals() and hashcode() combine to make object comparisons efficient and easy in your Java programs. Simply put, these methods work together to verify if two objects have the same values.  Without equals() and hashcode() we would have to create very large "if" comparisons, comparing every field from an object. This would make code really confusing and hard to read. Together, these two methods...

javaworld

Comparing Java objects with equals() and hashcode()

In this Java Challenger you’ll learn how equals() and hashcode() combine to make object comparisons efficient and easy in your Java programs. Simply put, these methods work together to verify if two objects have the same values.  Without equals() and hashcode() we would have to create very large "if" comparisons, comparing every field from an object. This would make code really confusing and hard to read. Together, these two methods...

javaworld

Debugging Java inheritance

Inheritance and composition are two programming techniques developers use to establish relationships between classes and objects. Whereas inheritance derives one class from another, composition defines a class as the sum of its parts.Classes and objects created through inheritance are tightly coupled because changing the parent or superclass in an inheritance relationship risks breaking your code. Classes and objects created through...

javaworld

Interfaces in Java

Java interfaces are different from classes, and it's important to know how to use their special properties in your Java programs. This tutorial introduces the difference between classes and interfaces, then guides you through examples demonstrating how to declare, implement, and extend Java interfaces.You'll also learn how the interface has evolved in Java 8, with the addition of default and static methods, and in Java 9 with the new...

javaworld

Class and object initialization in Java

Classes and objects in Java must be initialized before they are used. You've previously learned that class fields are initialized to default values when classes are loaded and that objects are initialized via constructors, but there is more to initialization. This article introduces all of Java's features for initializing classes and objects. download Get the code Download the source code for example applications in this...

javaworld

Java polymorphism and its types

Polymorphism refers to the ability of some entities to occur in different forms. It is popularly represented by the butterfly, which morphs from larva to pupa to imago. Polymorphism also exists in programming languages, as a modeling technique that allows you to create a single interface to various operands, arguments, and objects. Java polymorphism results in code that is more concise and easier to maintain.While this tutorial focuses...

javaworld

Java tip: Fields and methods in Java

The Java tutorial Java 101: Classes and objects in Java introduces the fundamentals of Java classes and objects, including how to declare classes, describe attributes via fields, describe behaviors via methods, initialize objects via constructors, and instantiate objects from classes and access their members. This Java tip builds on that introduction, introducing seven advanced techniques for working with fields and methods in...

javaworld

Java tip: Fields and methods in Java

The Java tutorial Java 101: Classes and objects in Java introduces the fundamentals of Java classes and objects, including how to declare classes, describe attributes via fields, describe behaviors via methods, initialize objects via constructors, and instantiate objects from classes and access their members. This Java tip builds on that introduction, introducing seven advanced techniques for working with fields and methods in...

javaworld

Inheritance in Java, Part 2: Object and its methods

Java provides a standard class library consisting of thousands of classes and other reference types. Despite the disparity in their capabilities, these types form one massive inheritance hierarchy by directly or indirectly extending the Object class. This is also true for any classes and other reference types that you create.The first half of this tutorial on Java inheritance showed you the basics of inheritance, specifically how to...

javaworld

Classes and objects in Java

Classes, fields, methods, constructors, and objects are the building blocks of object-based Java applications. This tutorial teaches you how to declare classes, describe attributes via fields, describe behaviors via methods, initialize objects via constructors, and instantiate objects from classes and access their members. Along the way, you'll also learn about setters and getters, method overloading, setting access levels for fields,...

javaworld

Java tip: Inheritance relationships in JPA and Hibernate

Inheritance is a common pattern in object-oriented programming, but it's not easily replicated in a database. This Java tip shows you how to model inheritance relationships in JPA with Hibernate.Learn the pros and cons of four different ORM inheritance strategies and get tips for choosing the one that best supports your application needs. I assume you've been introduced to Java persistence with JPA and Hibernate, including how to model...