why does this code show an error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ishween2k
    New Member
    • Oct 2009
    • 1

    why does this code show an error

    Code:
    class A {
     public void process() { System.out.print(”A,”); } }
    class B extends A {
     public void process() throws IOException {
     super.process();
     System.out.print(”B,”);
     throw new IOException();
     } }
    class C{
     public static void main(String[] args) {
     try { new B().process(); }
     catch (IOException e) { System.out.println(”Exception”); } }
    }
    Last edited by Frinavale; Oct 2 '09, 08:34 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    It'd be much more helpful of you to give the error text.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by ishween2k
      class A {
      Code:
       public void process() { System.out.print(”A,”); } }
      class B extends A {
       public void process() throws IOException {
       super.process();
       System.out.print(”B,”);
       throw new IOException();
       } }
      class C{
       public static void main(String[] args) {
       try { new B().process(); }
       catch (IOException e) { System.out.println(”Exception”); } }
      }
      Those double quotes are incorrect; you probably used Microsoft Word or something to type that in, don't do that. A second error is your widening of the throws clause for method 'process' in class B. Look up the Liskov Substitution principle and you'll see.

      kind regards,

      Jos (ex-moderator)

      Comment

      Working...