Advanced

Object-Oriented Programming

Model domains with classes, inheritance, and polymorphism.

What you will see

Rex 3

About 2 min of reading

Try It

Model a domain with classes. Build a class with fields and a method, override a parent method, and pass a subclass where a parent is expected to see runtime polymorphism in action.

1

Define a class with fields and a method that returns a value built from those fields. Create an instance with new, set a field, and print the result.

class Main { public static void main(String[] args) { Dog rex = new Dog(); rex.name = "Rex"; System.out.println(rex.speak()); } } class Dog { String name; int age; String speak() { return name + " says woof"; } }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Rex says woof
A class is a blueprint. Fields hold state (name, age); methods define behaviour. new Dog() creates one real instance from the blueprint.
2

Override a parent method in a subclass, then hold the subclass in a parent-typed variable. The declared type is Animal but the runtime type is Dog, so the overridden method runs.

class Main { public static void main(String[] args) { Animal a = new Dog(); a.sound(); } } class Animal { void sound() { System.out.println("..."); } } class Dog extends Animal { @Override void sound() { System.out.println("woof"); } }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
woof
Java binds these calls dynamically, so the Dog version runs even through an Animal reference. This is polymorphism.
3

Use a constructor with the same name as the class to initialize an object in one step. Pass the name and age through the constructor and print them back.

class Main { public static void main(String[] args) { Dog rex = new Dog("Rex", 3); System.out.println(rex.name + " " + rex.age); } } class Dog { String name; int age; Dog(String name, int age) { this.name = name; this.age = age; } }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Rex 3
this.name refers to the field, while name is the parameter. The constructor's job is to copy each parameter into the matching field.

Data Flow

A class defines a blueprint; instances hold state and call methods that resolve to the runtime type's implementation.
1 Define class with fields and methods
↓
2 Instantiate -> constructor sets state
↓
3 Call method -> receives instance
↓
4 Polymorphic call resolves to runtime type
Why this works
The four OOP pillars

Encapsulation hides state behind public methods. Inheritance reuses and extends classes. Polymorphism lets subclasses share an interface. Abstraction hides complexity behind simple interfaces like interfaces and abstract classes.

Method overriding and polymorphism

A subclass can override a parent method:

class Animal {
    void sound() { System.out.println("..."); }
}

class Dog extends Animal {
    @Override
    void sound() { System.out.println("woof"); }
}

Animal a = new Dog();   // polymorphic reference
a.sound();   // woof (runs Dog's version)

The declared type is Animal, but the runtime type is Dog, so the overridden method runs. Java binds these calls dynamically.

Examples

Defining a class

A simple class

class Dog {
    String name;
    int age;

    String speak() {
        return name + " says woof";
    }
}

Dog rex = new Dog();
rex.name = "Rex";
System.out.println(rex.speak());

A class is a blueprint. Fields hold state; methods define behaviour. new creates an instance.

Constructors

Initializing objects

class Dog {
    String name;
    int age;

    Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

Dog rex = new Dog("Rex", 3);

A constructor has the same name as the class and no return type. this refers to the current instance.

Inheritance

Subclassing

class Animal {
    void eat() { System.out.println("eating"); }
}

class Dog extends Animal {
    void bark() { System.out.println("woof"); }
}

Dog d = new Dog();
d.eat();   // inherited
d.bark();  // own

extends inherits fields and methods. Subclasses can add new behaviour or override existing methods.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Exception Handling
Previous Lesson
Exception Handling
Next Lesson
Collections Framework
Collections Framework →