- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between method hiding and method overriding in Java?
When super class and the sub class contains same instance methods including parameters, when called, the super class method is overridden by the method of the sub class.
WIn this example super class and sub class have methods with same signature (method name and parameters) and when we try to invoke this method from the sub class the sub class method overrides the method in super class and gets executed.
Example
class Super{ public void sample(){ System.out.println("Method of the Super class"); } } public class MethodOverriding extends Super { public void sample(){ System.out.println("Method of the Sub class"); } public static void main(String args[]){ MethodOverriding obj = new MethodOverriding(); obj.sample(); } }
Output
Method of the Sub class
When super class and the sub class contains same methods including parameters, and if they are static and, when called, the super class method is hidden by the method of the sub class.
Example
class Super{ public static void sample(){ System.out.println("Method of the Super class"); } } public class MethodHiding extends Super { public static void sample(){ System.out.println("Method of the Sub class"); } public static void main(String args[]){ MethodHiding obj = new MethodHiding(); obj.sample(); } }
Output
Method of the Sub class
Advertisements