• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Ques from Abhilash 'ssite

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The following code is for Questions 81 and 82

//Contents of File AnInterface.java
public interface AnInterface
{
public void methodOne() throws Exception;
}

//Contents of File AnInterfaceImpl.java
public class AnInterfaceImpl implements AnInterface
{
public void methodOne()
{
System.out.println("I will never throw an exception");
}
}
Question 81.
Read the code below carefully.
public class ATest
{
public static void main(String args[])
{
AnInterface ai = new AnInterfaceImpl();
ai.methodOne();
}
}
Attempting to compile and run the above code
1) Will cause a compile time error. (Line 5 : Exception must be caught or thrown by main(String))
2) Will cause a compile time error for Class AnInterfaceImpl. The method methodOne() be declared with "throws Exception".
3) Will cause no compile time error and print "I will never throw and Exception the screen".
Will Cause a run time error .
-----------------------------------------------------------------
Question 82.
Read the code below carefully.
public class ATest
{
public static void main(String args[])
{
AnInterfaceImpl ai = new AnInterfaceImpl();
ai.methodOne();
}
}
Attempting to compile and run the above code
1) Will cause a compile time error. (Line 5 : Exception must be caught or thrown by main(String))
2) Will cause a compile time error for Class AnInterfaceImpl. The method methodOne() be declared with "throws Exception".
3) Will cause no compile time error and print "I will never throw and Exception the screen".
Will Cause a run time error .

Ans 81)1
82)3
Can someone please explain me the output.
Thanks in advance.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
make two seperate posts.
Some of us have to work, How much spare time do you think we have?
 
Chaitali Deshpande
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
greg
I gave them in single post because both question are related.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<code>
public class Test {
public static void main(String args[]) {
Parent obj = new Child();
obj.p();
}
}
class Parent {
}
class Child extends Parent {
void p() {
System.out.println("I am Child");
}
}
</code>
Remember that the above code won't compile??
When compilation, all checking is based on the object reference. Late Binding is only occured at runtime.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chaitali Deshpande,
As Wong KK is correct at the compilation time, Compiler doesn't know about the child class object's method(In you case It only aware about the Interface method's Signature which requirs to catch the Exception that throws).
Just Check this code.......... It is also the Simmiller.

Hope you will be Clear Now.
-Siva
 
He does not suffer fools gladly. But this tiny ad does:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic