{"id":1088,"date":"2016-03-07T11:53:22","date_gmt":"2016-03-07T11:53:22","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?p=1088"},"modified":"2016-03-07T11:53:22","modified_gmt":"2016-03-07T11:53:22","slug":"super-keyword","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/java\/super-keyword.html","title":{"rendered":"Super Keyword In Java With Examples"},"content":{"rendered":"<p>In java the keyword <strong>super <\/strong>refers to the superclass of the class in which super appears. A subclass inherits the accessible variables and methods from its superclass, but the constructors of the superclass are not inherited in the subclass. They can only be invoked from constructors of subclass using the keyword <strong>super<\/strong>.<\/p>\n<p>The superclass constructor call must be the first statement in the body of childclass constructor. If one does not specify <strong>super<\/strong>, the compiler implicitly inserts <strong>super();<\/strong> at the beginning of the childclass\u2019s default constructor to call the superclass\u2019s default constructor.<\/p>\n<hr \/>\n<h4><strong>Syntax of Super<\/strong><\/h4>\n<p>Following syntax is used to call superclass constructor:<\/p>\n<pre>super([arguments])<\/pre>\n<p>Here the arguments enclosed in square bracket are optional.<\/p>\n<hr \/>\n<h4><strong>Program Examples of Super Keyword<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>Program 1:<\/strong><\/span> This program shows how to call super class <strong>constructor <\/strong>using \u201csuper\u201d keyword in Java.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1: <\/strong><\/span>First we create a class <strong>SuperClass <\/strong>in which we take a constructor:<\/p>\n<pre>class SuperClass\r\n\r\n{\r\n\r\n  public SuperClass(String str)\r\n\r\n  { \r\n\r\n    System.out.println(\"Super Class Constructor \" + str);\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2: <\/strong><\/span>Second we create a class <strong>SubClass <\/strong>which extends the class <strong>SuperClass <\/strong>and we use super keyword to call the SuperClass constructor:<\/p>\n<pre>class SubClass extends SuperClass\r\n\r\n{\r\n\r\n   public SubClass(String str)\r\n\r\n   {\r\n\r\n     super(str); \/\/calls SuperClass class constructor\r\n\r\n     System.out.println(\"Sub Class Constructor \" + str);\r\n\r\n   }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3: <\/strong><\/span>Third we create a class <strong>SuperConst <\/strong>in which we make an object of class <strong>SubClass<\/strong>:<\/p>\n<pre>class SuperConst\r\n\r\n{\r\n\r\n   public static void main(String args[])\r\n\r\n   {\r\n\r\n     SubClass obj = new SubClass(\"called\");\r\n\r\n   }\r\n\r\n}<\/pre>\n<p><strong>Output<\/strong><strong>:<\/strong><\/p>\n<pre>Super Class Constructor called\r\nSub Class Constructor called<\/pre>\n<p><span style=\"color: #008000;\"><strong>Program 2:<\/strong><\/span> This programs shows how to call super class <strong>method <\/strong>using \u201csuper\u201d in java.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1: <\/strong><\/span>First we create a class <strong>SuperClass<\/strong> in which we take a show() method:<\/p>\n<pre>class SuperClass\r\n\r\n{\r\n\r\n   void show()\r\n\r\n  {\r\n\r\n    System.out.println(\"Super Class method has been called\");\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2: <\/strong><\/span>Second we create a class <strong>SubClass<\/strong> which extends the class <strong>SuperClass<\/strong> and use the super keyword to call the SuperClass class method:<\/p>\n<pre>class SubClass extends SuperClass\r\n\r\n{\r\n\r\n  void show()\r\n\r\n  {\r\n\r\n    super.show();\r\n\r\n    System.out.println(\"Sub Class method has been called\");\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3: <\/strong><\/span>Third we create a class <strong>SuperMethod<\/strong> in which we make an object of class <strong>SubClass<\/strong>:<\/p>\n<pre>class SuperMethod\r\n\r\n{\r\n\r\n  public static void main(String args[])\r\n\r\n {\r\n\r\n\u00a0  SubClass obj = new SubClass();\r\n\r\n   obj.show();\r\n }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>Super Class method has been called\r\nSub Class method has been called<\/pre>\n<p><span style=\"color: #008000;\"><strong>Program 3:<\/strong><\/span> This program shows how to access super class <strong>variables <\/strong>using \u201csuper\u201d in java.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1: <\/strong><\/span>First we create a class <strong>SuperClass<\/strong> in which we take a variable:<\/p>\n<pre>class SuperClass\r\n\r\n{\r\n\r\n  int x = 30;\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2: <\/strong><\/span>Second we create a class <strong>SubClass<\/strong> which extends the class <strong>SuperClass<\/strong> and use the super keyword to call the SuperClass class variable:<\/p>\n<pre>class SubClass extends SuperClass\r\n\r\n{\r\n\r\n  void show()\r\n\r\n {\r\n\r\n   int x = super.x;\r\n\r\n   System.out.println(\"The value of x is : \" + x);\r\n\r\n }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3: <\/strong><\/span>Third we create a class <strong>SuperVar<\/strong> in which we make an object of class <strong>SubClass<\/strong>:<\/p>\n<pre>class SuperVar\r\n\r\n{\r\n\r\n   public static void main(String args[])\r\n\r\n  {\r\n\r\n    SubClass obj = new SubClass();\r\n\r\n    obj.show();\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>The value x is : 30<\/pre>\n<hr \/>\n<h4><strong>Rules of Super Keyword<\/strong><\/h4>\n<ul>\n<li>The superclass constructor call, using keyword <strong>super<\/strong> must be the first statement in the body of subclass constructor.<\/li>\n<li>A superclass constructor can only be called from a subclass constructor. Any other subclass method cannot call it.<\/li>\n<li>A superclass constructor call requires the use of <strong>super<\/strong>. It is illegal to specify the actual name of the class.<\/li>\n<li>From outside the class, a constructor is always called with a <strong>new<\/strong> From inside the class, it may be called from another constructor with this or <strong>super<\/strong>. \u201cthis\u201d operator is used to call another constructor of the same class while super is used to call the constructor of its superclass.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>Important Points to Remember:<\/strong><\/h4>\n<ul>\n<li>We cannot print super, there is a syntax error.<\/li>\n<li>Always data members of parent class are inherited by super keyword.<\/li>\n<li>If we hide any variable then super keyword becomes mandatory.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In java the keyword super refers to the superclass of the class in which super appears. A subclass inherits the accessible variables and methods from its superclass, but the constructors of the superclass are not inherited in the subclass. They can only be invoked from constructors of subclass using the keyword super. The superclass constructor &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/super-keyword.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Super Keyword In Java With Examples<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,79],"tags":[],"class_list":["post-1088","post","type-post","status-publish","format-standard","hentry","category-archieve","category-java-basics"],"psp_head":"<title>Super Keyword In Java With Examples \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn Super keyword with program examples in JAVA. In java the keyword super refers to the superclass of the class in which super appears.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/super-keyword.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=1088"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1088\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=1088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/categories?post=1088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/tags?post=1088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}