Open In App

Method Overriding with Access Modifier

Last Updated : 15 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisites: Method Overriding in java and Access Modifier in Java Method Overriding In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by its super-class or parent class. When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class. Method overriding is one of the ways by which java achieve Run Time Polymorphism. The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed. In other words, it is the type of the object being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed. Access Modifiers As the name suggests, access modifiers in Java help to restrict the scope of a class, constructor, variable, method or data member. There are four types of access modifiers available in java:

  • Default – No keyword required
  • Private
  • Protected
  • Public

Method Overriding with Access Modifiers There is Only one rule while doing Method overriding with Access modifiers i.e.

If you are overriding any method, overridden method (i.e. declared in subclass) must not be more restrictive.

Access modifier restrictions in decreasing order:

  • private
  • default
  • protected
  • public

i.e. private is more restricted then default and default is more restricted than protected and so on. Example 1: 

Java




class A {
    protected void method()
    {
        System.out.println("Hello");
    }
}
 
public class B extends A {
 
    // Compile Time Error
    void method()
    {
        System.out.println("Hello");
    }
 
    public static void main(String args[])
    {
        B b = new B();
        b.method();
    }
}


Output:

Compile Time Error

Note: In the above Example Superclass class A defined a method whose access modifier is protected. While doing method overriding in SubClass Class B we didn’t define any access modifier so Default access modifier will be used. By the rule, Default is more restricted then Protected so this program will give compile time error. Instead of default, we could’ve used public which is less restricted then protected. Example 2: 

Java




class A {
    protected void method()
    {
        System.out.println("Hello");
    }
}
 
public class B extends A {
    public void method()
    {
        System.out.println("Hello");
    }
 
    public static void main(String args[])
    {
        B b = new B();
        b.method();
    }
}


Output:

Hello

Note: In the above Example Superclass class A defined a method whose access modifier is protected. While doing method overriding in SubClass Class B we define access modifier as Public. Because Public access modifier is less restricted than Protected hence this program compiles successfully.



Similar Reads

Public vs Protected Access Modifier in Java
Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by using an appropriate keyword in java called access mo
4 min read
Public vs Protected vs Package vs Private Access Modifier in Java
Whenever we are writing our classes we have to provide some information about our classes to the JVM like whether this class can be accessible from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not etc. we can specify this information by using an appropriate keyword in java called access mo
7 min read
Private vs Final Access Modifier in Java
Whenever we are writing our classes we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not etc. we can specify this information by using an appropriate keyword in java called access modi
3 min read
Private vs Protected vs Final Access Modifier in Java
Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by using an appropriate keyword in java called access mo
5 min read
Protected vs Final Access Modifier in Java
Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by using an appropriate keyword in java called access mo
5 min read
Abstract vs Public Access Modifier in Java
Access Modifier in Java is the reserved keyword used to define the scope of a class, variable, and methods. It also tells us about that whether child class creation is possible or not or whether object creation is possible or not. Abstract Access Modifier is a modifier applicable only for classes and methods but not for variables. If we declare any
4 min read
Final vs Static vs Abstract Non-Access Modifier
Modifiers are specific keywords present in Java using that we can make changes to the characteristics of a variable, method, or class and limit its scope. Java programming language has a rich set of Modifiers. Modifiers in Java are divided into two types Access ModifiersNon-Access modifiers Non-access modifiers provide information about the charact
7 min read
Java - Final vs Static Access Modifier
Final keyword is used in different contexts. First of all, final is a non-access modifier applicable only to a variable, a method, or a class. Following are different contexts where final is used. While the static keyword in Java is mainly used for memory management. The static keyword in Java is used to share the same variable or method of a given
5 min read
Difference Between Method Overloading and Method Overriding in Java
The differences between Method Overloading and Method Overriding in Java are as follows: Method Overloading Method Overriding Method overloading is a compile-time polymorphism.Method overriding is a run-time polymorphism.Method overloading helps to increase the readability of the program.Method overriding is used to grant the specific implementatio
4 min read
Modifier isNative(mod) method in Java with Examples
The isNative(mod) method of java.lang.reflect.Modifier is used to check if the integer argument includes the native modifier or not. If this integer parameter represents native type Modifier then method returns true else false. Syntax: public static boolean isNative(int mod) Parameters: This method accepts a integer names as mod represents a set of
2 min read
Practice Tags :