{"id":1439,"date":"2022-03-04T05:44:39","date_gmt":"2022-03-04T05:44:39","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1439"},"modified":"2022-03-04T05:44:39","modified_gmt":"2022-03-04T05:44:39","slug":"overridden-methods-of-the-superclass","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/04\/overridden-methods-of-the-superclass\/","title":{"rendered":"Overridden Methods of the superclass"},"content":{"rendered":"\n<p>If methods with the same name are defined in both superclass and subclass, the method in the subclass overrides the method in the superclass. This is called&nbsp;<a href=\"https:\/\/www.programiz.com\/java-programming\/method-overriding\">method overriding<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1: Method overriding<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n\n  \/\/ overridden method\n  public void display(){\n    System.out.println(\"I am an animal\");\n  }\n}\n\nclass Dog extends Animal {\n\n  \/\/ overriding method\n  @Override\n  public void display(){\n    System.out.println(\"I am a dog\");\n  }\n\n  public void printMessage(){\n    display();\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n    Dog dog1 = new Dog();\n    dog1.printMessage();\n  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>I am a dog\n<\/samp><\/code><\/pre>\n\n\n\n<p>In this example, by making an object&nbsp;<var>dog1<\/var>&nbsp;of&nbsp;<var>Dog<\/var>&nbsp;class, we can call its method&nbsp;<var>printMessage()<\/var>&nbsp;which then executes the&nbsp;<code>display()<\/code>&nbsp;statement.<\/p>\n\n\n\n<p>Since&nbsp;<code>display()<\/code>&nbsp;is defined in both the classes, the method of subclass&nbsp;<var>Dog<\/var>&nbsp;overrides the method of superclass&nbsp;<var>Animal<\/var>. Hence, the&nbsp;<code>display()<\/code>&nbsp;of the subclass is called.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If methods with the same name are defined in both superclass and subclass, the method in the subclass overrides the method in the superclass. This is called&nbsp;method overriding. Example 1: Method overriding Output In this example, by making an object&nbsp;dog1&nbsp;of&nbsp;Dog&nbsp;class, we can call its method&nbsp;printMessage()&nbsp;which then executes the&nbsp;display()&nbsp;statement. Since&nbsp;display()&nbsp;is defined in both the classes, the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[573],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1439"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1439"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1439\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}