Skip to Content
Welcome to my more educational website. Head back to more fun stuff here!

Objects and Classes

An object is a grouping of data (fields) and operations (methods) that can be performed on that data.

A class is a blueprint for creating objects. It defines the data fields and behaviors (methods) that the objects created from the class will have.

Abstraction and Encapsulation

Abstraction is the concept of hiding implementation details and exposing only the essential features of an object.

Encapsulation is a mechanism used to achieve abstraction by:

  • bundling data and methods into a single unit (typically a class)
  • restricting access to data using access modifiers

The class construct defines a new object type, encapsulating its data and methods. Object-Oriented Programming is a programming paradigm centered on these objects.

A class may contain:

  • public members: accessible outside the class
  • private members: accessible only within the class
  • protected members: accessible to class and derived class members

A constructor is a special class member used to initialize objects. A constructor will have the same name as the class and no return type. A default constructor is automatically executed when an object is created. Constructors are not methods.

If no constructor is defined, Java provides a default constructor.

Example: Object and Class in Java

public class Car { // Private data field private String color; // Constructor (not a method) public Car(String color) { this.color = color; } // Public mutator method public void setColor(String color) { this.color = color; } // Public accessor method public String getColor() { return color; } }

Object Equality in Java

In Java, comparing objects requires understanding two different types of equality:

Reference Equality

Checks whether two variables point to the exact same object in memory.

String s1 = new String("hello"); String s2 = new String("hello"); System.out.println(s1 == s2); // false

Logical Equality

Checks whether two objects have the same content. Must be overridden for custom classes.

System.out.println(s1.equals(s2)); // true

Always override java equals() and java hashCode() in custom classes to ensure meaningful equality comparisons. java hashCode() is what is used with hash-based collections.

Passing Objects to Methods

Java is strictly pass-by-value for object references.

When you pass an object to a method, you are passing a copy of the reference to the object. NOT the actual object or reference itself. You can modify the object via this reference, but you cannot change what the original reference points to.

Autoboxing and Unboxing

Integer num = 10; // autoboxing: int → Integer int x = num; // unboxing: Integer → int

Autoboxing is Java’s automatic conversion of primitive types to their corresponding wrapper classes. Unboxing is the reverse.

Autoboxing is useful when working with generics or collections that require objects.

Inheritance and Polymorphism

Object-Oriented Programming (OOP) in Java is built on Inheritance and Polymorphism.

Inheritance: Modeling “Is-A” Relationships

Inheritance allows a new class (called a subclass/child/derived class) to acquire the attributes and behaviors of an existing class (called a superclass or parent class). This models an is-a relationship. For example, if Car inherits from Vehicle, then a car is a type of vehicle:

class Vehicle { void start() { System.out.println("Vehicle starting"); } } class Car extends Vehicle { void honk() { System.out.println("Car honking"); } }

Now, Car inherits the java start() method from Vehicle. This means a Car object can both java start() and java honk().

In Java, Inheritance is defined using the extends keyword. Subclasses can add new methods or override inherited ones. Each class can inherit from only one parent.

Composition: Modeling “Has-A” Relationships

Composition is another fundamental OOP concept where a class contains references to other objects. This models a “has-a” relationship, such as a Car having an Engine:

class Engine { void ignite() { System.out.println("Engine ignited"); } } class Car { private Engine engine = new Engine(); void start() { engine.ignite(); System.out.println("Car starting"); } }

Here, Car is not an Engine, but it has an Engine.

Composition allows for code to be reused without strict inheritance and is preferred over inheritance when classes don’t share a natural is-a relationship.

Polymorphism

Polymorphism in Java allows objects of different classes to be treated as objects of a common superclass. This is particularly useful when writing general-purpose code.

class Vehicle { void move() { System.out.println("Vehicle is moving"); } } class Bike extends Vehicle { void move() { System.out.println("Bike is moving fast"); } } class Truck extends Vehicle { void move() { System.out.println("Truck is moving slowly"); } } public class Demo { public static void main(String[] args) { Vehicle v1 = new Bike(); Vehicle v2 = new Truck(); v1.move(); // Bike is moving fast v2.move(); // Truck is moving slowly } }
Last updated on