Overriding vs Hiding JAVA Methods

Anna Scott
5 min readSep 9, 2021

--

This post is about hiding and overriding methods and it can not be done without divulging into the notion of inheritance. Inheritance is one of the four concepts of Object Oriented Programming (OOP) in JAVA (abstraction, encapsulation, and polymorphism are the other three). Inheritance allows us to create new classes by building on existing classes. The child class inherits public and protected members of the parent class, and if classes in the same package default members are also inherited by child class.

package com.company;

public class OverrideVsHide {
public int number;
String name;

protected int returnNumber() {
return number;
}

void printGreeting() {
System.out.println("Hi");
}
}

class ChildClass extends OverrideVsHide {

public static void main(String... input) {
ChildClass example = new ChildClass();
//inheriting instance variable
System.out.println("number = " + (example.number));
//Inheriting instance method
System.out.println("Result of method = " + example.returnNumber());
//inheriting default method in the same package
example.printGreeting();
//inheriting default variable in the same package
System.out.println(example.name);
}
}

In the above snippet, JAVA’s reserved word “extends” tells us that ChildClass inherrits from OverrideVsHide class. We have public, protected and default members. The default members are available to ChildClass because both classes are in the same package. The result of the above program is following:

number = 0
Result of method = 0
Hi
null

Inheritance allows us to override parent methods — provide a new implementation while keeping the name of the method and list of parameters.

Take a look at the method that is defined in the parent class:

void calculateSum(int a, int b) {
System.out.println("result of addition is " + (a + b));
}

Calling it on an instance of the child class :

example.calculateSum(1,1);

would produce the result:

result of addition is 2

Now we override this method in a child class to show the binary result of addition:

void calculateSum(int a, int b) {
System.out.println("binary result of addition is " + Integer.toBinaryString(a + b));
}

Now our program following output:

binary result of addition is 10

When we override method, we need to ensure that child method that overrides parent method has the same name and method parameters, overriding method has the same or wider accessibility, does not throw wider or new checked exception, and its return value is of the same or covariant type.

If we define a static method in our parent class and try to override it in a child class we get a hidden parent method:

Static method in parent class:

static void personalizedGreeting(String name) {
System.out.println(name + " loves to code");
}

Method in the child class that hides the method in the parent class:

static void personalizedGreeting(String name) {
System.out.println(name + " loves JAVA");
}

When we hide method, we need to make sure that the method in the child class has the same signature as the method it hides, is at least as accessible, is not throwing wider or new checked exception, has a return value of the same or covariant type, and it must be marked as static.

Calling the overriding method :

personalizedGreeting("Anna");

Produces :

Anna loves JAVA

We need to use the parent class name in order to call method in parent class:

OverrideVsHide.personalizedGreeting("Anna");

This will produce:

Anna loves to code

Overridden and hidden methods exhibit different behaviors. At runtime when we override method, instance calls overriden/child’s version of method. The overridden method in the child class replaces the parent method in calls defined in both the parent and child. We define a method in a parent class and then the other method that calls the one that we are going to override in a child class:

//defining method in a parent class
void calculateSum(int a, int b) {
System.out.println("result of addition is " + (a + b));
}
//calling method in parent class by another method from parent
void printResult() {calculateSum(1,1);}

With the code:

//calling overriden method
example.calculateSum(1,1);

//method call by the method in parent
example.printResult();

Our result is:

binary result of addition is 10
binary result of addition is 10

Take a look at complete code that produced the above result:

public class OverrideVsHide {    //defining method in a parent class
void calculateSum(int a, int b) {
System.out.println("result of addition is " + (a + b));
}
//calling method in parent class by another method from parent
void printResult() {calculateSum(1,1);}

}

class ChildClass extends OverrideVsHide {

public static void main(String... input) {

ChildClass example = new ChildClass();


//calling overriden method
example.calculateSum(1,1);

//method call by the method in parent
example.printResult();

}

//Overriding method calculateSum declaread in parent class
void calculateSum(int a, int b) {
System.out.println("binary result of addition is " + Integer.toBinaryString(a + b));
}
}

With the hiden static method what method would be called depends on whether it is invoked from parent or child class. In our parent class, we are going change our calculateSum to be a static method, and call it from a different method:

static void calculateSum(int a, int b) {
System.out.println("result of addition is " + (a + b));
}

void printResult() {calculateSum(1,1);}

Let’s also hide that static method in our child class:

static void calculateSum(int a, int b) {
System.out.println("binary result of addition is " + Integer.toBinaryString(a + b));
}

After calls:

//calling static child method
calculateSum
(1,1);

//calling hiden method
example.printResult();

The result:

binary result of addition is 10
result of addition is 2

Take a look at the complete code for the above result:

public class OverrideVsHide {    //creating static method in parent class    
static void calculateSum(int a, int b) {
System.out.println("result of addition is " + (a + b));
}
//calling static method by another method in static class
void printResult() {
calculateSum(1, 1);
}


}

class ChildClass extends OverrideVsHide {

public static void main(String... input) {

ChildClass example = new ChildClass();

//calling static child method
calculateSum
(1, 1);

//calling hiden method
example.printResult();

}

static void calculateSum(int a, int b) {
System.out.println("binary result of addition is " + Integer.toBinaryString(a + b));
}
}

Hidden and overridden methods are fundamentally different, the above examples demonstrate the difference in their behavior.

All the code could be found here:

Happy coding my friends!

--

--

Anna Scott
Anna Scott

Written by Anna Scott

Java, Android developer. Some Python.

No responses yet