Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advanced search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Devaka Cooray
Tim Cooke
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Saloon Keepers:
Piet Souris
Bartenders:
Forum:
Beginning Java
Difference between method overloading and overriding in Java
Baranidharan 321
Greenhorn
Posts: 1
posted 3 months ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hello everyone,
I am learning Java OOP concepts.
Can someone explain the difference
between method overloading and method overriding with a simple example?
Thank you.
Petr Šlechta
Ranch Hand
Posts: 46
1
posted 3 months ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
Overloading (methods have the same name but different arguments):
public class A { public void method(int arg) { } public void method(String arg) { } }
Overriding (the same name and arguments in a child class):
public class A { public void method(int arg) { } } public class B extends A { public void method(int arg) { } }
Campbell Ritchie
Marshal
Posts: 82459
594
posted 3 months ago
Number of slices to send:
Optional 'thank-you' note:
Send
Welcome to the Ranch
Remember, you can only override instance methods. Always use
this annotation
if you are overriding methods; if you make the tiniest mistake, e.g. spelling something wrongly, you will get a compile‑time error.
/* Not an overridden method: compilation will fail */ public class A { public void method(int arg) { } @Override public void method(String arg) { } }
/* Incorrectly overridden method: compilation will fail */ public class B { @Override public String tostring() { return ....; } }
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Help needed to understand Java concepts – Beginner
What is a Balanced Binary Tree?
Best Tutorials on Object Oriented Programming in java
Overloading
Overriding and Overloading Help Needed
More...