Java接口是抽象类吗?
我正在做一些家庭作业,之前试卷上的一个问题要求命名给定 UML 图中的所有抽象类。 我想相当简单。 有1个抽象类和3个接口。 一般来说,这些接口是否符合抽象类的资格?
I'm working through some homework and a question on a previous exam paper asks to name all of the abstract classes in a given UML diagram. Fairly straightforward, I suppose. There are one abstract class and three interfaces. Do these interfaces qualify as abstract classes, in general?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
问题是,虽然从技术上讲,接口可以在 Java 等语言中表示为类,但我不会将它们视为类。
抽象的? 当然好。 班级? 不可以。
接口不能有构造函数,也不能有属性、字段、函数体等。接口不能被继承,它们是被实现的(同样,从技术上讲,实现接口实际上可能是在特定语言中继承它,但这不是我的观点.)接口更像是“契约”,因为它们不像类那样定义任何行为。
现在,如果这是作业,那么你不应该与老师争论这类事情。 只需检查您的讲义,看看老师对界面的定义中是否提到了“类”一词。
Thing is, while technically interfaces may be represented as classes in languages like Java, I wouldn't consider them classes.
Abstract? Hell yes. Class? No.
Interfaces cannot have constructors, neither properties, fields, function bodies, etc. Interfaces cannot be inherited, they are implemented (again, technically it might be true that implementing an interface is actually inheriting it in specific languages, but that's not my point.) Interfaces are more like 'contracts' as they do not define any behaviour whatsoever like classes.
Now if this is a homework then you shouldn't really argue about these sort of stuff with the teacher. Just check your lecture notes and see if the word "class" is mentioned anywhere in the your teacher's definition of interface.
所有接口都是确实抽象
实际上,您可以在接口中将方法声明为抽象方法。 ..除了任何“checkstyle”工具都会告诉您abstract关键字是多余的。 并且所有方法都是公开的。
如果一个类实现了一个接口但未实现其所有方法,则必须将其标记为抽象。 如果一个类是抽象的,则其子类之一应实现其未实现的方法。
为了回应其他答案,接口不是一个类 。
接口是一种引用类型,类似于类,只能包含常量、方法签名和嵌套类型。 没有方法体。 接口无法实例化,只能由类实现或由其他接口扩展。
接口不是类层次结构的一部分,尽管它们与类结合使用。
当您定义新接口时,您正在定义新的引用数据类型。 您可以在可以使用任何其他数据类型名称的任何地方使用接口名称。 如果定义一个类型为接口的引用变量,则分配给它的任何对象都必须是实现该接口的类的实例。
为了更好地解释为什么接口不是类,请考虑以下内容:
1/ 接口是一个值使用的类型
2/ 类用于对象
3/:
根据编译器的类型检查,类型(对象)仅影响哪些代码有效,但不影响代码实际执行的操作。
对象的类会影响代码的功能,因此第二行中的 a.toString() 调用返回一个看起来像日期的字符串,而不是看起来像“java.lang.Object@XXXXXXXX”的字符串。
由于接口是一种类型,因此它仅用于值,并且不会代表对象在运行时实际执行的操作。
All interface are indeed abstract
Actually, you can declare an method as abstract within an interface... except any 'checkstyle' tool will tell you the abstract keyword is redundant. And all methods are public.
If a class implements an interface and does not implement all its methods, it must be marked as abstract. If a class is abstract, one of its subclasses is expected to implement its unimplemented methods.
To echo other answers, an interface is not a class.
An interface is a reference type, similar to a class, that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.
Interfaces are not part of the class hierarchy, although they work in combination with classes.
When you define a new interface, you are defining a new reference data type. You can use interface names anywhere you can use any other data type name. If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface
To better explain why an interface is not a class, consider the following:
1/ an interface is a type used by values
2/ a class is for Objects
3/:
The type (Object) only affects what code is valid according to the compiler's type-checking, but not what the code actually does.
The class of the object affects what the code does, so that the a.toString() call in the second line returns a String that looks like a Date, not one that looks like "java.lang.Object@XXXXXXXX".
Since an Interface is a type, it is used for values only, and will not represent what objects will actually do in term of runtime.
但在 Java 中,故事有一个转折——Java 中的所有接口都扩展了 java.lang.Object! 尝试添加一个方法:
public void notification();
在接口中,看看会发生什么。
扩展类的接口 - 这会使接口成为类吗? 或者类接口? 嗯嗯..我猜这是一个必须的黑客行为,以防止接口覆盖 java.lang.Object 中的定义,而接口的实现无论如何都必须扩展。
In Java though, theres a twist to the tale - all Interfaces in Java extend java.lang.Object! Try adding a method:
public void notify();
in an interface and see what happens..
An Interface extending a Class - Does that make the Interface a Class? Or the Class an Interface?? Uhh-huh.. Guess it was a hack that had to be done to prevent interfaces overriding the definitions in java.lang.Object that implementations of the interface had to extend anyway.
您只询问了抽象方面,但不要忘记类方面 - 我不会将接口称为类,因此即使接口是抽象的(根据规范),我仍然不认为它们算作抽象类。 不过,可能值得明确解释一下:)
You've only asked about the abstract side, but don't forget the class side - I wouldn't call an interface a class, so even though interfaces are abstract (as per the specification), I still don't think they count as abstract classes. It may be worth explicitly explaining that though :)
是的,接口隐式是抽象的。 查看幕后如何将其编码为
.class
文件。不过,语义是一件有趣的事情。 在考试条件下,“抽象类”必须从字面上使用类声明中的
抽象类
从.java
源文件进行编译。Yes, an Interface is implicitly Abstract. Look behind the scenes as to the way it is encoded to a
.class
file.Semantics are a funny thing though; under exam conditions "abstract class" would have to literally be compiled from a
.java
source file usingabstract class
in the Class' declaration.接口包含方法的原型(即声明)而不是定义,但抽象类可以包含方法和方法的定义。 至少一个抽象方法(只有原型的方法)
An interface contains prototype of methods (i.e Declaration ) not defination but Abstract class can contain defination of method & atleast one Abstract method (method with only prototype)
接口用于打破对象继承。
它们可以容纳多个类别的两个或多个对象
和类层次结构。
将接口视为插座插头。 所有课程
实现一个接口需要有一个,同样的
电脑、咖啡机、呼吸机和
冰箱需要有相同的设备才能获得
力量。
Interfaces are used to break the Object Inheritance.
They could hold two or more objects of several classes
and classes hierarchies.
Look at an interface as an outlet plug. All classes
implementing an Interface need to have one, the same
way a computer, a coffee machine, a ventilator and a
refrigerator need to have the same device to get
power.
抽象类看起来像接口。 抽象类可以有实现,而接口不能有任何实现。
那么,有一个问题。 如果抽象类只有方法签名,我们可以将其称为接口吗?
我认为,抽象类与接口不同,它就是类。 它们的使用成本很高,因为当您从抽象类继承一个类时需要进行查找。
Abstract class looks like interface. Abstract classes can have implementations where as interface can't have any implementations.
Then, there is a question. Can we call abstract class as interface if they only have method signatures?
I think, abstract classes, unlike interfaces, are classes. They are expensive to use because there is a lookup to do when you inherit a class from abstract class.