{"id":20983,"date":"2017-04-18T22:23:00","date_gmt":"2017-04-18T22:23:00","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=20983"},"modified":"2017-04-18T22:23:00","modified_gmt":"2017-04-18T22:23:00","slug":"java-programming-constructors-in-subclasses","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/java-programming-constructors-in-subclasses\/","title":{"rendered":"Java programming:  constructors in subclasses"},"content":{"rendered":"<h1>Tutoring Java programming, there are so many interesting facets to encounter.  The tutor talks about a constructor of a subclass and the word <i>super<\/i>.<\/h1>\n<p>Let&#8217;s imagine you write a subclass.  Furthermore, let&#8217;s imagine the parent class (the <i>superclass<\/i>) has a detailed constructor.  Can you assume that the subclass will simply inherit the parent&#8217;s constructor and default to its use?  Apparently not.<\/p>\n<p>My situation:  I wrote a subclass of an abstract class that, while abstract, has a well-defined constructor.  In the subclass I just defined the abstract functions from the parent.  The compiler protested, citing a mismatch between the parameters required for the constructor versus those given.  I looked up the problem.<\/p>\n<p>Apparently, to a subclass, the compiler provides a default constructor with no arguments.<\/p>\n<p>To instill the parent&#8217;s constructor in the subclass, one can declare a constructor in the subclass with the same parameters as those of the superclass&#8217; constructor, then call <i>super(parameter list)<\/i>.<\/p>\n<p>Let&#8217;s imagine a parent class PC like so:<\/p>\n<p style=\"font-family:monospace;font-size:120%;\">\nclass PC{<br \/>\n<br \/>\nString name;<br \/>\nString color;<\/p>\n<div style=\"background:#bbeeee\">\npublic PC(String name, String color){<br \/>\nthis.name=name;<br \/>\nthis.color=color;<br \/>\n}\/\/end of constructor<br \/>\n\n<\/div>\n<p>}\n<\/p>\n<p>Now, the subclass SC:<\/p>\n<p style=\"font-family:monospace;font-size:120%;white-space:pre-wrap\">\nclass SC extends PC{<\/p>\n<div style=\"background:#bbeeee\">\npublic SC(String name, String color){<br \/>\nsuper(name,color);<br \/>\n}\/\/end constructor<br \/>\n\n<\/div>\n<p>}\n<\/p>\n<p>From my observation, the code in the blue rectangles above is what will make the subclass take on the parent&#8217;s constructor.<\/p>\n<p>Source:<\/p>\n<p><a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/java\/javaOO\/constructors.html\">docs.oracle.com<\/a><\/p>\n<p><a href=\"http:\/\/journals.ecs.soton.ac.uk\/java\/tutorial\/java\/javaOO\/subclasses.html\">journals.ecs.soton.ac.uk\/java<\/a><\/p>\n<p>Jack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tutoring Java programming, there are so many interesting facets to encounter. The tutor talks about a constructor of a subclass and the word super. Let&#8217;s imagine you write a subclass. Furthermore, let&#8217;s imagine the parent class (the superclass) has a &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/java-programming-constructors-in-subclasses\/\"> <span class=\"screen-reader-text\">Java programming:  constructors in subclasses<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105,1801],"tags":[2266,2265,2268],"class_list":["post-20983","post","type-post","status-publish","format-standard","hentry","category-computer-science","category-java","tag-how-to-make-a-subclass-inherit-the-parents-constructor","tag-inheritance-of-constructor","tag-superclass-constructor"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/20983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/comments?post=20983"}],"version-history":[{"count":32,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/20983\/revisions"}],"predecessor-version":[{"id":21015,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/20983\/revisions\/21015"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=20983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=20983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=20983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}