{"id":1937,"date":"2011-04-08T00:13:19","date_gmt":"2011-04-07T18:43:19","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1937"},"modified":"2011-04-08T00:13:19","modified_gmt":"2011-04-07T18:43:19","slug":"virtual-function-in-java","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/virtual-function-in-java\/","title":{"rendered":"Virtual Function in JAVA"},"content":{"rendered":"<p>The programmers coming from c++ background to Java normally think that where is the Virtual function? \u00a0<span style=\"text-decoration: underline;\">In Java there is no keyword names &#8220;<strong>virtual<\/strong>&#8220;.<\/span><\/p>\n<p><strong>Definition of Virtual from wiki:<\/strong><\/p>\n<blockquote><p><strong>In object-oriented programming, a virtual function or virtual method is a function or method whose behaviour can be overridden within an inheriting class by a function with the same signature to provide the polymorphic behavior.<\/strong><\/p><\/blockquote>\n<p>Therefore according to definition, every <strong>non-static method<\/strong> in JAVA is by default <em>virtual method<\/em> <strong>except final and private methods<\/strong>. The methods which cannot be inherited for polymorphic behavior is not a virtual method.<!--more--><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\nimport java.util.*;\n\npublic class Animal\n{\n   public void eat()\n   {\n      System.out.println(&quot;I eat like a generic Animal.&quot;);\n   }\n\n   public static void main(String&#x5B;] args)\n   {\n      List&lt;Animal&gt; animals = new LinkedList&lt;Animal&gt;();\n\n      animals.add(new Animal());\n      animals.add(new Wolf());\n      animals.add(new Fish());\n      animals.add(new Goldfish());\n      animals.add(new OtherAnimal());\n\n      for (Animal currentAnimal : animals)\n      {\n         currentAnimal.eat();\n      }\n   }\n}\n\npublic class Wolf extends Animal\n{\n   @Override\n   public void eat()\n   {\n      System.out.println(&quot;I eat like a wolf!&quot;);\n   }\n}\n\npublic class Fish extends Animal\n{\n   @Override\n   public void eat()\n   {\n      System.out.println(&quot;I eat like a fish!&quot;);\n   }\n}\n\npublic class Goldfish extends Fish\n{\n   @Override\n   public void eat()\n   {\n      System.out.println(&quot;I eat like a goldfish!&quot;);\n   }\n}\n\npublic class OtherAnimal extends Animal {}\n\n<\/pre>\n<p>Output:<\/p>\n<blockquote><p>I eat like a generic Animal.<br \/>\nI eat like a wolf!<br \/>\nI eat like a fish!<br \/>\nI eat like a goldfish!<br \/>\nI eat like a generic Animal.<\/p><\/blockquote>\n<p><strong><span style=\"text-decoration: underline;\">Abstract class<\/span> is nothing but the<span style=\"text-decoration: underline;\"> pure virtual method<\/span> equivalent to C++ in Java.<\/strong><\/p>\n<hr \/>\n<p><strong>Question : why we say that static method is not a virtual method in Java?<\/strong><\/p>\n<p>Answer : static method is bound to the class itself, so calling the static method from class name or object does not provide the polymorphic behavior to the static method. We can override the static method however it will not give the advantage of the polymorphism.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Example and explanation of Virtual Function in JAVA<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[150],"class_list":["post-1937","post","type-post","status-publish","format-standard","hentry","category-java","tag-oops"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":2017,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-j2ee-interview-questions-1\/","url_meta":{"origin":1937,"position":0},"title":"Java &#8211; J2EE Interview Questions &#8211; 1","author":"Jitendra","date":"April 15, 2011","format":false,"excerpt":"JAVA - J2EE Interview Questions - 1, JAVA - J2EE Interview Questions - 1,custom tag JSP, Externalization , serialVersionUID, difference between interface and abstract class, iterate HashMap","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":702,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/difference-between-interfaceinheritance-abstract-class\/","url_meta":{"origin":1937,"position":1},"title":"Interface, Inheritance and abstract class","author":"Jitendra","date":"August 6, 2010","format":false,"excerpt":"What is abstract class, interface and inheritance in java. Source code and example","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Inheritance, Interface and abstract class","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/Inheritance1-300x173.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1638,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/exception-chaining\/","url_meta":{"origin":1937,"position":2},"title":"Exception chaining in Java","author":"Jitendra","date":"March 14, 2011","format":false,"excerpt":"Concept of Exception chaining with example in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Exception Chaining","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Exception-Chaining.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1933,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/csharp\/virtual-override-and-new-keyword-in-c\/","url_meta":{"origin":1937,"position":3},"title":"Virtual, Override and new Keyword in C#","author":"Jitendra","date":"April 7, 2011","format":false,"excerpt":"Virtual, Override and new Keyword : Polymorphism , Method Hiding and Overriding in C#","rel":"","context":"In &quot;c#&quot;","block_context":{"text":"c#","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/csharp\/"},"img":{"alt_text":"Multilevel Inheritance","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/Multilevel-Inheritance.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":460,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-destructor-finalize\/","url_meta":{"origin":1937,"position":4},"title":"Java Destructor &#8211; finalize()","author":"Jitendra","date":"July 1, 2010","format":false,"excerpt":"Concept of destructor in JAVA. finalize() method.","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Java Destructor finalize()","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/Trash.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":757,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/anonymous-classimplements-interface-methods-of-threads\/","url_meta":{"origin":1937,"position":5},"title":"Create Thread using Anonymous class and Interface","author":"Jitendra","date":"August 16, 2010","format":false,"excerpt":"Demonstration of creating Thread by Anonymous class and Interface","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1937","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1937"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1937\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}