JSP Java Tutorial

< HTML>
    
        Runtime Polymorphism
    
    
        

Runtime Polymorphism


        <%!
            javax.servlet.jsp.JspWriter localOut;
            class BaseClass
            {
                public void print() throws java.io.IOException 
                {
                    localOut.println("Hello from BaseClass...
");
                }
            }
            class DerivedClass1 extends BaseClass
            {
                public void print() throws java.io.IOException 
                {
                    localOut.println("Hello from DerivedClass1...
");
                }
            }
            class DerivedClass2 extends BaseClass
            {
                public void print() throws java.io.IOException 
                {
                    localOut.println("Hello from DerivedClass2...
");
                }
            }
            class DerivedClass3 extends BaseClass
            {
                public void print() throws java.io.IOException 
                {
                    localOut.println("Hello from DerivedClass3...
");
                }
            }
        %>     
        <%
            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();
        %>