{"id":329,"date":"2017-06-09T11:54:08","date_gmt":"2017-06-09T11:54:08","guid":{"rendered":"http:\/\/www.codeinsightacademy.com\/blog\/?p=329"},"modified":"2017-06-09T11:54:08","modified_gmt":"2017-06-09T11:54:08","slug":"polymorphism","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/java\/polymorphism\/","title":{"rendered":"polymorphism"},"content":{"rendered":"<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class A {\r\n    public void test(){\r\n        System.out.println(\"hello\");\r\n    }\r\n    \r\n    public void foo(){\r\n        System.out.println(\"fooo\");\r\n    }\r\n}\r\n\r\nclass B extends A {\r\n    @Override\r\n    public void test(){\r\n        System.out.println(\"world\");\r\n    }\r\n    public void bar(){\r\n        System.out.println(\"baar\");\r\n    }\r\n}\r\n\r\n\r\npublic class PolymorphismDemo {\r\n\r\n    public static void main(String[] args) {\r\n        \r\n        A a = new A();\r\n        a.test(); \/\/ allowed: will print hello\r\n        a.foo(); \/\/ allowed: will print fooo\r\n        \/\/a.bar(); \/\/ not allowed: compile time error\r\n        \r\n        B b = new B();\r\n        b.test(); \/\/ allowed: will print world\r\n        b.foo(); \/\/ allowed: will print fooo\r\n        b.bar(); \/\/ allowed: will print baar\r\n        \r\n        A c = new B();\r\n        c.test(); \/\/ allowed: will print world\r\n        c.foo(); \/\/ allowed: will print fooo\r\n        \/\/c.bar(); \/\/ not allowed: compile time error\r\n        \r\n        \/\/B d = new A(); \/\/ not allowed: compile time error\r\n        \r\n        A e = new B();\r\n        A f = (A)e;\r\n        f.test(); \/\/ allowed: will print world\r\n        f.foo(); \/\/ allowed: will print fooo\r\n        \/\/f.bar(); \/\/ not allowed: compile time error\r\n        \r\n        B g = new B();\r\n        A h = (A)g;\r\n        h.test(); \/\/ allowed: will print world\r\n        h.foo(); \/\/ allowed: will print fooo\r\n        \/\/h.bar(); \/\/ not allowed: compile time error\r\n    }    \r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>class A { public void test(){ System.out.println(&#8220;hello&#8221;); } public void foo(){ System.out.println(&#8220;fooo&#8221;); } } class B extends A { @Override public void test(){ System.out.println(&#8220;world&#8221;); } public void bar(){ System.out.println(&#8220;baar&#8221;); } } public class PolymorphismDemo { public static void main(String[] args) { A a = new A(); a.test(); \/\/ allowed: will print hello a.foo(); \/\/ allowed: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/329"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=329"}],"version-history":[{"count":1,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/329\/revisions"}],"predecessor-version":[{"id":330,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/329\/revisions\/330"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}