Saturday, 17 December 2011

JSP Examples Tutorial - Runtime Polymorphism


<HTML>
    <HEAD>
        <TITLE>Runtime Polymorphism</TITLE>
    </HEAD>
    <BODY>
        <H1>Runtime Polymorphism</H1>
        <%!
            javax.servlet.jsp.JspWriter localOut;
            class BaseClass
            {
                public void print() throws java.io.IOException 
                {
                    localOut.println("Hello from BaseClass...<BR>");
                }
            }
            class DerivedClass1 extends BaseClass
            {
                public void print() throws java.io.IOException 
                {
                    localOut.println("Hello from DerivedClass1...<BR>");
                }
            }
            class DerivedClass2 extends BaseClass
            {
                public void print() throws java.io.IOException 
                {
                    localOut.println("Hello from DerivedClass2...<BR>");
                }
            }
            class DerivedClass3 extends BaseClass
            {
                public void print() throws java.io.IOException 
                {
                    localOut.println("Hello from DerivedClass3...<BR>");
                }
            }
        %>     
        <%
            localOut = out;     
 
            BaseClass a1 = new BaseClass(); 
            DerivedClass1 b1 = new DerivedClass1(); 
            DerivedClass2 c1 = new DerivedClass2(); 
            DerivedClass3 d1 = new DerivedClass3(); 
 
            BaseClass baseClassVariable;
 
            baseClassVariable = a1;
            baseClassVariable.print();
 
            baseClassVariable = b1;
            baseClassVariable.print();
 
            baseClassVariable = c1;
            baseClassVariable.print();
 
            baseClassVariable = d1;
            baseClassVariable.print();
        %>
    </BODY>
</HTML>

1 comment:

  1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java EE Online Training Students.

    ReplyDelete