• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pl explain me
interface AnInterface
{
public void methodOne() throws Exception;
}
class AnInterfaceImpl implements AnInterface
{
public void methodOne()
{
System.out.println("I will never throw an exception");
}
}
public class ATest
{
public static void main(String args[])
{
//AnInterface ai = new AnInterfaceImpl();
//AnInterfaceImpl ai = new AnInterfaceImpl();
ai.methodOne();
}
}
1. AnInterImpl class methodOne() does not throw any Exception. Is it proper to implement this way?
2. when u uncomment the 2nd line, it displays "I will never throw...exception", but when u uncomment first line, causes a compile time error. ( Exception must be caught or thrown by main(String))
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.You can implement this way
2.because ai in 2nd line compile-time type is AnInterfaceImpl
so,compiler see methodOne not throw an exception
but 1st line compile-time type of ai is AnInterface so, compiler see methodOne throw an exception if u not catch exception
Compile-time error occur
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic