{"id":231,"date":"2017-12-12T21:54:00","date_gmt":"2017-12-12T21:54:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2017\/12\/12\/object-oriented-programmingoop-explained-with-java-examples-part-5\/"},"modified":"2017-12-12T21:54:00","modified_gmt":"2017-12-12T21:54:00","slug":"object-oriented-programmingoop-explained-with-java-examples-part-5","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/object-oriented-programmingoop-explained-with-java-examples-part-5\/","title":{"rendered":"Object Oriented Programming(OOP) Explained with Java Examples (Part 5)"},"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 are going to discuss the concept of polymorphism in the Java programming language.<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/4.bp.blogspot.com\/-_Ae9CUKZemo\/WjBQBAmG2mI\/AAAAAAAAAfU\/GJmPoEYqxsYE_7dtnm4UI8Y01H785CRAwCLcBGAs\/s1600\/Polymorphism%2Bin%2BJava.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"862\" data-original-width=\"1530\" height=\"180\" src=\"https:\/\/4.bp.blogspot.com\/-_Ae9CUKZemo\/WjBQBAmG2mI\/AAAAAAAAAfU\/GJmPoEYqxsYE_7dtnm4UI8Y01H785CRAwCLcBGAs\/s320\/Polymorphism%2Bin%2BJava.jpg\" width=\"320\" \/><\/a><\/div>\n<p><b>What is Polymorphism<\/b><br \/>Polymorphism means the ability to exist in different forms. Remember that the three pillars of Object Oriented Programming is:<\/p>\n<ul>\n<li>Inheritance<\/li>\n<li>Abstraction<\/li>\n<li>Polymorphism<\/li>\n<\/ul>\n<p>We have already discussed Inheritance and Abstraction in previous lesson. So, to be able to follow this lesson, you need a knowledge of Inheritance but you can still follow<\/p>\n<p><b>Step 1: <\/b>Open Netbeans IDE and create an application<\/p>\n<p><b>Step 2:<\/b> Create a class and give it a name <i>Animal<\/i><\/p>\n<p><b>Step 3:<\/b> Add a method to the class<br \/>Add a method to the class called <i>MakeSound()<\/i> defined&nbsp; like this:<\/p>\n<p><span style=\"color: #351c75;\"><span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">void MakeSound() {<\/span><\/span><br \/><span style=\"color: #351c75;\"><span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">&nbsp;&nbsp;&nbsp; System.out.println(&#8220;I can make a sound&#8221;);<\/span><\/span><br \/><span style=\"color: #351c75;\"><span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">}&nbsp;<\/span><\/span><\/p>\n<p><b>Step 4<\/b>: Go to the function in the polymorphismLesson.java file, create a new Animal object and call its function, <i>MakeNoise()<\/i>. The two lines you need to type into your main program would be:<\/p>\n<p><span style=\"color: #351c75;\"><span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Animal myPet = new Animal();&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myPet.MakeSound(); <\/span><\/span><\/p>\n<p><b>Step 5<\/b>: Run the program and observe that the programs runs and prints out:<\/p>\n<p>I can make a sound<\/p>\n<p>What happens is that the function <i>MakeSound()<\/i> of the Animal class is called.<\/p>\n<p><b>Step 6:<\/b> Now add three more classes to your program and name them:&nbsp; <i>Cat<\/i>, <i>Dog<\/i> and <i>Cow<\/i>. Add function <i>MakeSound()<\/i> to each of them but with different implementations.<\/p>\n<p>For the Cat class, the&nbsp; implementation would be:<br \/><span style=\"color: #351c75;\"><span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">&nbsp;System.out.println(&#8220;I can make a sound, meew! meew!);<\/span><\/span><\/p>\n<p>For the Dog:<\/p>\n<p><span style=\"color: #351c75;\"><span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">System.out.println(&#8220;I can make a sound, wow! wow!);<\/span><\/span><\/p>\n<p>For the Cow:<\/p>\n<p><span style=\"color: #351c75;\"><span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">System.out.println(&#8220;I can make a sound, Moo! Moo!);<\/span><\/span><\/p>\n<p><b>Step 7:<\/b> Now go to the main program and create objects of these classes. Call the <i>MakeNoise()<\/i> method of each class and make sure it work<\/p>\n<p><b>Polymorphism in Action<\/b><br \/>Lets now see how polymorphism works in all of these<\/p>\n<p><b>Step 8:<\/b> In your main function, create a cat object of the type Animal. Like this:<\/p>\n<p><span style=\"color: #351c75;\"><span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">Animal myCat = new Cat();&nbsp;<\/span><\/span><br \/><span style=\"color: #351c75;\"><span style=\"font-family: &quot;Courier New&quot;, Courier, monospace;\">myCat.MakeNoise();<\/span><\/span><\/p>\n<p>Step 9: Run this program and observe that the <i>MakeNoise()<\/i> function of Cat is called<\/p>\n<p><b>Step 10<\/b>: Repeat the same for Dog and Cow and observe that the right function of each of them is called<\/p>\n<p>The three objects <i>myCat<\/i>, <i>myDog <\/i>and <i>myCow <\/i>are of the same time Animal.&nbsp; But when the <i>MakeNoise()<\/i> is called, the correct function is called based on the class it was created from. So we can say that the <i>MakeNoise()<\/i> function is a polymorphous function, that is same function but different behaves in different ways.<\/p>\n<p>However, the concept of Polymorphism is not limited to just Polymorphous functions. In the next lesson, we would discuss another aspect of polymorphism and that is, Polymorphous Arrays.<\/p>\n<p>&nbsp;<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, we are going to discuss the concept of polymorphism in the Java programming language. What is PolymorphismPolymorphism means the ability to exist &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\/231"}],"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=231"}],"version-history":[{"count":0,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/231\/revisions"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}