{"id":1204,"date":"2022-02-25T16:47:24","date_gmt":"2022-02-25T16:47:24","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1204"},"modified":"2022-02-25T16:47:24","modified_gmt":"2022-02-25T16:47:24","slug":"java-method-overriding","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/25\/java-method-overriding\/","title":{"rendered":"Java Method Overriding"},"content":{"rendered":"\n<p>In this tutorial, we will learn about method overriding in Java with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">In the last tutorial, we learned about inheritance. Inheritance is an OOP property that allows us to derive a new class (subclass) from an existing class (superclass). The subclass inherits the attributes and methods of the superclass.<\/p>\n\n\n\n<p>Now, if the same method is defined in both the superclass and the subclass, then the method of the subclass class overrides the method of the superclass. This is known as method overriding.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-1\">Example 1: Method Overriding<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n   public void displayInfo() {\n      System.out.println(\"I am an animal.\");\n   }\n}\n\nclass Dog extends Animal {\n   @Override\n   public void displayInfo() {\n      System.out.println(\"I am a dog.\");\n   }\n}\n\nclass Main {\n   public static void main(String&#91;] args) {\n      Dog d1 = new Dog();\n      d1.displayInfo();\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 the above program, the&nbsp;<code>displayInfo()<\/code>&nbsp;method is present in both the&nbsp;<var>Animal<\/var>&nbsp;superclass and the&nbsp;<var>Dog<\/var>&nbsp;subclass.<\/p>\n\n\n\n<p>When we call&nbsp;<code>displayInfo()<\/code>&nbsp;using the&nbsp;<var>d1<\/var>&nbsp;object (object of the subclass), the method inside the subclass&nbsp;<var>Dog<\/var>&nbsp;is called. The&nbsp;<code>displayInfo()<\/code>&nbsp;method of the subclass overrides the same method of the superclass.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/method-overriding-in-java.png\" alt=\"Working of method overriding in Java.\" title=\"Method Overriding in Java\"\/><\/figure>\n\n\n\n<p>Notice the use of the&nbsp;<code>@Override<\/code>&nbsp;annotation in our example. In Java, annotations are the metadata that we used to provide information to the compiler. Here, the&nbsp;<code>@Override<\/code>&nbsp;annotation specifies the compiler that the method after this annotation overrides the method of the superclass.<\/p>\n\n\n\n<p>It is not mandatory to use&nbsp;<code>@Override<\/code>. However, when we use this, the method should follow all the rules of overriding. Otherwise, the compiler will generate an error.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"rules\">Java Overriding Rules<\/h2>\n\n\n\n<ul><li>Both the superclass and the subclass must have the same method name, the same return type and the same parameter list.<\/li><li>We cannot override the method declared as&nbsp;<code>final<\/code>&nbsp;and&nbsp;<code>static<\/code>.<\/li><li>We should always override abstract methods of the superclass (will be discussed in later tutorials).<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"super\">super Keyword in Java Overriding<\/h2>\n\n\n\n<p>A common question that arises while performing overriding in Java is:<\/p>\n\n\n\n<p><strong>Can we access the method of the superclass after overriding?<\/strong><\/p>\n\n\n\n<p>Well, the answer is&nbsp;<strong>Yes<\/strong>. To access the method of the superclass from the subclass, we use the&nbsp;<code>super<\/code>&nbsp;keyword.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Use of super Keyword<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n   public void displayInfo() {\n      System.out.println(\"I am an animal.\");\n   }\n}\n\nclass Dog extends Animal {\n   public void displayInfo() {\n      super.displayInfo();\n      System.out.println(\"I am a dog.\");\n   }\n}\n\nclass Main {\n   public static void main(String&#91;] args) {\n      Dog d1 = new Dog();\n      d1.displayInfo();\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 an animal.\nI am a dog.\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, the subclass&nbsp;<var>Dog<\/var>&nbsp;overrides the method&nbsp;<code>displayInfo()<\/code>&nbsp;of the superclass&nbsp;<var>Animal<\/var>.<\/p>\n\n\n\n<p>When we call the method&nbsp;<code>displayInfo()<\/code>&nbsp;using the&nbsp;<var>d1<\/var>&nbsp;object of the&nbsp;<var>Dog<\/var>&nbsp;subclass, the method inside the&nbsp;<var>Dog<\/var>&nbsp;subclass is called; the method inside the superclass is not called.<\/p>\n\n\n\n<p>Inside&nbsp;<code>displayInfo()<\/code>&nbsp;of the&nbsp;<var>Dog<\/var>&nbsp;subclass, we have used&nbsp;<code>super.displayInfo()<\/code>&nbsp;to call&nbsp;<code>displayInfo()<\/code>&nbsp;of the superclass.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>It is important to note that constructors in Java are not inherited. Hence, there is no such thing as constructor overriding in Java.<\/p>\n\n\n\n<p>However, we can call the constructor of the superclass from its subclasses. For that, we use\u00a0<code>super()<\/code>. <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"access-specifier\">Access Specifiers in Method Overriding<\/h2>\n\n\n\n<p>The same method declared in the superclass and its subclasses can have different access specifiers. However, there is a restriction.<\/p>\n\n\n\n<p>We can only use those access specifiers in subclasses that provide larger access than the access specifier of the superclass. For example,<\/p>\n\n\n\n<p>Suppose, a method&nbsp;<code>myClass()<\/code>&nbsp;in the superclass is declared&nbsp;<code>protected<\/code>. Then, the same method&nbsp;<code>myClass()<\/code>&nbsp;in the subclass can be either&nbsp;<code>public<\/code>&nbsp;or&nbsp;<code>protected<\/code>, but not&nbsp;<code>private<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3: Access Specifier in Overriding<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n   protected void displayInfo() {\n      System.out.println(\"I am an animal.\");\n   }\n}\n\nclass Dog extends Animal {\n   public void displayInfo() {\n      System.out.println(\"I am a dog.\");\n   }\n}\n\nclass Main {\n   public static void main(String&#91;] args) {\n      Dog d1 = new Dog();\n      d1.displayInfo();\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 the above example, the subclass&nbsp;<var>Dog<\/var>&nbsp;overrides the method&nbsp;<code>displayInfo()<\/code>&nbsp;of the superclass&nbsp;<var>Animal<\/var>.<\/p>\n\n\n\n<p>Whenever we call&nbsp;<code>displayInfo()<\/code>&nbsp;using the&nbsp;<var>d1<\/var>&nbsp;(object of the subclass), the method inside the subclass is called.<\/p>\n\n\n\n<p>Notice that, the&nbsp;<code>displayInfo()<\/code>&nbsp;is declared&nbsp;<code>protected<\/code>&nbsp;in the&nbsp;<var>Animal<\/var>&nbsp;superclass. The same method has the&nbsp;<code>public<\/code>&nbsp;access specifier in the&nbsp;<var>Dog<\/var>&nbsp;subclass. This is possible because the&nbsp;<code>public<\/code>&nbsp;provides larger access than the&nbsp;<code>protected<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Overriding Abstract Methods<\/h2>\n\n\n\n<p>In Java, abstract classes are created to be the superclass of other classes. And, if a class contains an abstract method, it is mandatory to override it.<\/p>\n\n\n\n<p>We will learn more about abstract classes and overriding of abstract methods in later tutorials.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about method overriding in Java with the help of examples. In the last tutorial, we learned about inheritance. Inheritance is an OOP property that allows us to derive a new class (subclass) from an existing class (superclass). The subclass inherits the attributes and methods of the superclass. Now, if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[531],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1204"}],"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=1204"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1204\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}