Prerequisite : Overriding in Java, Packages in Java
Packages provide more layer of encapsulation for classes. Thus, visibility of a method in different packages is different from that in the same package.
How JVM find which method to call?
When we run a java program,
- JVM checks the runtime class of the object.
- JVM checks whether the object’s runtime class has overridden the method of the declared class.
- If so, that’s the method called. Otherwise, declared class’s method is called.
Thus, the key point is visible check the visibility of method in different packages.
Following are the cases where we will see method overriding in different packages
1. Private method overriding : In this, access modifier of method we want to override is private.
As private method of parent class is not visible in child class. Thus no overriding takes place here.
If you don’t know how to run this program from terminal then, create Hello.java file and World.java file as specified above and run these commands.
2. Public method overriding : In this, access modifier of method we want to override is public.
Public method is accessible everywhere, hence when printMessage() is called, jvm can find the overridden method and thus call it.
Thus overriding takes place here.
3. Default method overriding
error: printMessage() is not public in Hello; cannot be accessed from outside package
Visibility of a default method is within its package only. Hence it can’t be access outside the package.
Thus, no overriding in this case.
Predict the output of the following program
Visibility of printMessage() is default in package a. Thus, no overriding takes place here.
This article is contributed by Shubham Juneja. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Java | Java Packages | Question 3
- Packages In Java
- Java | Packages | Question 1
- Java | Packages | Question 2
- Overriding equals method in Java
- Overriding toString() in Java
- Overriding in Java
- Exception Handling with Method Overriding in Java
- Difference between Method Overloading and Method Overriding in Java
- Static methods vs Instance methods in Java
- Overriding of Thread class start() method
- Method Overriding with Access Modifier
- Two interfaces with same methods having same signature but different return types
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.io.BufferedWriter class methods in Java
- java.lang.Character class methods | Set 1
- Private and final methods in Java
- Can we override private methods in Java?
- Java Interface methods
- Default Methods In Java 8
Improved By : Akanksha_Rai