Connecting Tech Pros Worldwide Help | Site Map

why does this code show an error

Newbie
 
Join Date: Oct 2009
Posts: 1
#1: Oct 2 '09
Expand|Select|Wrap|Line Numbers
  1. class A {
  2.  public void process() { System.out.print(”A,”); } }
  3. class B extends A {
  4.  public void process() throws IOException {
  5.  super.process();
  6.  System.out.print(”B,”);
  7.  throw new IOException();
  8.  } }
  9. class C{
  10.  public static void main(String[] args) {
  11.  try { new B().process(); }
  12.  catch (IOException e) { System.out.println(”Exception”); } }
  13. }
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#2: Oct 2 '09

re: why does this code show an error


It'd be much more helpful of you to give the error text.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#3: Oct 2 '09

re: why does this code show an error


Quote:

Originally Posted by ishween2k View Post

class A {

Expand|Select|Wrap|Line Numbers
  1.  public void process() { System.out.print(”A,”); } }
  2. class B extends A {
  3.  public void process() throws IOException {
  4.  super.process();
  5.  System.out.print(”B,”);
  6.  throw new IOException();
  7.  } }
  8. class C{
  9.  public static void main(String[] args) {
  10.  try { new B().process(); }
  11.  catch (IOException e) { System.out.println(”Exception”); } }
  12. }

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)
Reply