2

One definition of abstraction is hiding the implementation details and the one I am using for this question.

I read the question and many answers for using abstract in Java (Abstract class in Java). However, if we can define the functionality we want in subclasses from an abstract class, this seems to also contain the opposite of abstraction.

That is, by writing an abstract class we are exposing the implementation details.

If we look at the point of view from with in the implementing class, the only hidden implementation details provided by abstract class are

final public void finalMethod()

and

public void implementedMethod() // considering we do not override it.

However:

abstract public void abstractMethod();

seems to do the opposite of abstraction, as now we are in fact forced to implement this method, i.e. expose the implementation details.

What is the opposite of abstraction?

One answer is concretization as see here in "What’s the opposite of abstraction?" on Software Engineering.

The refined question is can an abstract class provide both abstraction and concretization?

It appears to do both.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    Can you clarify what your question is? What kind of an answer are you looking for? – ControlAltDel Jan 27 '23 at 15:35
  • 3
    If method is public, then it's a part of the abstraction. You need to look at the abstraction from the perspective of the client code. Not from the perspective of the class-implementation. That class by definition is an _implementation_ :) – Stanislav Bashkyrtsev Jan 27 '23 at 15:44
  • 1
    @Control - "The refined question is can an abstract class provide both abstraction and concretization?" ... It seems that sometimes we use a word like 'abstract' but it also refers to 'concrete' in the sense that an abstract class (1) can provide a function to a subclass but it can also (2) force the subclass to define the function. Two very different things. –  Jan 27 '23 at 15:55
  • 1
    @Stan - From the perspective of client code, we just call the method and all implementation details are hidden regardless of whether or not abstract classes are used ... and not relevant to this question, specifically about abstract classes. –  Jan 27 '23 at 15:57
  • 2
    I think you have misunderstood something. Abstraction hides implementation details from outside world (clients). Subclasses are implementors, so they should be aware of some of those details. – Chaosfire Jan 27 '23 at 16:04
  • +1 to Chaosfire. Abstract Classes provide a public API to the _client_ code. When working with variables - you'd work with the AbstractClass, _not_ with the implementation. Basically, Abstract Class is an _interface_ with some implementation. And _that's_ what is mean by the abstraction. And btw, in OOP usually it's considered a bad practice to use inheritance - we're supposed to mostly use interfaces and composition. – Stanislav Bashkyrtsev Jan 27 '23 at 16:25
  • Wikipedia states that the keywords abstract and interface provide abstraction in Java. This question is focusing on the abstract keyword. https://en.wikipedia.org/wiki/Abstraction_(computer_science) –  Jan 27 '23 at 16:49
  • @Chaos, you are saying the same thing that Stan is saying ... basically nothing related to the question. With your reasoning a simple function provides abstraction if I call it from a library I have not written or read. Please see wikipedia article above. –  Jan 27 '23 at 16:53
  • 1
    @bobbywang Because I am not trying to answer your question (that's why i wrote a comment, not answer), I am trying to fix a serious misconception about abstraction. I believe Stan is trying to do the same. – Chaosfire Jan 27 '23 at 17:07
  • @bobbywang Honestly, that article is too long to read, at least atm. But if it really states that `the keywords abstract and interface provide abstraction in Java`, that's not entirely correct. Basically any class is an abstraction (though i exclude POJOs from that). Imagine i create `public class MyList` class, providing get(), add(), etc. methods. I could implement it backed by an array, or by nodes, or maybe something else. The client does not know and does not care how I implemented it. This is an abstraction as well, even though i have not used `abstract` or `interface`. – Chaosfire Jan 27 '23 at 17:16

2 Answers2

0

If I understand your question right, to clarify what hiding the implementation means in this context you should approach it from the other way around.

It is not the abstract class that hides anything, but the class that extends (and eventually implements) the abstract class is hidden from the abstract classes point of view.

The abstract classes function is not to hide the implementation, but to share code between related classes.

For the second part of the question, Yes!. An abstract class can provide concretization. Meaning you can provide implementation in an abstract class in Java. Hiding implementation is not the purpose of abstraction in Java, but rather a side effect in my opinion.

A very similar question had already been posted in this SO thread You can check out it's accepted answer as well.

Csisanyi
  • 669
  • 4
  • 16
0

Based on your comment clarifying the type of answer you are looking for, here's what I would say:

Basically, you have the right idea. Abstraction in Java (and in software dev in general) means that parts of the class must be filled in later, within a given framework. For instance, check out ServerSocketEx

The purpose of this class is extend what java.net.ServerSocket gives you with a a framework. In the framework, you must define two things: A factory that creates socket runners/handlers, and an implementation that specifies what the SocketRunner should do with incoming data. This enables the ServerSocketEx to accept connections and create a handler and a thread to run it. It is concrete in its implementation of accepting connections and in the API a SocketRunner needs to abide by, but abstract in how those connections should be processed

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80