{"id":4245,"date":"2014-03-30T09:25:29","date_gmt":"2014-03-30T09:25:29","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-to-understand-super-in-java-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:25:29","modified_gmt":"2014-03-30T09:25:29","slug":"how-to-understand-super-in-java-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-to-understand-super-in-java-collection-of-common-programming-errors\/","title":{"rendered":"how to understand super in java-Collection of common programming errors"},"content":{"rendered":"<p>I encountered a problem that confused me, it is the keyword &#8216;super&#8217;, my test code is like this:<\/p>\n<pre><code>package test;\n\npublic class Parent {\n   private String name;\n\n   public Parent(){\n        this.name = \"parent\";      \n   }\n\n    public String getName() {\n        return name;\n    }\n\n    public void setName(String name) {\n        this.name = name;\n    }\n\n    public void showName(){\n        System.out.println(this.name);\n    }\n\n}\n\n\npublic class Child extends Parent{\n\n    public Child(){\n        this.setName(\"Child\");\n    }\n\n    public void showName(){\n        System.out.println(super.getClass().toString());\n        System.out.println(super.toString());\n\n        super.showName();\n        System.out.println(super.getName());\n    }\n}\n\npublic class Test {\n\n    public static void main(String[] args) {\n        Child d = new Child();\n        d.showName();    \n    }\n}\n<\/code><\/pre>\n<p>so the result is like this:<\/p>\n<pre><code>class test.Child\ntest.Child@2207d8bb\nChild\nChild\n<\/code><\/pre>\n<p>my understanding about &#8216;super&#8217; is that it is a reference to the parent instance of current instance, so my expecting output is like &#8216;Parent&#8217;, from the result , I am wrong, its like the current instance calls the parent method, and &#8216;super&#8217; is not parent instance, is my understanding right ? and is there a way that I can get parent instance only initializing the Child class ?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I encountered a problem that confused me, it is the keyword &#8216;super&#8217;, my test code is like this: package test; public class Parent { private String name; public Parent(){ this.name = &#8220;parent&#8221;; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void showName(){ System.out.println(this.name); } } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4245","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4245"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4245\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}