0

So I came across this line while studying abstraction in Java: ABSTRACT CLASSES can also contain implemented methods ( aka regular methods) whereas an INTERFACE contains ONLY abstract methods ( methods that are not implemented yet).

But if "abstract" literally means "no body", then how is it that the implementation of regular methods can be given in the abstract classes?

JDANDE
  • 1
  • 1
  • an abstract can have implemented methods. It just can't be instantiated, unless through a subclass. – Stultuske May 26 '23 at 06:12
  • actually, that answer is a bit out-of-date. interfaces can now also contain implemented (default) methods – Stultuske May 26 '23 at 06:13
  • 1
    "*an abstract class is one which has no body (implementation)*" is not correct, probably it was meant to be "*an abstract **method** is one which has no body (implementation)*" - a class always have a body and at least a default constructor (eventually empty) || and ' *"abstract" literally means "no body"* ' is wrong || Java Language Specification [8.1.1.1. abstract Classes](https://docs.oracle.com/javase/specs/jls/se20/html/jls-8.html#jls-8.1.1.1): "An abstract class is a class that is incomplete, or to be considered incomplete." – user16320675 May 26 '23 at 06:26
  • 1
    "_whereas an INTERFACE contains ONLY abstract methods_" – Not only is this wrong because interfaces can have `default` methods since Java 8, as mentioned previously, but interfaces can also have `private` concrete methods since Java 9 (note a private method can never be abstract). And that's ignoring the fact that interfaces can have `static` methods, though I suppose that's tangential to the concept, as static methods can never be abstract, and they aren't polymorphic. – Slaw May 26 '23 at 06:44

1 Answers1

0

Because in Java isn't used the literal meaning that you assign.

Please see online references:

Data abstraction is the process of hiding certain details and showing only essential information to the user. Abstraction can be achieved with either abstract classes or interfaces (which you will learn more about in the next chapter).

The abstract keyword is a non-access modifier, used for classes and methods:

Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).

Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from). An abstract class can have both abstract and regular methods

Lore
  • 1,286
  • 1
  • 22
  • 57