Java - Final - Methods

Java

In this tutorial we will learn about final methods in Java programming language.

In the previous tutorial we learned about final variables to create constants. Feel free to check that out.

We know that a child class inherits all the inheritable methods and variables from the parent class.

And in the method overridding tutorial we learned that a child class can override inherited methods of the parent class.

But in some scenarios we may not want the child class to override the methods of the parent class. And this is were we use the final methods.

The final methods

If we don't want a method to be overridden then we set the modifier of the method to final.

Syntax

class MyClass {
  
  final type myMethod(param_list) {
    // some code goes here...
  }
}

Where MyClass is the name of the class and it has a method by the name myMethod.

The return type of the method is denoted by type.

The modifier is set to final so, any class inheriting this class can't override the method.

Example #1

In the following example we have the parent class or superclass Hello having a greetings method that is marked final.

So, the child class or subclass Awesome won't be able to override the inherited greetings method.

class Hello {

  // this method is marked as final
  // so any child class inheriting
  // this class won't be able to
  // override this method
  final void greetings() {
    System.out.println("Hello World");
  }
}

class Awesome extends Hello {
  
  // overriding of greetings method not allowed
  /**
   * void greetings() {
   *   System.out.println("Action");
   * }
   */
  
  void message() {
    System.out.println("Message");
  }
}

// main class
public class Example {
  public static void main(String[] args) {

    // creating an object
    Awesome obj = new Awesome();

    // calling methods
    obj.greetings();
    obj.message();
  }
}

Output:

$ javac Example.java 
$ java Example
Hello World
Message

If we try to override the final method then we will get the following error.

$ javac Example.java 
Example.java:15: error: greetings() in Awesome cannot override greetings() in Hello
  void greetings() {
       ^
  overridden method is final
1 error

Example #2

In the following example we have a class Box and it has a final method getVolume which can't be overridden.

Then we have another class GiftBox that inherits the Box class.

We are using the super keyword to call the constructor of the parent class and initialise the dimensions of the GiftBox.

class Box {

  // member variables
  private double length;
  private double width;
  private double height;

  // constructor
  Box() {
    this.length = 0;
    this.width = 0;
    this.height = 0;
  }

  // constructor with parameters
  Box(double length, double width, double height) {
    this.length = length;
    this.width = width;
    this.height = height;
  }

  // this method is set as final
  // so it can't be overridden in
  // the child class
  final double getVolume() {
    return this.length * this.width * this.height;
  }
}

// this class is inheriting
// the Box class
class GiftBox extends Box {

  // member variables
  private double price;
  
  // constructor
  GiftBox() {
    // calling the constructor
    // of the parent class Box
    super();

    // setting the price
    this.price = 0;
  }

  // constructor with parameters
  GiftBox(double price, double length, double width, double height) {
    
    // calling the constructor
    // of the parent class Box
    super(length, width, height);

    // setting the price
    this.price = price;
  }

  // the getVolume method is inherited
  // but we can't override it
  // as it is a final method
  // in the parent class Box
}

// main class
public class Example {
  public static void main(String[] args) {

    // instantiate object
    GiftBox obj = new GiftBox(99, 5, 4, 3);

    // calling the method
    double volume = obj.getVolume();

    // output
    System.out.println("Volume of the GiftBox: " + volume);
  }
}

Output:

$ javac Example.java 
$ java Example
Volume of the GiftBox: 60.0

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT