• 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

interface problem

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


here object a1 is of tyep AnInterface but reference of AnInterfaceImpl.
Call method on this object is giving compile time error methodOne do not catch or throw Exception.
But why

this compiles and with out and error or exception.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,payal
In statement
AnInterface ai = new AnInterfaceImpl();
compile-time type of ai is AnInterface that methodOne() throws an
Exception if you not catch it compile-time error occur,
but statement
AnInterfaceImpl ai = new AnInterfaceImpl();
compile-time type is AnInterfaceImpl that methodOne() dosen't throws an Exception so it compiles without error
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To do away with the problem we could have a1.methodOne() in a try catch block or we could simply use a throws with the main() in ATest .
Thanx , it cleared my doubt aswell .
Rest w/ George
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Would like to add that it is always better to program to an interface rather than program to a class.The advantage you get is you can change the stub without any requirement for client compilation.This cannot be possible when you program to a class.
Hence,

AnInterface ai = new AnInterfaceImpl();

is better than

AnInterfaceImpl aii = new AnInterfaceImpl();

It is bad programming to implement the methodOne() without throwing an exception.Remember that the interface signifies responsibilities which every implementing class must adhere to.

Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
[This message has been edited by Desai Sandeep (edited July 25, 2001).]
 
what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
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