Abstract Class with only abstract methods and Interface - Which should I use?

Nidhin Joseph

Please note. This question is not an abstract class vs interface kind of question.

Yes. I know. It's not necessary for a class which extends an abstract class to override all of its unimplemented methods. If a child class is not giving definition to some of its parent's unimplemented methods, then child class will be also considered to be abstract.

But a class which implements an interface should implement all of its methods ( Also Multiple inheritance is possible with interfaces).

Is this the only difference between an abstract class with only abstract methods and an interface?

yes, I understand. An Abstract class can have states and method implementations. But I'm making the question very clear. Its not actually a interface vs abstract class kind of question.

Here, in the question, the abstract class is not having any data members or any method implementations. Just some abstract methods only. eg:

abstract class shape{
    abstract void draw();
}

I just want to know whether there are any other differences. What should I use in such a scenario?

Please help.

chrissukhram

I believe your question is not necessarily one vs the other but how to know which one to choose in an instance.

A way I like to think about it is that interfaces tend to be actions that are available to the user to interface with the class. For example, if a class implemented the interface playable you know it would have a play method, whether it be starting an audio player, starting video, or starting a game would depend on the class itself, but you just know it is a playable class and can play.

Whereas shape is not an action, it is a type, shape comes along with attributes to define it, you need to know the color, size, etc. in order to know something is a shape. This is why I would use an abstract class to define all of the properties they have in common.

Because draw() is functionality that can apply to shapes, images, or scenes I would implement that as a Drawable interface.

Example:

 public class Square extends Shape implements Drawable{

      public void draw(){
          //draw code here
      }
 }

This way you can define all the common properties inside of Shape and provide the functionality to draw itself through the Drawable interface.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

When should I use interface/abstract class and when should I not?

From Dev

Abstract class with only abstract methods

From Dev

Should I use interface or abstract base class defining List functionalities

From Dev

Interface, Abstract Class and Methods of Abstract Class

From Java

Use of an abstract class without any abstract methods

From Java

Why use an abstract class without abstract methods?

From Java

Are there any differences between a normal interface class and an abstract class that only has abstract methods?

From Dev

I tried to implement abstract class and interface in a class and only interface members got called and abstract method didnt?

From Dev

Kotlin abstract class with generic param and methods which use type param

From Dev

PHP use abstract class or interface?

From Java

When to use abstract class or interface?

From Java

Why use abstract class and not interface?

From Java

In Java, when should I use an abstract method in an interface?

From Dev

If both interface and abstract class having same methods then which one is better to override all methods into my class in java?

From Dev

What access modifier should abstract methods have in an abstract class?

From Dev

What is difference between abstract class with all abstract methods and interface(not technically)

From Dev

How can I use vector of an abstract/interface class as function parameter?

From Dev

How to implement Interface to an abstract class which is constraint?

From Java

Abstract class or interface. Which way is correct?

From Dev

Overload abstract methods in abstract class

From Java

Why should we use interface if we can simply override methods of the superclass or use abstract classes?

From Java

Abstract methods in a Java interface

From Dev

Abstract out two nearly identical methods which differ only in which property they use on an List of Objects

From Dev

Can I use lambda expression to replace instantiating an abstract class with only one abstract method?

From Dev

Abstract class ,class, interface

From Java

Why to use an empty abstract class instead of an interface?

From Dev

Real world examples when to use Abstract class and when to use Java 8 introduced interface(default and static methods)

From Dev

Should I initialize members of an abstract class?

From Dev

Java - abstract class and interface

Related Related

  1. 1

    When should I use interface/abstract class and when should I not?

  2. 2

    Abstract class with only abstract methods

  3. 3

    Should I use interface or abstract base class defining List functionalities

  4. 4

    Interface, Abstract Class and Methods of Abstract Class

  5. 5

    Use of an abstract class without any abstract methods

  6. 6

    Why use an abstract class without abstract methods?

  7. 7

    Are there any differences between a normal interface class and an abstract class that only has abstract methods?

  8. 8

    I tried to implement abstract class and interface in a class and only interface members got called and abstract method didnt?

  9. 9

    Kotlin abstract class with generic param and methods which use type param

  10. 10

    PHP use abstract class or interface?

  11. 11

    When to use abstract class or interface?

  12. 12

    Why use abstract class and not interface?

  13. 13

    In Java, when should I use an abstract method in an interface?

  14. 14

    If both interface and abstract class having same methods then which one is better to override all methods into my class in java?

  15. 15

    What access modifier should abstract methods have in an abstract class?

  16. 16

    What is difference between abstract class with all abstract methods and interface(not technically)

  17. 17

    How can I use vector of an abstract/interface class as function parameter?

  18. 18

    How to implement Interface to an abstract class which is constraint?

  19. 19

    Abstract class or interface. Which way is correct?

  20. 20

    Overload abstract methods in abstract class

  21. 21

    Why should we use interface if we can simply override methods of the superclass or use abstract classes?

  22. 22

    Abstract methods in a Java interface

  23. 23

    Abstract out two nearly identical methods which differ only in which property they use on an List of Objects

  24. 24

    Can I use lambda expression to replace instantiating an abstract class with only one abstract method?

  25. 25

    Abstract class ,class, interface

  26. 26

    Why to use an empty abstract class instead of an interface?

  27. 27

    Real world examples when to use Abstract class and when to use Java 8 introduced interface(default and static methods)

  28. 28

    Should I initialize members of an abstract class?

  29. 29

    Java - abstract class and interface

HotTag

Archive