Home java Why abstract class in java

Why abstract class in java

Author

Date

Category

java study 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) found an example describing the role of abstract class:

// Basic arithmetic operation
ABSTRACT CLASS OPERATION {
Public Abstract int Calculate (int a, int b);
}
// Addition
Class Addition {
Public Int Calculate (Int A, Int B) {
Return A + B;
}
}
// Subdument
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 converting an ordinary one from an abstract class, and even removing it at all the program did not stop working:

// Basic arithmetic operation
Class Operation
{
  Public Int Calculate (Int A, Int B)
  {
    RETURN A * B;
  }
}
// Addition
Class Addition Extends Operation
{
  Public Int Calculate (Int A, Int B)
  {
    Return A + B;
  }
}
// Subdument
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 ());
  }
}

Describe, please, an example in which the removal of an abstract class (or “reworking” in the usual) is not possible and will cause an error, or simply check out in what situations the usual class will not replace abstract. Thanks in advance.


Answer 1, Authority 100%

What is the textbook, if not a secret? In decent books on Java, the foundations of the OOP are still given.

essence of this. When you mark the class as Abstract , you are the same forbidding creating its copies. Since in the class Operation not defined CALCULATE , you should not be able to create its instances:

operation o = new operation ();

If there are no abstract methods in the class, then “reworking” it will not show itself in the usual one. It will simply be possible to create an instance of this class.

Specifically in your “rework” you have determined the body for the Calculate method, thereby changing the logic of the work of your hierarchy. If you now create another subsidiary from Operation Class and not define CALCULATE in it, it will use the implementation of the parent class, that is, to calculate the product of two arguments.

And even removing it at all, the program has never ceased to work:

I do not believe. If you deleted the class Operation you would have to edit the child classes (remove Extends and edit main ). In addition, after this, your classes addition and Subtraction would be independent of each other.

In general, abstract classes are widely used when you need to implement some overall functionality for the class family.


Answer 2, Authority 188%

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

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

I will try to give an example from real life:

  1. Suppose we want to describe the methods of assembly and disassembly of different furniture, for which we ask the class Furniture
  2. In this case, Furniture Typical abstract class with two abstract methods: Collect and Disassemble . Since the methods of assembling and disassembling furniture depend on the specific model, it is not possible in the class furniture describe them, so we declare them as abstract.
  3. Next, we are asked the table , which inherits from the class Furniture , in which we sell methods>to collect / disassemble – well, something like: fasten Legs, then and so on.
  4. Next, we are set in the class Sofa , which inherits the Furniture Furniture , in which again we sell Methods to collect / disassemble – Now: fasten the back, Then armrests and so on.

Next begins the most interesting and important for what the whole circus actually with horses actually and stood:

  • If there were no abstract classes, then the assembly master would bring exactly Tables and Sofas . Well, type – the master sees that it is Table takes instructions on its assembly and collects. He must know exactly what it is the table or sofa otherwise the pipe – the assembly will not take place.
  • In the presence of an abstract class, the master brings not Table or Sofa , and just some Furniture . Master in general, then on the drum, the table is or a sofa or a wardrobe. The master pulls out the furniture attached sheet with the instruction and collects it.

Feels the difference?


Answer 3, Authority 88%

For example, write an airport accounting system at the airport. There are freight and passenger aircraft and necessary, for example, to be a list of aircraft that are now at the airport.
We make an abstract class “aircraft” (we do not know in advance which aircraft will arrive)
and determine some basic fields suitable for any aircraft (flight speed and length, for example)

Public Abstract class FLIGHT {
   INT SPEED, WIDTH;
}

Next Create two classes, one for a passenger aircraft and one for freight, and in each you add already special parameters for it

Public Class Passflight EXTENDS FLIGHT {
   INT MAXPASSENGERS; // Max number of passengers
}
Public Class Freighter EXTENDS FLIGHT {
   INT MAXCARGO; // Max Magnifier
}

Next already in the class, which will work with all this business, create a sheet and piham those airplanes that fly

Public Class Runner {
  Public Static Void Main (String [] Args) {
    List & lt; Flight & GT; FlightList = new ArrayList & lt; Flight & gt; ();
    flightlist.add (new passflight ());
    FlightList.add (New Freighter ());
  }
}

Now, in principle, it is clear that we have a list with airplanes, and what they are – no longer important. For me, the main chip of abstract classes is that with the help of them you can set the logic of work without implementing it.


Answer 4

Actually, the main difference between the abstract class from the usual consists only in the fact that the abstract class cannot create an object and therefore it is intended only for overriding. And your first example is incorrect because there is no connection between the class of Addition, Substraction and Operation.
An example can be viewed, for example, here .


Answer 5

This is a cross between the interface and the usual class. The class has the impossibility of creating instances, and from the interface the possibility of a full-fledged announcement of methods. Conveniently, when for some set of classes there is a general method, but the rest must be overridden.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions