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?


Asked by: Chester157 | Posted: 23-01-2022






Answer 1

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.

Answered by: Ada219 | Posted: 24-02-2022



Answer 2

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.

Answered by: Richard555 | Posted: 24-02-2022



Answer 3

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.

Answered by: Roman686 | Posted: 24-02-2022



Answer 4

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

Answered by: Eric163 | Posted: 24-02-2022



Answer 5

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.

Answered by: Julia454 | Posted: 24-02-2022



Answer 6

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)

Answered by: Chelsea825 | Posted: 24-02-2022



Answer 7

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.

Answered by: Adrian194 | Posted: 24-02-2022



Answer 8

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.

Answered by: Owen492 | Posted: 24-02-2022



Similar questions

java - Need for Abstract Class as well as Interface?

An interface is a 100% abstract class, so we can use an interface for efficient programming. Is there any situation where an abstract class is better than an interface?


java - When to use abstract class or interface?


java - 100% Abstract class vs Interface

Is there a reason to use a 100% abstract class and not an interface ? Can you give me a good example when to use both so I can grasp the concept a little? Update: 100% Abstract class -> abstract class with only abstract methods. I'm curios if there are differences between php and java regarding this aspect. Update2: Even if I understand most of the reasons I'm more interested in the conceptual


java - When to use an abstract class? An interface?

This question already has answers here:


When shall we go for interface or abstract class in Java?

This question already has answers here:


class - Abstract and Interface in java

This question already has answers here:


java - why do we need interface when we have abstract class?

This question already has answers here:


java - Why in this case use Interface over Abstract class?

I was reading a blog post here: http://codeofdoom.com/wordpress/2009/02/12/learn-this-when-to-use-an-abstract-class-and-an-interface/ public interface Actor{ Performance say(Line l); } public interface Director{ Movie direct(boolean goodmovie); } public interface ActorDire...


java - When to only abstract class but not interface

This question already has answers here:


java - Why use abstract class and not interface?

For example a real estate builder is constructing an apartment with many flats. All the rooms in the flats have the same design, except the bedroom. The bedroom design is left for the people who would own the flats i.e; the bed Rooms can be of different designs for different flats. I can achieve this through an abstract class like below: public abstract class Flat { //some propertie...


java - When to use an Abstract Class and an Interface

This question already has answers here:


Abstract class or interface Java

This question already has answers here:


java - What should I use Interface or abstract class?

This question already has answers here:


java - Abstract class ,class, interface

Closed. This question needs to be more focused. It ...


java - When to use Interface and when to use Abstract class

This question already has answers here:


Java interface and abstract class

Closed. This question does not meet Stack Overflow guid...


java - Song Class, Using abstract class and interface

What I'am trying to do with this program is output the information of a song using the toString on Song class. When I output it, everything is fine except the SongType/genre. It is still outputting UNDETERMINED. abstract class Song implements ISong //SONG CLASS { private String name; private String rating; private int id; private SongType genre; public Song() { name = " "; rating = " "; i...


oop - Java - abstract class and interface

I'm working on my java project which is a basic roleplaying-game, and I have an OOD question (I guess it's pretty simple, but it's my first java project). I started working on weapons, I created an abstract Weapon class with an abstract method named attack. Weapon has two subclasses - MeleeWeapon and RangedWeapon, and both have attack method, and it's fine. Then I thought about another thing - what would happen i...


Abstract class or an interface : JAVA?

Closed. This question is opinion-based. It is not c...


java - Using Interface and Abstract class

I am trying to learn about Interface and Abstract class and its uses. Lets say, I have an Interface IPlan. Have three classes implementing it, schoolPlan, highSchoolPlan, collegePlan. If I want to write a createPlan() service method that will accept any of this three plan type objects, how do I do it? In my service class, if I do something like, public String c...


java - Need for Abstract Class as well as Interface?

An interface is a 100% abstract class, so we can use an interface for efficient programming. Is there any situation where an abstract class is better than an interface?


eclipse - Why would one declare a Java interface method as abstract?

I used the "pull interface" refactoring feature of Eclipse today to create an interface based on an existing class. The dialog box offered to create all the new methods of the new interface as "abstract" methods. What would be the benefit of that? I thought that the fact that you were allowed to declare interface methods as abstract was a superfluous and harmless feature of the language that is not particul...


java - using abstract keyword in interface

I know the difference between "public interface" and "public abstract interface", but when applied on methods are there differences? public interface IPluggableUi { abstract public JComponent getPanel(); abstract public void initUi(); } or public interface IPluggableUi { public JComponent getPanel(); public void initUi(); }


java - When to use abstract class or interface?


java - Why declare an interface as abstract?

What's point of declaring an interface as abstract? Same thing for an interface method. Is there a point to it? eg. public abstract interface Presenter { public abstract void go(final HasWidgets container); }


java - 100% Abstract class vs Interface

Is there a reason to use a 100% abstract class and not an interface ? Can you give me a good example when to use both so I can grasp the concept a little? Update: 100% Abstract class -> abstract class with only abstract methods. I'm curios if there are differences between php and java regarding this aspect. Update2: Even if I understand most of the reasons I'm more interested in the conceptual


How to port Java interface codes into C++ abstract base class?

Im new in OOP programming and C++ and Im currently into learning design patterns. Just grab the Head First Design Patterns book to learn from. Its actually great and Im already getting hold into the basic concepts.The first chapter talks about programming to an interface rather an implementation. Unfortunately for me though, the examples uses java. Below is the java code sample from the books using "interf...


java - Different rules for abstract methods in abstract class and interface

We cannot declare abstract methods in interface as protected and default (even if we don't mention any access specifier (default) compiler takes it as public) but we can declare abstract method in abstract class as protected and default. Why there are different rules for abstract class and interface?


java - When to use an abstract class? An interface?

This question already has answers here:


java - Interface vs Abstract Classes

I am a little bit familiar with the difference between Abstract and Interface classes but What do you think is the meaning of the sentence below? An interface can only define constants while abstract class can have fields.


What is the best way to interface C# and Java?

Closed. This question needs to be more focused. It ...


java - Need for Abstract Class as well as Interface?

An interface is a 100% abstract class, so we can use an interface for efficient programming. Is there any situation where an abstract class is better than an interface?


java - Why doesn't this generic interface stack work?

I have a bunch of generic interfaces and classes public interface IElement { // omited } class Element implements IElement { // omited } public interface IElementList<E extends IElement> extends Iterable { public Iterator<E> iterator(); } class ElementList implements IElementList<Element> { public Iterator<Element> iterator() { // omited } } public interfa...


user interface - Java GUI repaint() problem?

I have a JFrame. This JFrame contains a JButton. I click the JButton and 10 JTextFields are created. the problem: I cannot see them until "I force a repaint()" by resizing the window. Only then do I see the JTextFields created. CODE: JPanel points = new JPanel(); //Creating the JTextFields: for (int i=0; i<10; i++) { JTextField textField = new JTextField(); points.add(textField);...


java - How to discover the type of the NAT a given interface is behind

I would like to discover the type of the NAT (FullCone, Restricted Cone, Port Restricted cone, Symmetric) a given network interface is behind. I've tested different tools (http://freshmeat.net/projects/jstun/, http://code.google.com/p/boogu/) but they report differen...


user interface - Java GUI - General

Closed. This question does not meet Stack Overflow guid...


java - How to have JMX bind to a specific interface?

I am currently starting my Java VM with the com.sun.management.jmxremote.* properties so that I can connect to it via JConsole for management and monitoring. Unfortunately, it listens on all interfaces (IP addresses) on the machine. In our environment, there are often cases where there is more than one Java VM running on a machine at the same time. While it's possible to tell JMX to ...


Java: Adding a User Interface to a program

If I have Java program and I need to alter it to an interface and include icons, is there any easy I can do this and is there a good application that can help me to do it ? or do I have to code it in myself?


swing - How to design interface for a game in Java

We're a team of a designer and a programmer. Me, the programmer, is hopeless on design of all sorts, so the user interface of the game must be created by the designer. We were thinking of a main screen, where the character walks around, a small minimap in the top, an inventory section, etc. All these things would have to be designed in a way that looks pretty, i.e nice graphics/buttons/colors etc. I'm assuming that...


java - Is there more to an interface than having the correct methods

So lets say I have this interface: public interface IBox { public void setSize(int size); public int getSize(); public int getArea(); //...and so on } And I have a class that implements it: public class Rectangle implements IBox { private int size; //Methods here } If I wanted to use the interface IBox, i can't actually create an instance ...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top