{"id":2170,"date":"2020-03-17T18:12:17","date_gmt":"2020-03-17T18:12:17","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=2170"},"modified":"2026-07-31T06:27:10","modified_gmt":"2026-07-31T10:27:10","slug":"method-overriding-java","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/method-overriding-java\/","title":{"rendered":"Method overriding in Java"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><b>Introduction to Method Overriding:<\/b><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Method overriding is a fundamental concept in Java that empowers developers to create more dynamic and flexible applications. By allowing subclasses to provide specific implementations of methods defined in their parent classes, method overriding facilitates code reusability and enhances maintainability. This technique is particularly useful in designing systems where a base class provides generic methods, while subclasses implement specialized behaviors.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">Firstly, let&#8217;s understand the overriding in object-oriented terms. Overriding means override the functionality of an existing method. This is a crucial concept in Java and other object-oriented programming languages, as it allows developers to modify inherited behavior from a parent class.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">If the subclass or child class has the same method as we declared in the superclass or parent class, it is called <a href=\"https:\/\/www.h2kinfosys.com\/blog\/method-overriding-java\/\"><strong>Method overriding in Java<\/strong><\/a>. For example, if a superclass has a method to calculate an area and a subclass implements its own version of this method to calculate a different shape&#8217;s area, that&#8217;s method overriding.<\/span><b>.<\/b><\/li>\n<\/ul>\n\n\n\n<li><span style=\"font-weight: 400;\">For instance, consider a scenario in a graphic application where we have a superclass called <code>Shape<\/code> and subclasses like <code>Circle<\/code> and <code>Square<\/code>. The <code>Shape<\/code> class can have a method <code>draw()<\/code> that is overridden by each subclass to provide specific drawing instructions.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Method overriding not only allows the use of the same method name but also enables polymorphism, which is a core principle of object-oriented programming. This means that a method call will invoke the overridden method in the subclass, even if the call is made on a reference of the superclass type.<\/span><\/li>\n\n\n\n<h3 class=\"wp-block-heading\"><b>Example of Method Overriding:<\/b><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To illustrate method overriding, let&#8217;s examine a simple example:<\/p>\n\n\n\n<li><span style=\"font-weight: 400;\">This mechanism of method overriding contributes significantly to the concept of code reusability. Developers can build upon existing classes to create specialized classes without modifying the existing codebase, thus adhering to the open\/closed principle of software design.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Additionally, method overriding enhances the readability of code. By using the same method name across classes, it is easier for developers to understand the behavior of various objects in the system without having to remember different method names.<\/span><\/li>\n\n\n\n<h3 class=\"wp-block-heading\"><b>Common Use Cases of Method Overriding:<\/b><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Method overriding is widely used in various real-life applications. Below are some common scenarios:<\/p>\n\n\n\n<li><span style=\"font-weight: 400;\">It is crucial to remember that method overriding can lead to unexpected behavior if not carefully managed, especially when dealing with large class hierarchies. Developers should ensure that overridden methods maintain the expected behavior and adhere to the principles of least surprise.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Moreover, testing overridden methods is essential to ensure that the subclass implementations correctly fulfill the intended purpose without breaking existing functionality.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Finally, understanding method overriding is vital for any Java developer aiming to create scalable and maintainable applications. It is a powerful tool that, when used correctly, can greatly enhance the design of a software system.<\/span><\/li>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">UI Frameworks: In user interface frameworks, base classes might define generic behaviors for different UI components, which can be overridden by specific component classes to handle user interactions uniquely.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Game Development: In game development, base classes can represent generic game characters, while subclassing allows different character types to implement unique behavior for actions such as jumping or shooting.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Data Processing: When processing different types of data, a base data processor class can define a method for handling data input\/output, which is overridden by subclasses for specific data types or formats.<\/span><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal { public void sound() { System.<mark class=\"rank-math-highlight\" style=\"background-color: #fee894\"><mark class=\"rank-math-highlight\" style=\"background-color: #fee894\">out.<\/mark><\/mark>println(\"Animal makes sound\"); }}class Dog extends Animal { public void sound() { System.out.println(\"Dog barks\"); }}class Cat extends Animal { public void sound() { System.out.println(\"Cat meows\"); }}public class Test { public static void main(String&#91;] args) { Animal a1 = new Dog(); Animal a2 = new Cat(); a1.sound(); a2.sound(); }}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, the <code>Animal<\/code> class has a method <code>sound()<\/code>. Both <code>Dog<\/code> and <code>Cat<\/code> classes override this method to provide their own specific sounds. When we create instances of <code>Dog<\/code> and <code>Cat<\/code> and call the <code>sound()<\/code> method, it invokes the overridden method in the respective subclasses, demonstrating method overriding in action.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><b>Use of Method Overriding:<\/b><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\"><b>Conclusion:<\/b><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In conclusion, method overriding is a cornerstone of object-oriented programming in Java. It allows for the creation of flexible and reusable code by enabling subclasses to define specific implementations of methods inherited from parent classes. By adhering to the rules and understanding the proper use cases, developers can leverage method overriding to enhance their applications&#8217; functionality and maintainability. As you continue to work with Java, mastering method overriding will undoubtedly contribute to your success as a programmer.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">It is used to give us the implementation of a method that is already provided by its superclass. This allows for more specific functionality in subclasses.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">It is used for runtime <a href=\"https:\/\/www.h2kinfosys.com\/blog\/introduction-to-polymorphism\/\">polymorphism<\/a> (many forms). By calling the overridden method on a subclass object, we can execute the subclass&#8217;s implementation, demonstrating polymorphic behavior.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><b>Rules about the Method Overriding:<\/b><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">First, the method signature, like method name, its parameter list, and also the return type, have to match exactly. This is essential for the Java compiler to recognize the method as an overridden method.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">The method should have the same name as same as the parent class. This naming consistency is what informs Java that it is overriding a method.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">The method must have the same parameter as the same as the parent class has. This ensures that the method can be called using the same arguments as the parent class&#8217;s method.<\/span><\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img fetchpriority=\"high\" decoding=\"async\" width=\"300\" height=\"234\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_15-300x234.png\" alt=\"Method overriding\" class=\"wp-image-2171\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_15-300x234.png 300w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_15.png 494w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"911\" height=\"478\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_16.png\" alt=\"\" class=\"wp-image-2172\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_16.png 911w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_16-300x157.png 300w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_16-768x403.png 768w\" sizes=\"(max-width: 911px) 100vw, 911px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Difference between Method Overloading and Method Overriding:<\/b><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><b>Method Overloading:<\/b><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">It is in the same class, where we have more than one method having the same name but different parameters. This is useful for performing different tasks with the same method name but varying inputs, demonstrating compile-time polymorphism.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><b>Method Overriding:<\/b><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">It is one of the methods which is also defined in the subclass as the same in the<a href=\"https:\/\/en.wikipedia.org\/wiki\/Superclass\" rel=\"nofollow noopener\" target=\"_blank\"> superclass<\/a>. This is essential for creating a specific behavior in the subclass that is different from the superclass.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">In this, the parameters of the method remain the same. This consistency allows for dynamic method dispatch, which is a key feature of Java&#8217;s runtime polymorphism.<\/span><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><b>Note:<\/b><span style=\"font-weight: 400;\"> Method Overriding is mostly used to achieve Run Time Polymorphism. For instance, in our earlier example, the <code>sound()<\/code> method in the <code>Dog<\/code> and <code>Cat<\/code> classes demonstrates this principle. It is essential for creating dynamic and flexible applications where specific behaviors can be executed at runtime based on the object&#8217;s actual class type. Understanding method overriding not only fosters better programming practices but also equips developers with the necessary skills to navigate complex software systems effectively.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1785493739544\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">1.What is method overriding in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Method overriding is an object-oriented programming feature in Java where a subclass provides its own implementation of a method that is already defined in its superclass. This enables runtime polymorphism and allows the subclass to customize inherited behavior.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1785493756606\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">2.What is the difference between method overriding and method overloading?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Method overriding occurs between a superclass and a subclass with the same method signature, enabling runtime polymorphism. Method overloading occurs within the same class (or inherited class) using the same method name but different parameter lists, enabling compile-time polymorphism.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1785493793103\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">3.Can constructors be overridden in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>No. Constructors cannot be overridden because they are not inherited by subclasses. Each class has its own constructors that are called when an object of that class is created.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1785493813567\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">4.Can static methods be overridden?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>No. Static methods belong to the class rather than the object. If a subclass defines a static method with the same signature, it is known as method hiding, not method overriding.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Method overriding in Java is a fundamental concept of object-oriented programming that allows a subclass to redefine the behavior of a method inherited from its superclass. It is the basis of runtime polymorphism, enabling Java applications to execute different implementations of the same method depending on the object&#8217;s actual type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By following Java&#8217;s overriding rules and using the <code>@Override<\/code> annotation, developers can create flexible, maintainable, and reusable code. Method overriding is widely used in real-world applications, frameworks, and APIs to extend functionality, implement dynamic behavior, and support scalable software development.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to Method Overriding: Method overriding is a fundamental concept in Java that empowers developers to create more dynamic and flexible applications. By allowing subclasses to provide specific implementations of methods defined in their parent classes, method overriding facilitates code reusability and enhances maintainability. This technique is particularly useful in designing systems where a base [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2177,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":"","_members_access_role":[],"_members_access_error":""},"categories":[42],"tags":[58,431,430],"class_list":["post-2170","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-java","tag-method-overloading","tag-method-overriding"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=2170"}],"version-history":[{"count":1,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2170\/revisions"}],"predecessor-version":[{"id":43870,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2170\/revisions\/43870"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/2177"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=2170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=2170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=2170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}