{"id":1206,"date":"2022-02-25T16:48:47","date_gmt":"2022-02-25T16:48:47","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1206"},"modified":"2022-02-25T16:48:47","modified_gmt":"2022-02-25T16:48:47","slug":"java-super","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/25\/java-super\/","title":{"rendered":"Java super"},"content":{"rendered":"\n<p>In this tutorial, we will learn about the super keyword in Java with the help of examples.<\/p>\n\n\n\n<p>The&nbsp;<code>super<\/code>&nbsp;keyword in Java is used in subclasses to access superclass members (attributes, constructors and methods).<\/p>\n\n\n\n<p>Before we learn about the\u00a0<code>super<\/code>\u00a0keyword, make sure to know about\u00a0Java inheritance.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"uses\">Uses of super keyword<\/h2>\n\n\n\n<ol><li>To call methods of the superclass that is overridden in the subclass.<\/li><li>To access attributes (fields) of the superclass if both superclass and subclass have attributes with the same name.<\/li><li>To explicitly call superclass no-arg (default) or parameterized constructor from the subclass constructor.<\/li><\/ol>\n\n\n\n<p>Let\u2019s understand each of these uses.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"access-superclass-methods\">1. Access Overridden Methods of the superclass<\/h2>\n\n\n\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\u00a0method overriding.<\/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\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-overriding-example.png\" alt=\"Java overriding example\"\/><\/figure>\n\n\n\n<p><strong>What if the overridden method of the superclass has to be called?<\/strong><\/p>\n\n\n\n<p>We use&nbsp;<code>super.display()<\/code>&nbsp;if the overridden method&nbsp;<code>display()<\/code>&nbsp;of superclass&nbsp;<var>Animal<\/var>&nbsp;needs to be called.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: super to Call Superclass Method<\/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\n    \/\/ this calls overriding method\n    display();\n\n    \/\/ this calls overridden method\n    super.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\nI am an animal\n<\/samp><\/code><\/pre>\n\n\n\n<p>Here, how the above program works.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/call-superclass-method.png\" alt=\"Working of super in Java\"\/><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"access-superclass-attributes\">2. Access Attributes of the Superclass<\/h2>\n\n\n\n<p>The superclass and subclass can have attributes with the same name. We use the&nbsp;<code>super<\/code>&nbsp;keyword to access the attribute of the superclass.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3: Access superclass attribute<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n  protected String type=\"animal\";\n}\n\nclass Dog extends Animal {\n  public String type=\"mammal\";\n\n  public void printType() {\n    System.out.println(\"I am a \" + type);\n    System.out.println(\"I am an \" + super.type);\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n    Dog dog1 = new Dog();\n    dog1.printType();\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 mammal\nI am an animal\n<\/samp><\/code><\/pre>\n\n\n\n<p>In this example, we have defined the same instance field&nbsp;<var>type<\/var>&nbsp;in both the superclass&nbsp;<var>Animal<\/var>&nbsp;and the subclass&nbsp;<var>Dog<\/var>.<\/p>\n\n\n\n<p>We then created an object&nbsp;<var>dog1<\/var>&nbsp;of the&nbsp;<var>Dog<\/var>&nbsp;class. Then, the&nbsp;<code>printType()<\/code>&nbsp;method is called using this object.<\/p>\n\n\n\n<p>Inside the&nbsp;<code>printType()<\/code>&nbsp;function,<\/p>\n\n\n\n<ul><li><var>type<\/var>&nbsp;refers to the attribute of the subclass&nbsp;<var>Dog<\/var>.<\/li><li><var>super.type<\/var>&nbsp;refers to the attribute of the superclass Animal.<\/li><\/ul>\n\n\n\n<p>Hence,&nbsp;<code>System.out.println(\"I am a \" + type);<\/code>&nbsp;prints&nbsp;<samp>I am a mammal<\/samp>. And,&nbsp;<code>System.out.println(\"I am an \" + super.type);<\/code>&nbsp;prints&nbsp;<samp>I am an animal<\/samp>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. Use of super() to access superclass constructor<\/h2>\n\n\n\n<p>As we know, when an object of a class is created, its default constructor is automatically called.<\/p>\n\n\n\n<p>To explicitly call the superclass constructor from the subclass constructor, we use&nbsp;<code>super()<\/code>. It&#8217;s a special form of the&nbsp;<code>super<\/code>&nbsp;keyword.<\/p>\n\n\n\n<p><code>super()<\/code>&nbsp;can be used only inside the subclass constructor and must be the first statement.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 4: Use of super()<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n\n  \/\/ default or no-arg constructor of class Animal\n  Animal() {\n    System.out.println(\"I am an animal\");\n  }\n}\n\nclass Dog extends Animal {\n\n  \/\/ default or no-arg constructor of class Dog\n  Dog() {\n\n    \/\/ calling default constructor of the superclass\n    super();\n\n    System.out.println(\"I am a dog\");\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n    Dog dog1 = new Dog();\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>Here, when an object&nbsp;<var>dog1<\/var>&nbsp;of&nbsp;<var>Dog<\/var>&nbsp;class is created, it automatically calls the default or no-arg constructor of that class.<\/p>\n\n\n\n<p>Inside the subclass constructor, the&nbsp;<code>super()<\/code>&nbsp;statement calls the constructor of the superclass and executes the statements inside it. Hence, we get the output&nbsp;<samp>I am an animal<\/samp>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/super%28%29-example.png\" alt=\"Working of super() in Java\"\/><\/figure>\n\n\n\n<p>The flow of the program then returns back to the subclass constructor and executes the remaining statements. Thus,&nbsp;<samp>I am a dog<\/samp>&nbsp;will be printed.<\/p>\n\n\n\n<p>However, using&nbsp;<code>super()<\/code>&nbsp;is not compulsory. Even if&nbsp;<code>super()<\/code>&nbsp;is not used in the subclass constructor, the compiler implicitly calls the default constructor of the superclass.<\/p>\n\n\n\n<p><strong>So, why use redundant code if the compiler automatically invokes super()?<\/strong><\/p>\n\n\n\n<p>It is required if the&nbsp;<strong>parameterized constructor (a constructor that takes arguments)<\/strong>&nbsp;of the superclass has to be called from the subclass constructor.<\/p>\n\n\n\n<p>The parameterized&nbsp;<code>super()<\/code>&nbsp;must always be the first statement in the body of the constructor of the subclass, otherwise, we get a compilation error.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"access-superclass-constructors\">Example 5: Call Parameterized Constructor Using super()<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n\n  \/\/ default or no-arg constructor\n  Animal() {\n    System.out.println(\"I am an animal\");\n  }\n\n  \/\/ parameterized constructor\n  Animal(String type) {\n    System.out.println(\"Type: \"+type);\n  }\n}\n\nclass Dog extends Animal {\n\n  \/\/ default constructor\n  Dog() {\n\n    \/\/ calling parameterized constructor of the superclass\n    super(\"Animal\");\n\n    System.out.println(\"I am a dog\");\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n    Dog dog1 = new Dog();\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>Type: Animal\nI am a dog\n<\/samp><\/code><\/pre>\n\n\n\n<p>The compiler can automatically call the no-arg constructor. However, it cannot call parameterized constructors.<\/p>\n\n\n\n<p>If a parameterized constructor has to be called, we need to explicitly define it in the subclass constructor.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/parameterized-super-example.png\" alt=\"Working of super in case of parameterized constructor.\"\/><\/figure>\n\n\n\n<p>Note that in the above example, we explicitly called the parameterized constructor&nbsp;<code>super(\"Animal\")<\/code>. The compiler does not call the default constructor of the superclass in this case.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about the super keyword in Java with the help of examples. The&nbsp;super&nbsp;keyword in Java is used in subclasses to access superclass members (attributes, constructors and methods). Before we learn about the\u00a0super\u00a0keyword, make sure to know about\u00a0Java inheritance. Uses of super keyword To call methods of the superclass that is [&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\/1206"}],"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=1206"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1206\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}