{"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":"2024-11-08T13:03:08","modified_gmt":"2024-11-08T07:33:08","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":"<h2><b>Introduction to Method Overriding:<\/b><\/h2>\n<ul>\n<li style=\"font-weight: 400;\"><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.<\/span><\/li>\n<li style=\"font-weight: 400;\"><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><\/span><b>.<\/b><\/li>\n<\/ul>\n<h3><b>Use of Method Overriding:<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It is used to give us the implementation of a method that is already provided by its superclass.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It is used for runtime <a href=\"https:\/\/www.h2kinfosys.com\/blog\/introduction-to-polymorphism\/\">polymorphism<\/a> (many forms).<\/span><\/li>\n<\/ul>\n<h3><b>Rules about the Method Overriding:<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">First, the method signature, like method name, its parameter list, and also the return type, have to match exactly.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The method should have the same name as same as the parent class.<\/span><\/li>\n<li style=\"font-weight: 400;\">The method must have the same parameter as the same as the parent class has.<\/li>\n<\/ul>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-2171 size-medium\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_15-300x234.png\" alt=\"Method overriding\" width=\"300\" height=\"234\" 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\" \/><\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-2172 aligncenter\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_16.png\" alt=\"\" width=\"911\" height=\"478\" 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\" \/><\/p>\n<h2><b>Difference between Method Overloading and Method Overriding:<\/b><\/h2>\n<h3><b>Method Overloading:<\/b><\/h3>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><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<\/span><span style=\"font-weight: 400;\">.\u00a0 \u00a0<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3><b>Method Overriding:<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\"><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:\/\/www.h2kinfosys.com\/blog\/nested-classes-in-java\/\">superclass<\/a>.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">In this, the parameters of the method remain the same.\u00a0 \u00a0\u00a0<\/span><\/li>\n<\/ul>\n<p><b>Note:<\/b><span style=\"font-weight: 400;\"> Method Overriding is mostly used to achieve Run Time Polymorphism.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to Method Overriding: Firstly, let&#8217;s understand the overriding in object-oriented terms. Overriding means override the functionality of an existing method. If the subclass or child class has the same method as we declared in the superclass or parent class, it is called Method overriding in Java. Use of Method Overriding: It is used to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2177,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"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"],"_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":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2170\/revisions"}],"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}]}}