Java 为什么实现接口的抽象类会错过接口方法之一的声明/实现?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/197893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why an abstract class implementing an interface can miss the declaration/implementation of one of the interface's methods?
提问by Giulio Piancastelli
A curious thing happens in Java when you use an abstract class to implement an interface: some of the interface's methods can be completely missing (i.e. neither an abstract declaration or an actual implementation is present), but the compiler does not complain.
当您使用抽象类来实现接口时,Java 中会发生一件奇怪的事情:接口的某些方法可能完全丢失(即既不存在抽象声明也不存在实际实现),但编译器不会抱怨。
For example, given the interface:
例如,给定接口:
public interface IAnything {
void m1();
void m2();
void m3();
}
the following abstract class gets merrily compiled without a warning or an error:
以下抽象类在没有警告或错误的情况下被愉快地编译:
public abstract class AbstractThing implements IAnything {
public void m1() {}
public void m3() {}
}
Can you explain why?
你能解释一下为什么吗?
回答by Bill the Lizard
That's because if a class is abstract, then by definition you are required to create subclasses of it to instantiate. The subclasses will be required (by the compiler) to implement any interface methods that the abstract class left out.
这是因为如果一个类是抽象的,那么根据定义,您需要创建它的子类来实例化。子类将需要(由编译器)来实现抽象类遗漏的任何接口方法。
Following your example code, try making a subclass of AbstractThing
without implementing the m2
method and see what errors the compiler gives you. It will force you to implement this method.
按照您的示例代码,尝试在AbstractThing
不实现该m2
方法的情况下创建一个子类,并查看编译器给您带来的错误。它会强迫你实现这个方法。
回答by Gishu
Perfectly fine.
You can't instantiate abstract classes.. but abstract classes can be used to house common implementations for m1() and m3().
So ifm2() implementation is different for each implementation but m1 and m3 are not. You could create different concrete IAnything implementations with just the different m2 implementation and derive from AbstractThing -- honoring the DRY principle. Validating if the interface is completely implemented for an abstract class is futile..
完全没问题。
你不能实例化抽象类……但是抽象类可以用来容纳 m1() 和 m3() 的通用实现。
因此,如果每个实现的 m2() 实现不同,但 m1 和 m3 不是。您可以使用不同的 m2 实现创建不同的具体 IAnything 实现,并从 AbstractThing 派生出来——尊重 DRY 原则。验证接口是否完全为抽象类实现是徒劳的。
Update: Interestingly, I find that C# enforces this as a compile error. You are forced to copy the method signatures and prefix them with 'abstract public' in the abstract base class in this scenario.. (something new everyday:)
更新:有趣的是,我发现 C# 将此作为编译错误强制执行。在这种情况下,您被迫复制方法签名并在抽象基类中使用“抽象公共”作为前缀..(每天都有新的东西:)
回答by Vincent Ramdhanie
Abstract classes are not required to implement the methods. So even though it implements an interface, the abstract methods of the interface can remain abstract. If you try to implement an interface in a concrete class (i.e. not abstract) and you do not implement the abstract methods the compiler will tell you: Either implement the abstract methods or declare the class as abstract.
不需要抽象类来实现这些方法。所以即使它实现了一个接口,接口的抽象方法也可以保持抽象。如果您尝试在具体类(即非抽象类)中实现接口并且您没有实现抽象方法,编译器会告诉您:要么实现抽象方法,要么将类声明为抽象类。
回答by Mustakimur Rahman
Interface means a class that has no implementation of its method, but with just declaration.
Other hand, abstract class is a class that can have implementation of some method along with some method with just declaration, no implementation.
When we implement an interface to an abstract class, its means that the abstract class inherited all the methods of the interface. As, it is not important to implement all the method in abstract class however it comes to abstract class (by inheritance too), so the abstract class can left some of the method in interface without implementation here. But, when this abstract class will inherited by some concrete class, they must have to implements all those unimplemented method there in abstract class.
接口意味着一个没有实现其方法,而只有声明的类。
另一方面,抽象类是一个类,它可以实现一些方法以及一些只有声明的方法,没有实现。
当我们为抽象类实现接口时,意味着抽象类继承了接口的所有方法。因为,在抽象类中实现所有方法并不重要,但它涉及到抽象类(也通过继承),因此抽象类可以在接口中留下一些方法而不在这里实现。但是,当这个抽象类被某个具体类继承时,他们必须在抽象类中实现所有那些未实现的方法。
回答by Grateful
That's fine. To understand the above, you have to understand the nature of abstract classes first. They are similar to interfaces in that respect. This is what Oracle say about this here.
没关系。要理解上述内容,您必须首先了解抽象类的性质。在这方面,它们类似于接口。这就是 Oracle在这里所说的。
Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation.
抽象类类似于接口。您不能实例化它们,它们可能包含声明有或没有实现的方法的混合。
So you have to think about what happens when an interface extends another interface. For example ...
所以你必须考虑当一个接口扩展另一个接口时会发生什么。例如 ...
//Filename: Sports.java
public interface Sports
{
public void setHomeTeam(String name);
public void setVisitingTeam(String name);
}
//Filename: Football.java
public interface Football extends Sports
{
public void homeTeamScored(int points);
public void visitingTeamScored(int points);
public void endOfQuarter(int quarter);
}
... as you can see, this also compiles perfectly fine. Simply because, just like an abstract class, an interface can NOT be instantiated. So, it is not required to explicitly mention the methods from its "parent". However, ALL the parent method signatures DO implicitly become a part of the extending interface or implementing abstract class. So, once a proper class (one that can be instantiated) extends the above, it WILL be required to ensure that every single abstract method is implemented.
...正如你所看到的,这也编译得很好。仅仅是因为,就像抽象类一样,接口不能被实例化。因此,不需要明确提及其“父级”的方法。但是,所有父方法签名都隐式地成为扩展接口或实现抽象类的一部分。因此,一旦适当的类(可以实例化的类)扩展了上述内容,就需要确保实现每个抽象方法。
Hope that helps... and Allahu 'alam !
希望有帮助......真主啊!
回答by Do Nhu Vy
When an Abstract Class Implements an Interface
In the section on Interfaces, it was noted that a class that implements an interface must implement all of the interface's methods. It is possible, however, to define a class that does not implement all of the interface's methods, provided that the class is declared to be abstract. For example,
当抽象类实现接口时
在接口部分,注意到实现接口的类必须实现接口的所有方法。但是,可以定义一个不实现所有接口方法的类,前提是该类声明为抽象类。例如,
abstract class X implements Y {
// implements all but one method of Y
}
class XX extends X {
// implements the remaining method in Y
}
In this case, class X must be abstract because it does not fully implement Y, but class XX does, in fact, implement Y.
在这种情况下,类 X 必须是抽象的,因为它没有完全实现 Y,但类 XX 实际上实现了 Y。
Reference: http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
参考:http: //docs.oracle.com/javase/tutorial/java/IandI/abstract.html
回答by sharhp
Given the interface:
鉴于接口:
public interface IAnything {
int i;
void m1();
void m2();
void m3();
}
This is how Java actually sees it:
这是 Java 的实际看法:
public interface IAnything {
public static final int i;
public abstract void m1();
public abstract void m2();
public abstract void m3();
}
So you can leave some (or all) of these abstract
methods unimplemented, just as you would do in the case of abstract
classes extending another abstract
class.
因此,您可以保留一些(或全部)这些abstract
方法未实现,就像您在abstract
类扩展另一个abstract
类的情况下所做的那样。
When you implement
an interface
, the rule that all interface
methods must be implemented in the derived class
, applies only to concrete class
implementation (i.e., which isn't abstract
itself).
当您implement
使用 时interface
,所有interface
方法必须在派生中实现的规则class
仅适用于具体class
实现(即,它abstract
本身不是)。
If you indeed plan on creating an abstract class
out of it, then there is no rule that says you've to implement
all the interface
methods (note that in such a case it is mandatory to declare the derived class
as abstract
)
如果您确实计划abstract class
从中创建一个,那么没有规则说您必须使用implement
所有interface
方法(请注意,在这种情况下,必须将派生声明class
为abstract
)