File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interfaces doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interfaces doubt" Watch "Interfaces doubt" New topic
Author

Interfaces doubt

Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
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
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


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

Thanks,


cmbhatt
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
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");}
}
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: May 04, 2007
Posts: 332
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

Joined: May 04, 2007
Posts: 332
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

Joined: Feb 28, 2007
Posts: 1707
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,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interfaces doubt