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

Venki

Hi I have faced in my SCJP examination this question. If both interface and abstract class having same methods then which one is better to override all methods into my class in java? Please explain me in different scenarios for which one is better to take.

This is interface :

interface ArithmeticMethods {
       public abstract void add();
       public abstract void sub();
       public abstract void div();
       public abstract void mul();
   }

This is abstract class :

abstract ArithMethods {
       public abstract void add();
       public abstract void sub();
       public abstract void div();
       public abstract void mul();
   }

This is my class name: ArithMethodImplemenation class.

Now at what scenario I should do like this

public ArithMethodImplemenation implements ArithmeticMethods{
   //override all methods of ArithmeticMethods
}

or at what scenario I should do like this

public ArithMethodImplemenation extends ArithMethods{
   //override all methods of ArithMethods
}

Please explain me with different scenarios. And my friends also faced this question in so many interviews. But they couldn't succeed.

anubhava

There is no point in having an empty abstract class with only abstract methods.

Just use an interface instead.

Remember in Java you can extend only one class but can implement multiple interfaces.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

Interface, Abstract Class and Methods of Abstract Class

From Dev

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

From Dev

Why do we need Interface when same thing as Interface can be achieved by making all methods abstract in an abstract class?

From Java

Abstract class with all concrete methods

From Dev

How to call methods within my abstract class that implements an imported interface?

From Java

Get all methods of an Interface or Abstract class using Reflection

From Dev

Do we have to write all methods in abstract class into an interface?

From Java

Do i need to implement all the methods of interface again while inheriting abstract class in Java?

From Dev

Is it a good practice to create methods inside an abstract class and not override them inside the class which extends it?

From Dev

Interface, all methods must be abstract, but one?

From Dev

Y it's not mendatory to implement all interface methods in Child abstract class But Mendatory to implement all Interface methods in Grand Child class?

From Dev

Class that implements an interface in a jar, but does not override the methods

From Java

What's the point in having an abstract class with no abstract methods?

From Dev

Must a class implement all abstract methods?

From Java

Abstract methods in a Java interface

From Dev

Extending REST class and override methods on the same path

From Java

Java Class cannot override method in abstract class which has a method signature that contains an interface

From Dev

Overload abstract methods in abstract class

From Dev

Abstract class with only abstract methods

From Java

Polymorphism and inheritance in Java with static methods of abstract class

From Dev

How to implement methods of an abstract class? (Java)

From Dev

How to override class methods

From Dev

Is having both static and non-static methods in one class considered bad practise?

From Dev

Calling methods for my java class

From Java

Require override of specific methods of a non-abstract class

From Dev

Why there is an ability to override non abstract methods of base class

From Dev

Django: Override model methods from abstract base class models

From Dev

Class methods - which one to use and when?

Related Related

  1. 1

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

  2. 2

    Interface, Abstract Class and Methods of Abstract Class

  3. 3

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

  4. 4

    Why do we need Interface when same thing as Interface can be achieved by making all methods abstract in an abstract class?

  5. 5

    Abstract class with all concrete methods

  6. 6

    How to call methods within my abstract class that implements an imported interface?

  7. 7

    Get all methods of an Interface or Abstract class using Reflection

  8. 8

    Do we have to write all methods in abstract class into an interface?

  9. 9

    Do i need to implement all the methods of interface again while inheriting abstract class in Java?

  10. 10

    Is it a good practice to create methods inside an abstract class and not override them inside the class which extends it?

  11. 11

    Interface, all methods must be abstract, but one?

  12. 12

    Y it's not mendatory to implement all interface methods in Child abstract class But Mendatory to implement all Interface methods in Grand Child class?

  13. 13

    Class that implements an interface in a jar, but does not override the methods

  14. 14

    What's the point in having an abstract class with no abstract methods?

  15. 15

    Must a class implement all abstract methods?

  16. 16

    Abstract methods in a Java interface

  17. 17

    Extending REST class and override methods on the same path

  18. 18

    Java Class cannot override method in abstract class which has a method signature that contains an interface

  19. 19

    Overload abstract methods in abstract class

  20. 20

    Abstract class with only abstract methods

  21. 21

    Polymorphism and inheritance in Java with static methods of abstract class

  22. 22

    How to implement methods of an abstract class? (Java)

  23. 23

    How to override class methods

  24. 24

    Is having both static and non-static methods in one class considered bad practise?

  25. 25

    Calling methods for my java class

  26. 26

    Require override of specific methods of a non-abstract class

  27. 27

    Why there is an ability to override non abstract methods of base class

  28. 28

    Django: Override model methods from abstract base class models

  29. 29

    Class methods - which one to use and when?

HotTag

Archive