{"id":235,"date":"2017-12-10T02:33:00","date_gmt":"2017-12-10T02:33:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2017\/12\/10\/object-oriented-programmingoop-explained-with-java-examples-part-1\/"},"modified":"2020-11-05T13:01:23","modified_gmt":"2020-11-05T12:01:23","slug":"object-oriented-programmingoop-explained-with-java-examples-part-1","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/object-oriented-programmingoop-explained-with-java-examples-part-1\/","title":{"rendered":"Object Oriented Programming(OOP) Explained with Java Examples (Part 1)"},"content":{"rendered":"<div style=\"color: #555555; font-size: 18px; line-height: 30px; text-align: justify;\">\n<div style=\"font-family: 'segoe ui';\">In this lesson, we would understand the idea of object oriented programming and then we would show some examples of how Object Oriented Programing works using Java.<br \/>We would use Netbeans IDE to run our program codes, so make sure you have it installed in your system.<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/2.bp.blogspot.com\/-1_IMIRP7sSE\/WiycSW1r8PI\/AAAAAAAAAcQ\/CdYJmO67XUg7VkFkty4CQysYanZOomAcgCLcBGAs\/s1600\/Object-Oriente-Programming-Explained-With-Java-Examples-Using-Netbeans.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"882\" data-original-width=\"1497\" height=\"188\" src=\"https:\/\/2.bp.blogspot.com\/-1_IMIRP7sSE\/WiycSW1r8PI\/AAAAAAAAAcQ\/CdYJmO67XUg7VkFkty4CQysYanZOomAcgCLcBGAs\/s320\/Object-Oriente-Programming-Explained-With-Java-Examples-Using-Netbeans.jpg\" width=\"320\" \/><\/a><\/div>\n<p>This lesson would be based on the following sub-topics:<\/p>\n<ul>\n<li>What is Object Oriented Programming<\/li>\n<li>Create a Class in Java<\/li>\n<li>Create&nbsp; a Subclass<\/li>\n<li>Concept of Inheritance<\/li>\n<li>Concept of Polymorphism<\/li>\n<li>Encapsulation<\/li>\n<\/ul>\n<p>So let&#8217;s start with the first one<\/p>\n<p><b>What is Object Oriented Programming?<\/b><br \/>&nbsp;Object oriented programming is a programming method that models entities in the program like real objects with attributes.<\/p>\n<p>&nbsp;To Illustration OOP in Java, we would try to build a hospital personnel database made up of both patients and physicians.<\/p>\n<p><b>Create a Class in Java (Superclass)<\/b><br \/>Let&#8217;s create a class in Java called Person. To do this you would need Netbeans installed in your system.<br \/><b>Step 1<\/b>: Open Netbeans and create a Java Application<br \/><b>Step 2:<\/b> Add&nbsp; a new class and name it Person<br \/><b>Step 3:<\/b> Add the following attributes to the class<\/p>\n<ul>\n<li>SSN<\/li>\n<li>Firstname<\/li>\n<li>Lastname<\/li>\n<li>Email<\/li>\n<li>Phone<\/li>\n<li>DateOfBirth<\/li>\n<\/ul>\n<p><b>Step 4<\/b>: Add one constructor to the class that takes one parameter: SSN<br \/><b>Step 5:<\/b> Add another constructor that takes three parameters: SSN, Firstname and Lastname<br \/><b>Step 6<\/b>: Add a third constructor that take no parameter. <\/p>\n<p>At the end of this step, your class file would look like shown below:<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/2.bp.blogspot.com\/-tYV-t6SOjuM\/WiyLMgNUSSI\/AAAAAAAAAbc\/JacJr4GbAgEkHEco8PfmgxjoXxj85DBjQCLcBGAs\/s1600\/Person-Class-File-in-Java.JPG\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"728\" data-original-width=\"1037\" height=\"448\" src=\"https:\/\/2.bp.blogspot.com\/-tYV-t6SOjuM\/WiyLMgNUSSI\/AAAAAAAAAbc\/JacJr4GbAgEkHEco8PfmgxjoXxj85DBjQCLcBGAs\/s640\/Person-Class-File-in-Java.JPG\" width=\"640\" \/><\/a><\/div>\n<p><b>Step 7: Add Getters and Setters<\/b><br \/>You could do this manually, but you could save a lot of time by allowing Netbeans to generate these for you. To do that:<\/p>\n<ul>\n<li>Right-click on inside the class file (in an open space)<\/li>\n<li>In the list that appears, click on Insert Code<\/li>\n<li>In the list that appears, click on Getter and Setter. The window below appears:<\/li>\n<\/ul>\n<p><\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/3.bp.blogspot.com\/-kwQPPjl0TSw\/WiyDtljfzQI\/AAAAAAAAAbM\/9GGt1gWH65Q6YLpta7_If4Vq_wT0ev4SACLcBGAs\/s1600\/Getter-and-Setter-Window-in-Java-Netbeasn.JPG\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"575\" data-original-width=\"440\" height=\"640\" src=\"https:\/\/3.bp.blogspot.com\/-kwQPPjl0TSw\/WiyDtljfzQI\/AAAAAAAAAbM\/9GGt1gWH65Q6YLpta7_If4Vq_wT0ev4SACLcBGAs\/s640\/Getter-and-Setter-Window-in-Java-Netbeasn.JPG\" width=\"488\" \/><\/a><\/div>\n<p><\/p>\n<ul>\n<li>Tick Encapsulate Fields<\/li>\n<li>Click on Select All to select all the fields<\/li>\n<li>Click on Generate. At this point the getters and setters are added to your class.<\/li>\n<\/ul>\n<p><b>Step 8: Create the Patient Class (Subclass) <\/b><br \/>Add another class to your program and name it Patient. Remember to place this class in MyClasses package.<br \/>After the class name add &#8216;extends Person&#8217;. This tell the compiler that the Patient class is a subset of the Person class. This means that &#8220;a Patient is a Person&#8221;.<br \/>Add three attributes:<\/p>\n<ul>\n<li>AdmissionDate<\/li>\n<li>Allergies<\/li>\n<li>Diagnosis<\/li>\n<\/ul>\n<p>Right-click and choose Insert Code<br \/>Choose Constructor from the list that appears. This would display the Generate Construction dialog box as shown below:<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/2.bp.blogspot.com\/-sVITLDhj368\/WiyO123oI3I\/AAAAAAAAAbo\/2F6Ek7bjddQlxvP7kw9vEod3wBeEwr7EQCLcBGAs\/s1600\/Generate-Constructor-For-The-Subclass.JPG\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"552\" data-original-width=\"852\" height=\"414\" src=\"https:\/\/2.bp.blogspot.com\/-sVITLDhj368\/WiyO123oI3I\/AAAAAAAAAbo\/2F6Ek7bjddQlxvP7kw9vEod3wBeEwr7EQCLcBGAs\/s640\/Generate-Constructor-For-The-Subclass.JPG\" width=\"640\" \/><\/a><\/div>\n<p>In the Select Super Construction section, select everything.<\/p><\/div>\n<div style=\"font-family: 'segoe ui';\">Click on Generate.<\/div>\n<div style=\"font-family: 'segoe ui';\">After then, your patient class would be as shown in the figure below:<\/div>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/1.bp.blogspot.com\/-cQjq-n14zxk\/WiyPia4M4rI\/AAAAAAAAAbw\/QGN05OQgINgLWN04IFAvYLXg_HbyjsvcgCLcBGAs\/s1600\/Patient-Class-File-in-Java.JPG\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"708\" data-original-width=\"1037\" height=\"436\" src=\"https:\/\/1.bp.blogspot.com\/-cQjq-n14zxk\/WiyPia4M4rI\/AAAAAAAAAbw\/QGN05OQgINgLWN04IFAvYLXg_HbyjsvcgCLcBGAs\/s640\/Patient-Class-File-in-Java.JPG\" width=\"640\" \/><\/a><\/div>\n<div style=\"font-family: 'segoe ui';\">&nbsp;Add Getter and Setter. The same way you did in the Person class.<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><b>Step 9: Add the Physician Class<\/b><br \/>Add a class to the project and name it Physician. Remember to put this class in the MyClasses folder.<br \/>Make the class extend Person <br \/>Follow the same procedure as with the Patient class but this time you will add the following attributes:<\/p>\n<ul>\n<li>Specialty<\/li>\n<li>Department<\/li>\n<li>Office<\/li>\n<\/ul>\n<p>Add the Constructors the way you did for the Patient class<br \/>Add Getter and Setter the way you did for the Patient class<\/p>\n<p>The UML Diagram below summarizes the three classes you have in&nbsp; your project. <\/p>\n<div style=\"clear: both; text-align: center;\">&nbsp;<a href=\"https:\/\/3.bp.blogspot.com\/-ZftNFersQQk\/WiyVp4iMaBI\/AAAAAAAAAcA\/J1EGT4mrsQon_Ya4f8kRThx7djQo9DTTQCLcBGAs\/s1600\/UML-Diagram.JPG\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"736\" data-original-width=\"1366\" height=\"344\" src=\"https:\/\/3.bp.blogspot.com\/-ZftNFersQQk\/WiyVp4iMaBI\/AAAAAAAAAcA\/J1EGT4mrsQon_Ya4f8kRThx7djQo9DTTQCLcBGAs\/s640\/UML-Diagram.JPG\" width=\"640\" \/><\/a><\/div>\n<p>Try to examine the class diagram to make sure you understand various parts of it. In the next lesson, we would use this three classes to illustrate the concept of Inheritance. The video tutorial of this lesson would be published in couple of days.<\/p>\n<div style=\"text-align: center;\"><a href=\"https:\/\/kindsonthegenius.com\/blog\/object-oriented-programmingoop-explained-with-java-examples-part-2\/\">On to Part 2 <\/a><\/div>\n<\/div>\n<div style=\"font-family: 'segoe ui';\"><a href=\"https:\/\/kindsonthegenius.com\/blog\/object-oriented-programmingoop-explained-with-java-examples-part-2\/\">Object Oriented Programming (OOP) explained with Java examples using Netbeans, Part 2!<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, we would understand the idea of object oriented programming and then we would show some examples of how Object Oriented Programing works &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[85],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/235"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=235"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/235\/revisions"}],"predecessor-version":[{"id":1691,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/235\/revisions\/1691"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}