• 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

Interfaces doubt

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

Source : TeamTesting



import java.io.IOException;

interface myInterface1 {
public void myMethod(int x) throws IOException;
}

interface myInterface2 {
public void myMethod(int x) throws NullPointerException;
}

public class ObjectTest implements myInterface1, myInterface2 {
public void myMethod(int x) {}
}

In the above code the class "ObjectTest" will override which of the two interfaces method?


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


Now you tell, which is overridden by the ObjectTest class?

Thanks,
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,
I got that and in the below code of which interface "ObjectClass" override



interface myInterface1 {
public void myMethod();
}

interface myInterface2 {
public void myMethod();
}

public class ObjectTest implements myInterface1, myInterface2 {
public void myMethod() {System.out.println("Implemented");}
}
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chandra Bhatt:
[CODE]

Now you tell, which is overridden by the ObjectTest class?



I guess, neither of them :-), both are only implemented.

By implementing interface you are saying, that you guarantee, that your class will have methods _compatible_ with those specified in interface.
Why am I saying compatible? Because you can narrow exceptions. (Increasing visibility make no sense, they are already public)

Why are we seeing those compile-errors? Because compiler checks for checked exceptions at compile time based on type you provided at compile time. There is no guarantee, that exceptions will be not thrown, because child is not throwing them. Compiler doesn't know.

nik: ObjectClass implements both interfaces with one method that conforms both interfaces. There is only one method. (at contrast to C++ multiple inheritance, where you might get in some trouble)
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,

I agree, the term overriding is not usable in case of interface
implementation. It is the contract between class and interface that the
class will give the definition (implementation) of the interface methods
with proper signature, that is public accessibility and not declaring any
broad checked exception to be thrown.


I take compatibility in the terms that the exception the implementing
class declares for the interface method must be compatible with all the
interfaces it implements (in case more than one interfaces have the same
method name and signature)




Obviously this does not apply with non-checked exceptions.


Thanks,
[ May 08, 2007: Message edited by: Chandra Bhatt ]
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chandra Bhatt:
Hi John,

I agree, the term overriding is not usable in case of interface
implementation.
[ May 08, 2007: Message edited by: Chandra Bhatt ]



I think, I said that before thinking about it properly.



Seems, that overriding is usable term. (Interface is just a pure abstract class)
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this:


It is legal to throw descendant of exception. (can be safely upcasted)
[ May 08, 2007: Message edited by: John Stone ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I get from overriding is polymorphic call of methods,
run time decision which method to select on behalf of the
object the reference variable referring to.

What I see in the code example (I posted some posts above),
that it only matters what reference variable you are
using to call the interface methods. ref type decides whether you
must declare or handle the exception declared to be thrown by the
interface method. But if you have implemented that interface without
declaring that exception(checked) what the interface says to be,
still you have to declare or handle it because you are using
interface ref variable to call that method (it is the time compiler
bothers you).

So far as implementing and overriding terms are considered, both
are used interchangeably but has difference. Overriding means
the JVM at run time, could choose your overridden method, not the
inherited from the parent hence polymorphism the most liked thing
or OOP as I think.



Thanks,
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic