{"id":4304,"date":"2014-03-30T09:44:56","date_gmt":"2014-03-30T09:44:56","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/field-access-and-memory-allocation-for-objects-in-java-polymorphism-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:44:56","modified_gmt":"2014-03-30T09:44:56","slug":"field-access-and-memory-allocation-for-objects-in-java-polymorphism-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/field-access-and-memory-allocation-for-objects-in-java-polymorphism-collection-of-common-programming-errors\/","title":{"rendered":"Field access and Memory Allocation for Objects in Java Polymorphism-Collection of common programming errors"},"content":{"rendered":"<blockquote>\n<p>Question 1:<\/p>\n<p>The Method is derived class is displayed and Field of the Super class is displayed.<\/p>\n<p>Can some one explain why number field in derived class is not called ? i.e., output is blue2<\/p>\n<\/blockquote>\n<p>Fields cannot be overridden. Even if two classes share a parent-child relationship, the fields belong to the class they were defined in, even if they share names with an inherited field. In other words, <code>number<\/code> in <code>Sub<\/code> is totally different field than <code>number<\/code> in <code>Super<\/code>.<\/p>\n<blockquote>\n<p>Question 2: Here memory for variables, &#8216;number and subText&#8217; in derived class &#8216;Sub&#8217; is created and the address of the variables is placed in supersub Object when i access, supersub.subText i got error that subText cannot be resolved.<\/p>\n<\/blockquote>\n<p>The object stored in <code>supersub<\/code> is of type <code>Sub<\/code>, but the compiler doesn&#8217;t know that.<\/p>\n<p>Because Java is a <em>statically typed<\/em> language, the compiler goes by the <em>declared<\/em> type (that is, the variable type) of the reference because, in most real-world cases, the <em>runtime<\/em> type (the one that&#8217;s apparent in the <code>new<\/code> expression) isn&#8217;t necessarily known at compile time. For example, you may have gotten this object from another method, or from two or three candidate methods, therefore the runtime type is unpredictable.<\/p>\n<p>Storing the reference in a superclass variable means that you intent to use that object as <code>Super<\/code> for a while. The compiler, then, works on this perceived intention of yours. <code>Super<\/code> isn&#8217;t guaranteed to only have instances of a runtime type of <code>Sub<\/code>, therefore it can&#8217;t make the assumptions you expect.<\/p>\n<p>That being said, storing the reference in one kind of variable or another does not modify the object. If you were to cast the object back to a variable of a type that actually knows about those members you&#8217;re trying to access (in your case, the <code>Sub<\/code> type), you&#8217;ll find that they&#8217;re still there (and they retain their values).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Question 1: The Method is derived class is displayed and Field of the Super class is displayed. Can some one explain why number field in derived class is not called ? i.e., output is blue2 Fields cannot be overridden. Even if two classes share a parent-child relationship, the fields belong to the class they were [&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-4304","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4304","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=4304"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4304\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}