Home Tutorial Java Scjp Part6 SCJP Module-6 Question-15

 
 

Share on Google+Share on Google+
SCJP Module-6 Question-15
Posted on: July 14, 2010 at 12:00 AM
Advertisement
The Sample program given below will test your knowledge of Inheritance in Java and it will also test that how to override method properly in Java.

Given below the sample code :

1 class SuperClass {
2 public void MyMethod()  {
3 System.out.print("SuperClass,");
4 }}
5 class SubClass extends SuperClass {
6 public void MyMethod() throws IOException {
7 super.MyMethod();
8 System.out.print("SubClass,");
9 throw new IOException();
10 }
11 public static void main(String[] args) {
12 try {
13 new SubClass().MyMethod();
14} catch (IOException e) {
15 System.out.println("Exception");
16 }}}

How can we correct the above code ?

1. No need of correction.

2. By adding "throws IOException" at line 2

3.By adding "import java .io.*;" at line 1.

4.by adding "throws Exception" at line 2.

Answer :

(2) and(3)

Explanation :

Above code will give compile error because override method can't throws any exception ,which was not thrown by overridden method and to throw "IOException" you have to import "java.io" package.

 

Advertisement

Related Tags for SCJP Module-6 Question-15:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 14, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com