-1

When calling a method from another class is it considered abstraction? Because when the main method creates the Dog object and uses the method animalSound, the user cannot see what is being done. Is this considered abstraction?

Dog.java

public class Dog {
    public void animalSound() {
         System.out.println("Woof");
    }
}

Main.java

public class Main {
    public static void main() {
        Dog d = new Dog();
        d.animalSound();
    }
}
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Welcome to stack overflow. Please take care of proper formatting of the code parts. Also - what abstraction is - is easy to find on google. Question should only be asked after at least quick investigation. Concerning the content: that is just a CALL. Abstraction comes into play when you are using inheritance. – Martin Meeser Jan 18 '20 at 06:27

1 Answers1

2

When calling a method from another class is it considered abstraction?

Abstraction (in computer science) means hiding details.

So you are essentially asking whether a method call is abstracting (hiding) the details of the method.

The strict answer is No. The details of what the method does are being hidden by the method declaration not by the method call.

However, in the more general sense, it is true that methods are a form of abstraction.


By the way, a commentator said this:

Abstraction comes into play when you are using inheritance.

This is not correct.

  1. Data abstraction does not require inheritance. In fact, the classical definition of a (staticly) object oriented language is that it supports "inheritance + data abstraction (or encapsulation) + polymorphism". Inheritance and data abstraction are orthogonal properties.

  2. There are examples of programming languages that support data abstraction without inheritance. For instance, CLU and (depending on how strict you are1) Ada83.

  3. Data abstraction is not the only kind of abstraction. Other kinds include:

    • Procedural abstraction; i.e. procedures, functions, subroutines, etcetera. In procedural abstraction, we are hiding just algorithmic details.

    • Modularisation which hides larger scale details of some part of an application.

See the Wikipedia page on Abstraction for more details.


1 - In Ada83, a subtype cannot add new fields or override existing methods ("operations"). See http://goanna.cs.rmit.edu.au/dale/ada/aln/14_OO.html

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216