1 2 Previous Next 16 Replies Latest reply: Feb 2, 2010 3:33 AM by 843789 RSS

    Why is the Super Class method not called in the  code even after casting ?

    843789
      This is my code :
      package exceptions;
      
      class SuperExcp41
      {
           public void process()
           {
                System.out.println("A");
           }
      }
      
      public class Excp41 extends SuperExcp41{
           public static void main(String[] args) {
                try
                {
                     ((SuperExcp41) new Excp41()).process();
                }
                catch(Exception e)
                {
                     System.out.println("Exception");
                }
           }
           public void process() throws RuntimeException
           {
                super.process();
                if(true)
      
                     throw new RuntimeException();
                System.out.println("B");
      
           }
      }
      O/P :
      A
      Exception
      My question is ,Even after we cast Excp41 object to SuperExcp41,why does the process() of the subclass ,that is ,Excp41 get called ?

      Thank you.
        1 2 Previous Next