Is a Java interface an abstract class? [duplicate]

2019-03-13 05:35发布

This question already has an answer here:

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 is one abstract class and three interfaces. Do these interfaces qualify as abstract classes, in general?

标签: java oop
8条回答
再贱就再见
2楼-- · 2019-03-13 06:12

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.

查看更多
▲ chillily
3楼-- · 2019-03-13 06:17

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/:

Object a = new Date();
String s = a.toString();
  • The type of the variable 'a' is Object (which is actually a type notation in Java source code meaning a reference to an Object),
  • but the class of the object it points to is Date.

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.

查看更多
我命由我不由天
4楼-- · 2019-03-13 06:20

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 using abstract class in the Class' declaration.

查看更多
\"骚年 ilove
5楼-- · 2019-03-13 06:30

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.

查看更多
相关推荐>>
6楼-- · 2019-03-13 06:30

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.

查看更多
beautiful°
7楼-- · 2019-03-13 06:31

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 :)

查看更多
登录 后发表回答