Java

method overriding in java with example using NetBeans

Description:

Method Overriding in java with examples using NetBeans:- Are you using overrides? This is useful when you want to modify and use a method in an existing class. It’s convenient because you only need to rewrite the methods you want to change, without having to rewrite member variables or other methods.

So in This article, we will discuss the following method overriding topics

  • What is a method override in java
  • Using method overriding in java
  • how to use super
  • Rules for using method overriding in java

From the basic content, we will also explain the applied content. This time, I will explain how to use overrides in an easy-to-understand manner!



What is a method override in java?

Overriding means redefining a superclass method with the same method name in a subclass when inheriting a class. When overriding, the method names must be the same, and the method arguments must be the same number and order.

The superclass is the inherited parent class, and the subclass is the inherited class. Also, by using the “super” clause, it is possible to use the method defined in the superclass without modification even if it is overridden.

Benefits of using method Overriding in java

Use overrides in inherited subclass methods. First of all, the merit of inheritance is that it can be reused and the amount of description to be written can be reduced. If you need to change, you can change less.

Thanks to overriding, when a method needs to be changed, there are fewer things to change. You can reuse class member variables as they are, and redefine only the methods you want to change.

It realizes the idea of ​​polymorphism (diversity) that “you can freely change what you want to change while reusing it”.

Difference between overload and override in java

There is a similar term overload. I’m often mistaken, so let me explain.

Overloading means defining methods with the same method name but with different numbers and order of arguments. The override explained this time was to redefine a method with the same method name and the same number and order of arguments in the inherited subclass.


How to Use method overriding in java program

Let’s see how to use method overriding using sample code.

package com.mycompany.javabasics;

/**
 *
 * @author Fawadkhan
 */
// super class
class ClassSuper {

    protected int num1 = 100;
    protected int num2 = 2;

    public void Cal() {
        System.out.println("Number1 + Number2 = " + (num1 + num2));
    }
}
// subclass

class ClassSub extends ClassSuper {

    @Override
    public void Cal() {
        System.out.println("Number1 / Number2 = " + (num1 / num2)); // change processing   
    }
}

public class MethodOverrideExample {

    public static void main(String[] args) {
        ClassSub obj = new ClassSub(); //create ClassSub object  
        obj.Cal(); // calling method
    }

}

output:

method overriding in java

In this example, the ClassSub class inherits from the ClassSuper class. The processing is changed by overriding the Cal method of the super class ClassSuper class in the ClassSub class.

Note that “@Override” is written before redefining the Cal method in the subclass. 


How to use super in method overriding in java

By using the “super” clause, you can use the superclass method as it is even if it is overridden in the subclass.

Let’s actually check with the sample code.

package com.mycompany.javabasics;

/**
 *
 * @author Fawadkhan
 */
// super class
class ClassSuper {

    protected int num1;
    protected int num2;

    public ClassSuper(int num1, int num2) {
        this.num1 = num1;
        this.num2 = num2;
    }

    public void Cal() {
        System.out.println("Number1 + Number2 = " + (num1 + num2));
    }
}
// subclass

class ClassSub extends ClassSuper {

    public ClassSub(int num1, int num2) {
        super(num1, num2);
    }

    @Override
    public void Cal() {
        System.out.println("Number1 / Number2 = " + (num1 / num2)); // change processing   
        super.Cal(); // Same processing as super class 
    }
}

public class MethodOverrideExample {

    public static void main(String[] args) {
        ClassSub obj = new ClassSub(100, 5);
        obj.Cal();

    }

}

output:

method overriding in java

The ClassSub class inherits from the ClassSuper class in this example as well. The processing is changed by overriding the Cal method of the super class ClassSuper class in the ClassSub class. After that, we use the “super” clause to call the Cal method, and the result matches that of the Cal method of the super class, ClassSuper class.

By using the “super” clause like this, you can use the superclass method as it is even if it is overridden in the subclass. Also, the example uses a constructor to assign values.

Superclass constructors must also be written in subclasses, but can be written concisely by using the “super” clause and can be used as is.


Rules for using Method Overriding in java

Overriding is convenient because you can freely change the methods you want to change while reusing the superclass, but there are some rules that must be followed when using it. I will explain in detail.

return type and arguments

As mentioned before, overrides must have the same class name and the same number and order of arguments. Also, the return types of the methods must be the same.

@Override (annotation)

In the sample code introduced, “@Override” is written before the method to be redefined by overriding. Although commented out, this is called an annotation.

Annotations are written to declare overriding. By writing this, if the superclass does not have a method with the same name, the compiler will issue an error message, so it is recommended to add it.



static modifier

A static method is a class-specific method and cannot be overridden. 

private modifier

Methods overridden in subclasses cannot be granted access levels that are more restrictive than those specified for superclass methods. For example, if a superclass method is specified as protected, it cannot be specified as private in the subclass.

Please be careful!

For more information about access modifiers, and access levels read this article.

abstract modifier

If a superclass method has the abstract modifier, it must be overridden in the subclass. If not overridden, the entire subclass becomes an abstract class.

final modifier

If a superclass method has the final modifier, it cannot be overridden in a subclass.

specifying exception class in throws

If you use throws in a superclass method to throw an exception, you need to be careful with overriding methods in subclasses. The overriding method cannot specify exception classes other than those specified in throws of the superclass method.

Fawad

Shahzada Fawad

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button