There is just one question in which i am got confused... There are two interfaces A , B which contains the same method methodC()...and C class implements A&B. which interface method it should be implemented A or B
Now as per my analysis i have lyk say below class..
interface A {
/**
* Doc A
* **/
public void MethodA();
public String MethodB();
}
interface B {
/**
* Doc B
* **/
public String MethodA();
public void MethodB();
}
class lucy implements A,B{
@Override
public String MethodA() {
//To change body of implemented methods use File | Settings | File Templates.
return null;
}
@Override
public String MethodB() {
//To change body of implemented methods use File | Settings | File Templates.
return null;
}
}
Output:
Hello Hello
Let's assume we have an interface:
public interface A {
public void firstMethod();
public void secondMethod();
}
and this interface:
public interface B {
public void firstMethod();
public void secondMethod();
}
If you make a class, that implements both of these interfaces, it will be compiled and work ok.
public class MyClass implements A, B {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
@Override
public void firstMethod() {
// some code here
}
@Override
public void secondMethod() {
// some code here
}
}
But if you add methods firstMethod()
and secondMethod()
to the MyClass
but with different return type, it won't compile.
public class MyClass implements A, B {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
public String firstMethod() {
return "";
}
public String secondMethod() {
return "";
}
@Override
public void firstMethod() {
// some code here
}
@Override
public void secondMethod() {
// some code here
}
}
To compose method signature Java uses method parameters, but not the return type. And it will lead to "method is already defined" error. Because from Java's point of view methods: void firstMethod();
and String firstMethod();
(the same for the secondMethod()
method) have the same signatures.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.