checked exception issues in method overriding Examples
Demo1:
// super class method throws unchecked exception
// sub class method no throws
package oops;
class Parentclass
{
public void sm()throws Exception
{
System.out.println("super class");
}
}
public class OREx extends Parentclass{
public void sm() // valid
{
System.out.println("sub class");
}
public static void main(String a[])
{
OREx s=new OREx();
s.sm(); // sub class
}
}
Demo2:
// super class ,Sub class methods throws unchecked exception
package oops;
class Parentclass
{
public void sm()throws Exception
{
System.out.println("super class");
}
}
public class OREx extends Parentclass
//Super ,Sub classes throws same exception noerror
public void sm()throws Exception
{
System.out.println("sub class");
}
public static void main(String a[])throws Exception
{
OREx s=new OREx();
s.sm(); // sub class
}
}
Demo3:
// super class method no throws
// sub class method throws unchecked exception
package oops;
class Parentclass
{
public void sm()
{
System.out.println("super class");
}
}
public class OREx extends Parentclass{
public void sm()throws Exception // compile time error
//saying Exception Exception 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();
}
}
1 comments:
nice example very thanks!!
Post a Comment