Java is learning recently. Stuck on the fact that I can not understand the role of an abstract class in java. In the textbook (not only in 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 ab; } } 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 remaking 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 ab; } } 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 the deletion of an abstract class (or "remaking" into a regular one) is impossible and will lead to an error, or just explain in what situations an ordinary class will not replace the abstract one. Thank you in advance.