Why abstract class in java

Question:

I'm new to Java. Stuck on not being able to understand the role of an abstract class in java. In a textbook (not just one) I found an example describing the role of an abstract class:

// Базовая арифметическая операция
abstract class Operation {
public abstract int calculate(int a, int b);
}
// Сложение
class Addition {
public int calculate(int a, int b) {
return a+b;
}
}
// Вычитание
class Subtraction {
public int calculate(int a, int b) {
return a-b;
}
}
class Test {
public static void main(String s[]) {
Operation o1 = new Addition();
Operation o2 = new Subtraction();
o1.calculate(2, 3);
o2.calculate(3, 5);
}
}

But having remade an ordinary class from an abstract class, and even deleting it altogether, the program did not stop working:

// Базовая арифметическая операция
class Operation
{
    public int calculate(int a, int b)
    {
        return a*b;
    }
}
// Сложение
class Addition extends Operation
{
    public int calculate(int a, int b)
    {
        return a+b;
    }
}
// Вычитание
class Subtraction extends Operation
{
    public int calculate(int a, int b)
    {
        return a-b;
    }
}
class Test
{
    public static void main(String s[])
    {
        Operation/*Addition*/ o1 = new Addition();
        Operation/*Subtraction*/ o2 = new Subtraction();
        Operation/*Subtraction*/ o3 = new Subtraction();
        Operation o4 = new Operation();
        System.out.println(o1.calculate(2, 3));
        System.out.println(o2.calculate(3, 5));
        System.out.println(o3.calculate(10, 20));
        System.out.println(o4.calculate(10, 10));
        System.out.println(o1.getClass()==o2.getClass());
        System.out.println(o3.getClass()==o2.getClass());
    }
}

Please describe an example in which deleting an abstract class (or "remaking" it into a regular one) is impossible and will lead to an error, or just explain in which situations a regular class will not replace an abstract one. Thanks in advance.

Answer:

Colleagues, in my opinion, of course, do not place the accents quite right. First of all, an abstract class is, of course, not a class that cannot be created, but a class whose implementation is torn off from its declaration.

Abstraction in the general sense is a separation from detail. In our case, this is the declaration of the class without caring about its implementation. Well, the implementation in each case can be different – sometimes complex, sometimes simple. An abstract class has no implementation.

I'll try to give an example from real life:

  1. Suppose we want to describe how to assemble and disassemble various furniture, for which we set the class Мебель
  2. In this case Мебель is a typical abstract class with two abstract methods: Собрать and Разобрать . Since the methods of assembling and disassembling furniture depend on a specific model, it is not possible to describe them in the Мебель class, which is why we declare them as abstract.
  3. Next, we set the class Стол , which inherits from the class Мебель , in which we implement the Собрать/Разобрать methods – well, something like: fasten the legs, this and so on.
  4. Next, we ask the Диван class, which inherits from the Мебель class, in which we again implement the Собрать/Разобрать methods – now: fasten the back, then the armrests, and so on.

Then the most interesting and important thing begins, for the sake of which, in fact, this whole circus with horses was started:

  • If there were no abstract classes, then Столы and Диваны would be brought to the assembly master. Well, like – the master sees that this Стол takes instructions for assembling it and assembles it. He must know for sure that this is a table or a sofa, otherwise the pipe – the assembly will not take place.
  • In the presence of an abstract class, the master is not brought a Стол or a Диван , but simply some Мебель . The master, in general, doesn’t care about the table, it’s either a sofa or a closet in general. The master takes out an attached sheet with instructions from the furniture and assembles it.

Feel the difference?

Scroll to Top