Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a Java interface an abstract class? [duplicate]

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?

like image 915
Martin Doms Avatar asked Nov 03 '08 10:11

Martin Doms


People also ask

Is abstract class and interface are same?

Abstract Class Vs. Interface: Explore the Difference between Abstract Class and Interface in Java. The Abstract class and Interface both are used to have abstraction. An abstract class contains an abstract keyword on the declaration whereas an Interface is a sketch that is used to implement a class.

Is interface an abstract class in Java?

Interface contains only abstract methods that can't be instantiated and it is declared by keyword interface. A class that is declared with the abstract keyword is known as an abstract class in Java.

Can we replace abstract class with interface?

If an abstract class contains only abstract method declarations, it should be declared as an interface instead. Multiple interfaces can be implemented by classes anywhere in the class hierarchy, whether or not they are related to one another in any way.

CAN interface have 2 abstract methods?

1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods. Since Java 8, it can have default and static methods also. 2) Abstract class doesn't support multiple inheritance.


2 Answers

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.

like image 113
Tamas Czinege Avatar answered Sep 20 '22 02:09

Tamas Czinege


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.

like image 29
VonC Avatar answered Sep 19 '22 02:09

VonC