How do I use the “return” keyword in Java?

The return keyword is used to return from a method when its execution is complete. When a return statement is reached in a method, the program returns to the code that invoked it.

A method can return a value or reference type or does not return a value. If a method does not return a value, the method must be declared void and it doesn’t need to contain a return statement.

If a method declare to return a value, then it must use the return statement within the body of method. The data type of the return value must match the method’s declared return type.

package org.kodejava.basic;

public class ReturnDemo {

    public static void main(String[] args) {
        int z = ReturnDemo.calculate(2, 3);
        System.out.println("z = " + z);

        Dog dog = new Dog("Spaniel", "Doggie");
        System.out.println(dog.getDog());
    }

    public static int calculate(int x, int y) {
        // return an int type value
        return x + y;
    }

    public void print() {
        System.out.println("void method");

        // it does not need to contain a return statement, but it
        // may do so
        return;
    }

    public String getString() {
        return "return String type value";

        // try to execute a statement after return a value will
        // cause a compile-time error.
        //
        // String error = "error";
    }
}

class Dog {
    private String breed;
    private String name;

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

    public Dog getDog() {
        // return Dog type
        return this;
    }

    public String toString() {
        return "breed: " + breed.concat("name: " + name);
    }
}

Sometimes learning Java can be challenging, but the main thing is to remember that you can find any help on our website. Just be dedicated and passionate about what you do. If you are still at university, a pay for essay service EssayWritingService can be of any assistance for you.

How do I use the “this” keyword in Java?

Every instance method has a variable with the name this that refers to the current object for which the method is being called. You can refer to any member of the current object from within an instance method or a constructor by using this keyword.

Each time an instance method is called, the this variable is set to reference the particular class object to which it is being applied. The code in the method will then relate to the specific members of the object referred to by this keyword.

package org.kodejava.basic;

public class RemoteControl {
    private String channelName;
    private int channelNum;
    private int minVolume;
    private int maxVolume;

    RemoteControl() {
    }

    RemoteControl(String channelName, int channelNum) {
        // use "this" keyword to call another constructor in the 
        // same class
        this(channelName, channelNum, 0, 0);
    }

    RemoteControl(String channelName, int channelNum, int minVol, int maxVol) {
        this.channelName = channelName;
        this.channelNum = channelNum;
        this.minVolume = minVol;
        this.maxVolume = maxVol;
    }

    public static void main(String[] args) {
        RemoteControl remote = new RemoteControl("ATV", 10);

        // when the following line is executed, the variable in
        // changeVolume() is referring to remote object.
        remote.changeVolume(0, 25);
    }

    public void changeVolume(int x, int y) {
        this.minVolume = x;
        this.maxVolume = y;
    }
}

How do I invoke superclass constructor?

This example shows you how to use the super keyword to call a superclass constructor. The Female class constructor calls its superclass constructor and initializes its own initialization parameters. The call to the superclass constructor must be done in the first line of the constructor in the subclass.

package org.kodejava.example.fundamental;

public class Human {
    private String gender;
    private int age;

    public Human(String gender) {
        this.gender = gender;
    }
}

To call a superclass constructor we call super(). In the case below we call the superclass constructor with one string variable as a parameter.

package org.kodejava.example.fundamental;

public class Female extends Human {
    private String hairStyle;

    public Female(String hairStyle, String gender) {
        super(gender);
        this.hairStyle = hairStyle;
    }
}

How do I create and implement abstract class?

Abstract class is a class that have one or more methods declared, but not defined. Abstract class cannot have instances. This class uses in inheritance to take advantage of polymorphism. To declare that a class is abstract, use the abstract keyword in front of the class keyword in the class definition.

Methods in abstract class that have no definition are called abstract methods. The declaration for an abstract method ends with a semicolon and you specify the method with the abstract keyword to identify it as such. The implementation is left to the sub classes.

package org.kodejava.example.fundamental;

public abstract class Animal {
    private String species;

    public Animal(String species) {
        this.species = species;
    }

    public abstract void makeASound();

    public String getSpecies() {
        return species;
    }

    public static void main(String[] args) {
        Animal pig = new Pig("Warthog");
        pig.makeASound();
    }
}

The Pig class extends the Animal class. Because the Animal class contains an abstract method makeASound() the Pig class must implements this method or else the Pig will also become an abstract class.

package org.kodejava.example.fundamental;

public class Pig extends Animal {

    public Pig(String species) {
        super(species);
    }

    @Override
    public void makeASound() {
        System.out.println("oink oink");
    }
}

How do I override a method in Java?

If a subclass has a method that have the same signature with a method in its superclass, then it’s an overriding method. Override inherited methods allow subclasses to provide specialized implementation for those methods. The overriding method has the same name, number and type of arguments, and return value as the method it overrides.

The overriding method can have a different throws clause as long as it doesn’t specify any types not specified by the throws clause in the overridden method. Also, the access specifier for the overriding method can allow more but not less access than the overridden method.

For example, a protected method in the superclass can be made public but not private. A subclass cannot override methods that are declared final in the superclass. A subclass must override methods that are declared abstract in the superclass, or the subclass itself must be abstract.

package org.kodejava.example.fundamental;

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;
    }
}

In the Truck class we override the getCarInfo() method to provide more information about the truck object. We can also add an @Override annotation to an overriding method.

package org.kodejava.example.fundamental;

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();
    }
}