Tuesday, February 24, 2009

parent child relation checked exceptions issues in overriding



//In Exception Hierarchy IOException super class,FileNotFoundException subclass .
/* While overriding the size of the CheckedException
should not increase */
DEMO 1:
package oops;

import java.io.FileNotFoundException;
import java.io.IOException;

class Parentclass
{
public void sm()throws IOException
{
System.out.println("super class");
}
}
public class OREx extends Parentclass{

public void sm()throws FileNotFoundException // no error
{
System.out.println("sub class");
}
public static void main(String a[])throws Exception
{
OREx s=new OREx();
s.sm(); // sub class
}
}

Demo 2:
package oops;

import java.io.FileNotFoundException;
import java.io.IOException;

class Parentclass
{
public void sm()throws FileNotFoundException
{
System.out.println("super class");
}
}
public class OREx extends Parentclass{

public void sm()throws IOException // compile time error
// Exception IOException is not compatible with throws clause
{
System.out.println("sub class");
}
public static void main(String a[])throws Exception
{
OREx s=new OREx();
s.sm();
}
}

DEMO 3:
package oops;

import java.io.FileNotFoundException;
import java.io.IOException;

class Parentclass
{
public void sm()throws FileNotFoundException
{
System.out.println("super class");
}
}
public class OREx extends Parentclass{

public void sm()throws RuntimeException // no error
{
System.out.println("sub class");
}
public static void main(String a[])throws Exception
{
OREx s=new OREx();
s.sm(); // sub class
}
}



0 comments:

Post a Comment