cancel
Showing results for 
Search instead for 
Did you mean: 

question from package

Former Member
0 Kudos

hello i m a beginner in java trying 4 a job. Kindly help me in solving these question.

wat happens when you attempt to compile and run these two files in the same directory ?

//File P1.java

package MyPackage;

class P1{

void afancymethod(){

System.out.println("What a fancy method");

}

//File P2 .java

public class P2 extends P1{

public static void main(string argv[]){

P2 p2= new P2();

p2.afancymethod();

}

}

Options---

1.Both compile and P2 outputs "What a fancy method" when run

2.Neither will compile

3.Both compile but P2 has an error at run time

4.P1 compiles cleanly but P2 as error at compile time

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Payal,

Just check the file if it has closing braces. if the first file is

package MyPackage;
 class P1{
  void afancymethod(){
   System.out.println("What a fancy method");
  }
 <b>}</b>

Then option 4 is correct ie. P1 compiles cleanly but P2 as error at compile time

But if there is no closing braces for the class P1 then option 2 is correct ie neither will get complied.

If you find my answers helpful, award points.

Regards,

Richa

Answers (0)