{"id":234,"date":"2017-12-11T00:46:00","date_gmt":"2017-12-11T00:46:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2017\/12\/11\/object-oriented-programmingoop-explained-with-java-examples-part-2\/"},"modified":"2020-11-05T13:02:37","modified_gmt":"2020-11-05T12:02:37","slug":"object-oriented-programmingoop-explained-with-java-examples-part-2","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/object-oriented-programmingoop-explained-with-java-examples-part-2\/","title":{"rendered":"Object Oriented Programming(OOP) Explained with Java Examples (Part 2)"},"content":{"rendered":"<div style=\"color: #555555; font-size: 18px; line-height: 30px; text-align: justify;\">\n<div style=\"font-family: 'segoe ui';\">In the first part of the Object Oriented Programming lessons, you learnt:<\/p>\n<ul>\n<li>How to create classes<\/li>\n<li>How to create a subclass and inherit from a superclass<\/li>\n<li>How to extend a&nbsp; class<\/li>\n<li>How to define class methods<\/li>\n<li>How to inherit methods from a superclass <\/li>\n<li>Concept of Inheritance (the subclass inherits the properties of the superclass) <\/li>\n<\/ul>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/3.bp.blogspot.com\/-8NVvvdEdWyQ\/Wi3VYP4l5SI\/AAAAAAAAAc8\/Pj_SqzAcgAA2NMTVO_ajTHkWd-Uu5LmcgCLcBGAs\/s1600\/Picture1.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"858\" data-original-width=\"1600\" height=\"213\" src=\"https:\/\/3.bp.blogspot.com\/-8NVvvdEdWyQ\/Wi3VYP4l5SI\/AAAAAAAAAc8\/Pj_SqzAcgAA2NMTVO_ajTHkWd-Uu5LmcgCLcBGAs\/s400\/Picture1.jpg\" width=\"400\" \/><\/a><\/div>\n<p>In this lesson, we would use these classes and actually use these classes in our main program to see how it works.<\/p>\n<p><b>Step 1<\/b>: Open the OOPLesson project<\/p>\n<p><b>Step 2<\/b>: In the main program, type the following lines immediately after the line that begins with package<\/p>\n<p><span style=\"color: #134f5c;\"><span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">import MyClasses.Person;<\/span><\/span><br \/><span style=\"color: #134f5c;\"><span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">import MyClasses.Patient;<\/span><\/span><br \/><span style=\"color: #134f5c;\"><span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">import MyClasses.Physician;<\/span><\/span><\/p>\n<p>This lines tell the compiler that we would like to use these classes in the main program. If you have done this correctly, your program should look like this:<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/2.bp.blogspot.com\/-IO17qkLnv50\/Wi3N02zhUtI\/AAAAAAAAAcg\/JuE_vKB2CeQDE66g8UIBtCNoqXHpN17NACLcBGAs\/s1600\/Import-Statements-for-Lesson-2.JPG\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"651\" data-original-width=\"956\" height=\"433\" src=\"https:\/\/2.bp.blogspot.com\/-IO17qkLnv50\/Wi3N02zhUtI\/AAAAAAAAAcg\/JuE_vKB2CeQDE66g8UIBtCNoqXHpN17NACLcBGAs\/s640\/Import-Statements-for-Lesson-2.JPG\" width=\"640\" \/><\/a><\/div>\n<p>&nbsp;<\/p><\/div>\n<div style=\"font-family: 'segoe ui';\"><b>Step 4<\/b>: Write the following lines of code inside the main function:<\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><\/div>\n<div style=\"font-family: 'segoe ui';\"><span style=\"color: #741b47;\"><span style=\"font-size: x-small;\">Person myPerson = new Person(10);&nbsp; \/\/CREATES A PERSON OBJECT myPerson WITH A PARAMETER SSN<\/span><\/span><\/div>\n<div style=\"font-family: 'segoe ui';\"><span style=\"color: #741b47;\"><span style=\"font-size: x-small;\">&nbsp;<\/span><\/span><\/div>\n<div style=\"font-family: 'segoe ui';\"><span style=\"color: #741b47;\"><span style=\"font-size: x-small;\">myPerson.setFirstname(&#8220;Oleander&#8221;);&nbsp; \/\/ASSIGNS Firstname TO myPerson<\/span><\/span><\/div>\n<div style=\"font-family: 'segoe ui';\"><span style=\"color: #741b47;\"><span style=\"font-size: x-small;\">myPerson.setLastname(&#8220;Yuba&#8221;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ASSIGNS Lastname TO myPerson<\/span><\/span><\/div>\n<div style=\"font-family: 'segoe ui';\"><span style=\"color: #741b47;\"><span style=\"font-size: x-small;\">myPerson.setEmail(&#8220;oleandery@gmail.com&#8221;); \/\/ASSIGNS Email TO myPerson&nbsp; System.out.println(myPerson.getFirstname());&nbsp; \/\/PRINTS THE FIRSTNAME<\/span><\/span><\/div>\n<div style=\"font-family: 'segoe ui';\"><span style=\"color: #741b47;\"><span style=\"font-size: x-small;\">System.out.println(myPerson.getLastname());&nbsp;&nbsp; \/\/PRINTS THE LASTNAME<\/span><\/span><\/div>\n<div style=\"font-family: 'segoe ui';\"><span style=\"font-size: x-small;\"><span style=\"color: #741b47;\">System.out.println(myPerson.getEmail());&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/PRINTS THE EMAIL<\/span><\/span><br \/>If you have done this correctly, then your code would look like the one below.<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/4.bp.blogspot.com\/-ry_WSwlBk84\/Wi3S3zSDnZI\/AAAAAAAAAcw\/BYBELfsDAKkOAHKcfPVl5pz-AwvNQbvKwCLcBGAs\/s1600\/Creating-The-myPerson%2Bobject.JPG\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"714\" data-original-width=\"1259\" height=\"362\" src=\"https:\/\/4.bp.blogspot.com\/-ry_WSwlBk84\/Wi3S3zSDnZI\/AAAAAAAAAcw\/BYBELfsDAKkOAHKcfPVl5pz-AwvNQbvKwCLcBGAs\/s640\/Creating-The-myPerson%2Bobject.JPG\" width=\"640\" \/><\/a><\/div>\n<p><b>Step 5<\/b>: Run the program and observe the output<\/p>\n<p><b>Step 6<\/b>: Follow the same procedure to create myPatient object<\/p>\n<p><b>Step 7<\/b>: Follow the same procedure to create myPhysician object<\/p>\n<p><b>Step 8<\/b>: Take some time to understand how&nbsp; the program works<\/p>\n<p><b>Final Notes<\/b><br \/>In this lesson, Object Oriented Programming(OOP) Explained with Java Examples (Part 2), you have been able to create objects based on the classes you created. This is called instantiating the class, that is creating objects from the class.<br \/>You have also learnt how to assign attributes to the object you created using the public methods of the classes<\/p>\n<p>In the next lesson 3, we would learn about Static Methods and Abstract Classes.<\/p>\n<p>This is lesson 1, in case you want to do a review:<br \/><a href=\"https:\/\/kindsonthegenius.com\/blog\/object-oriented-programmingoop-explained-with-java-examples-part-3\/\">Object Oriented Programming(OOP) Explained With Java Examples (Part 1) <\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In the first part of the Object Oriented Programming lessons, you learnt: How to create classes How to create a subclass and inherit from a &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\/234"}],"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=234"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/234\/revisions"}],"predecessor-version":[{"id":1692,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/234\/revisions\/1692"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}