How do I overload methods in Java?

Method overloading allows a method to use the same name or identifier as the method name as long as the argument list is different. Java can differentiate each method by their method signatures. For example to print some value you can create a print method that accept different kind of objects or values as its parameters.

Overloaded method is differentiated by the number and the type of argument they accept. The print(String string) and print(int number) are distinct and unique due to their argument type.

The compiler does not count a return type as a method differentiator. So it is not legal to create a method with the same name, the same number, the same type of argument but with a different return type.

package org.kodejava.basic;

public class OverloadedExample {
    public void print(Object object) {
        System.out.println("object = " + object);
    }

    public void print(String string) {
        System.out.println("string = " + string);
    }

    public void print(int number) {
        System.out.println("number = " + number);
    }

    public void print(float number) {
        System.out.println("number = " + number);
    }

    public void print(double number) {
        System.out.println("number = " + number);
    }
}

How do I use the super keyword?

When a class extends from other class, the class or usually called as subclass inherits all the accessible members and methods of the superclass. If the subclass overrides a method provided by its superclass, a way to access the method defined in the superclass is through the super keyword.

package org.kodejava.basic;

public class Bike {
    public void moveForward() {
        System.out.println("Bike: Move Forward.");
    }
}

In the ThreeWheelsBike‘s moveForward() method we call the overridden method using the super.moveForward() which will print the message from the Bike class.

package org.kodejava.basic;

public class ThreeWheelsBike extends Bike {
    public static void main(String[] args) {
        Bike bike = new ThreeWheelsBike();
        bike.moveForward();
    }

    @Override
    public void moveForward() {
        super.moveForward();
        System.out.println("Three Wheels Bike: Move Forward.");
    }
}

How do I create an interface in Java?

Interface only contains methods declaration and all its methods are abstract methods. In its most common form, an interface is a group of related methods with empty bodies. To create an interface, use interface keyword in class definition. The file name of interface always the same with the interface name in class definition and the extension is .java.

The RemoteControl interface defines four move methods and a getPosition() methods. These methods have no bodies. If a class implements an interface, the class should implement all the contracts / methods defined by the interface.

package org.kodejava.basic;

public interface RemoteController {
    void moveUp(int n);

    void moveRight(int n);

    void moveDown(int n);

    void moveLeft(int n);

    int[] getPosition();
}

The following snippet show you how to create a class that implements the RemoteController interface.

package org.kodejava.basic;

public class DummyRemoteControl implements RemoteController {
    private int x;
    private int y;

    public DummyRemoteControl(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public static void main(String[] args) {
        RemoteController control = new DummyRemoteControl(0, 0);
        control.moveDown(10);
        control.moveLeft(5);
        System.out.println("X = " + control.getPosition()[0]);
        System.out.println("Y = " + control.getPosition()[1]);
    }

    public void moveUp(int n) {
        x = x + n;
    }

    public void moveRight(int n) {
        y = y + n;
    }

    public void moveDown(int n) {
        x = x - n;
    }

    public void moveLeft(int n) {
        y = y - n;
    }

    public int[] getPosition() {
        return new int[]{x, y};
    }
}

How do I implement interfaces in Java?

To implement an interface, a java class must use implements keyword on its class definition. For example, class A implements interface B. The class definition of class A would look like this:

class A implements B {
}

A class can implements more than one interfaces. For example, class A can implements interface B and interface C:

class A implements B, C {
}

Classes that implements an interface must implements all methods declared in that interface.

package org.kodejava.basic;

public interface Language {

    String getBirthday();
    String getGreeting();

}

The following class is an English implementation of the Language interface.

package org.kodejava.basic;

public class English implements Language {
    public String getBirthday() {
        return "Happy Birthday";
    }

    public String getGreeting() {
        return "How are you?";
    }
}

The following class is an Indonesian implementation of the Language interface.

package org.kodejava.basic;

public class Indonesian implements Language {
    public String getBirthday() {
        return "Selamat Ulang Tahun";
    }

    public String getGreeting() {
        return "Apa kabar?";
    }
}

And here is a snippet that show the interface and classes in action.

package org.kodejava.basic;

public class LanguageDemo {
    public static void main(String[] args) {
        Language language = new English();
        System.out.println(language.getBirthday());
        System.out.println(language.getGreeting());

        language = new Indonesian();
        System.out.println(language.getBirthday());
        System.out.println(language.getGreeting());
    }
}

How do I extend classes in Java?

Inheritance is one of object-oriented programming concepts. This concept allows classes to inherit commonly used state and behavior from other classes. Inheritance is the way to put commonly used states and behaviors into one class and reuse it.

The class that inherits all the attributes from other class is called as sub class. While, the class that inherited is called as superclass. You can use the extends keyword in class definition to inherit from other classes.

When you apply the final keyword to the class declaration you will make the class final, a final class cannot be extended by other class.

For example below we have, a Truck class and a Sedan that derived from a Car class.

package org.kodejava.basic;

public class CarDemo {
    public static void main(String[] args) {
        Car car = new Car();
        car.setBrand("Honda");
        System.out.println("Brand = " + car.getBrand());

        // The setBrand() and getBrand() is inherited from the Car
        // class.
        Truck truck = new Truck();
        truck.setBrand("Ford");
        System.out.println("Brand = " + truck.getBrand());
        int loadCapacity = truck.getLoadCapacity();

        // The setBrand(), getBrand() and setNumberOfSeat methods
        // are is inherited from the Car class.
        Sedan sedan = new Sedan();
        sedan.setBrand("Hyundai");
        System.out.println("Brand = " + sedan.getBrand());
        sedan.setNumberOfSeat(2);
        int gearType = sedan.getGearType();
    }
}

Here the definition of the Car, the Truck and the Sedan classes.

package org.kodejava.basic;

public class Car {
    private String type;
    private String brand;
    private String model;
    private int numberOfSeat;

    public Car() {
    }

    public Car(String type, String brand, String model) {
        this.type = type;
        this.brand = brand;
        this.model = model;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getNumberOfSeat() {
        return numberOfSeat;
    }

    public void setNumberOfSeat(int numberOfSeat) {
        this.numberOfSeat = numberOfSeat;
    }

    public String getCarInfo() {
        return "Type: " + type
                + "; Brand: " + brand
                + "; Model: " + model;
    }
}
package org.kodejava.basic;

public class Truck extends Car {
private int loadCapacity;

    public Truck() {
    }

    public Truck(String type, String brand, String model) {
        super(type, brand, model);
    }

    public int getLoadCapacity() {
        return loadCapacity;
    }

    public void setLoadCapacity(int loadCapacity) {
        this.loadCapacity = loadCapacity;
    }

    @Override
    public String getCarInfo() {
        return "Type: " + getType()
                + "; Brand: " + getBrand()
                + "; Model: " + getModel()
                + "; Load capacity: " + getLoadCapacity();
    }
}
package org.kodejava.basic;

public class Sedan extends Car {
    private int gearType;

    public Sedan() {
    }

    public int getGearType() {
        return gearType;
    }

    public void setGearType(int gearType) {
        this.gearType = gearType;
    }
}