{"id":57,"date":"2020-12-31T22:00:39","date_gmt":"2020-12-31T22:00:39","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/practical-java-programming\/?page_id=57"},"modified":"2021-01-04T16:50:35","modified_gmt":"2021-01-04T16:50:35","slug":"classes-objects","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/classes-objects\/","title":{"rendered":"Classes &#038; Objects"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_83 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a06ed5694ef3\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a06ed5694ef3\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/classes-objects\/#What_is_a_Class\" >What is a Class?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/classes-objects\/#What_is_a_Class_Instance\" >What is a Class Instance?<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>Classes are arguably the most important aspect of Java &#8212; or of any object-oriented language.<\/p>\n\n\n\n<p>The relationship between classes and objects is a close one, so we&#8217;ll examine that relationship here.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_a_Class\"><\/span>What is a Class?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Humans classify things all the time.<\/p>\n\n\n\n<p>It helps us from drowning in details:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>&#8220;What vehicle ran over you, madam?&#8221;<br>&#8220;A blue Buick four-door sedan.&#8221;<\/p><\/blockquote>\n\n\n\n<p>rather than:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>&#8220;What vehicle ran over you, madam?&#8221;<br>&#8220;Why, officer, it had a blue body, four wheels, four doors, six windows, a front, back and two sides of some kind of metal. There were four people sitting inside it, and one of them was driving. Each door had a window, a handle, and pinstripes, was about five feet high and four feet wide. In front, there was a shiny metal grille, and &#8230;&#8221;<\/p><\/blockquote>\n\n\n\n<p>For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A\u00a0<strong>Dog<\/strong>\u00a0is a distillation of the common attributes and behaviors of dogs of all sizes, types, and ferocities.<\/li><li>A\u00a0<strong>Car<\/strong>\u00a0is a similar distillation applied to cars of all sizes, colors, speeds, styles, makes, etc.<\/li><li>A\u00a0<strong>Nut<\/strong>\u00a0describes common attributes of Filberts, Brazils, Almonds, Macadamias, etc. Or it describes the attributes of the kind of Nut that attaches to a Bolt (or perhaps even yours\u00a0 truly&#8230;)<\/li><li>A\u00a0<strong>Road<\/strong>\u00a0describes the common attributes of dirt tracks, town streets, county roads and superhighways.<\/li><li>A\u00a0<strong>Student<\/strong>\u00a0encompasses those of types young and old, single and married, male and female, tall and short, undergraduate, graduate, etc.<\/li><li>A\u00a0<strong>College<\/strong>\u00a0describes Technical Colleges, Liberal Arts Colleges, Colleges of Art and Design, Universities, etc.<\/li><\/ul>\n\n\n\n<p>To perform this kind of classification, we need to distill out:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>Attributes<\/em>&nbsp;or&nbsp;<em>Properties<\/em>&nbsp;(has fur, four legs, a tail, sharp teeth, etc.)<\/li><li><em>Behaviors<\/em>&nbsp;or&nbsp;<em>Operations<\/em>&nbsp;(barks, walks, runs, wags tail, etc.)<\/li><\/ul>\n\n\n\n<p>Here&#8217;s an example of a Java class:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npublic class Dog\n{\n    public void StartBarking() { \/* ... *\/ }\n    public void StopBarking() { \/* ... *\/ }\n\n    public int getAge()   { return m_age; }\n    public int getColor() { return m_color; }\n    public int getTemperament() { return m_temperament; }\n\n    public static final int DOCILE = 1;\n    public static final int GOOD_WITH_CHILDREN = 2;\n    public static final int VICIOUS = 3;\n\n    int m_age;\n    int m_color;\n    int m_temperament;\n}\n<\/pre><\/div>\n\n\n<p>Here&#8217;s another example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nimport java.lang.Math;\n\npublic class Circle\n{\n    \/\/ Accessor methods\n    public double getX()      { return m_x; }\n    public double getY()      { return m_y; }\n    public double getRadius() { return m_r; }\n    \/\/ Mutator methods\n    public void setX(double v)      { m_x = v; }\n    public void setY(double v)      { m_y = v; }\n    public void setRadius(double r) { m_r = r; }\n    \/\/ Operations\/Attributes\n    public double getCircumference()\n                { return 2 * Math.PI * m_r; }\n    public double getArea()\n                { return Math.PI * m_r*m_r; }\n\n    double m_x, m_y;\t\/\/ coordinates of center\n    double m_r;\t\t\/\/ radius\n}\n<\/pre><\/div>\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><em><u>Note:<\/u><\/em><\/strong>\u00a0Think of a class as a\u00a0<em>template,<\/em>\u00a0or a\u00a0<em>recipe<\/em>\u00a0from which to make\u00a0<em>instances<\/em>\u00a0of that class.<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><em><u>Note:<\/u><\/em><\/strong>\u00a0It&#8217;s hard to tell an abstract concept &#8220;Dog&#8221; to go fetch!<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_a_Class_Instance\"><\/span>What is a Class Instance?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A&nbsp;<em>class<\/em>&nbsp;is a&nbsp;<em>template<\/em>&nbsp;from which to make an&nbsp;<em>object<\/em><\/p>\n\n\n\n<p>An&nbsp;<em>instance<\/em>&nbsp;of a class is an object.&nbsp; (This is somewhat loose terminology, but the two terms are usually considered synonyms.)<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code><strong>Fido<\/strong><\/code>\u00a0is an instance of class\u00a0<code><strong>Dog<\/strong><\/code><\/li><li><code><strong>FredBloggs<\/strong><\/code>\u00a0is an instance of class\u00a0<code><strong>Person<\/strong><\/code><\/li><\/ul>\n\n\n\n<p>For example, given a class like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nclass BankAccount\n{\n   \/\/ Instance methods\n   double deposit(double amount)\n   {\n      m_balance += amount;\n      return m_balance;\n   }\n\n   double withdraw(double amount)\n   {\n      m_balance -= amount;\n      return m_balance;\n   }\n\n   String getHolder()\n   {\n      return m_holder;\n   }\n\n   \/\/ Instance variables\n   String m_holder;\n   double m_balance;\n}\n<\/pre><\/div>\n\n\n<p>we can&nbsp;<em>instantiate<\/em>&nbsp;an instance of this class as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>BankAccount ac = new BankAccount();<\/strong><\/pre>\n\n\n\n<p>The actual instantiation is performed in:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>new BankAccount();<\/strong><\/pre>\n\n\n\n<p>while:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>BankAccount ac;<\/strong><\/pre>\n\n\n\n<p>is just a&nbsp;<em>reference<\/em>&nbsp;to the instance.<\/p>\n\n\n\n<p>The instance is created in two stages:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The&nbsp;<strong>new&nbsp;<\/strong>operator creates the space for the class instance<\/li><li>A&nbsp;<em>constructor<\/em>&nbsp;is called to initialize the instance<\/li><\/ol>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><em><u>Note:<\/u><\/em><\/strong>\u00a0More about\u00a0<em>constructor methods<\/em>\u00a0very soon&#8230;<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Classes are arguably the most important aspect of Java &#8212; or of any object-oriented language. The relationship between classes and objects is a close one, so we&#8217;ll examine that relationship here. What is a Class? Humans classify things all the time. It helps us from drowning in details: &#8220;What vehicle ran over you, madam?&#8221;&#8220;A blue [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":34,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_eb_attr":"","_uag_custom_page_level_css":"","ocean_post_layout":"left-sidebar","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"ocs-course-topics-sidebar","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","footnotes":""},"class_list":["post-57","page","type-page","status-publish","hentry","entry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"ocean-thumb-m":false,"ocean-thumb-ml":false,"ocean-thumb-l":false},"uagb_author_info":{"display_name":"Bryan Higgs","author_link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/author\/bryan\/"},"uagb_comment_info":0,"uagb_excerpt":"Classes are arguably the most important aspect of Java &#8212; or of any object-oriented language. The relationship between classes and objects is a close one, so we&#8217;ll examine that relationship here. What is a Class? Humans classify things all the time. It helps us from drowning in details: &#8220;What vehicle ran over you, madam?&#8221;&#8220;A blue&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/comments?post=57"}],"version-history":[{"count":2,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/57\/revisions"}],"predecessor-version":[{"id":380,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/57\/revisions\/380"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/34"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}