Why bother with abstract or interface classes?

This clogged me with why it is better to have an abstract class. So let's say that I have to calculate areas of different shapes (circle, rectangle). I was taught it better to have an abstract / interface form, and then classes like Rectangle, Circle that extend it.

I made the following code

abstract class Shape { abstract int getArea(); } class Rectangle extends Shape{ private int width; private int height; public Rectangle (){ this.width = width; this.height = height; } // get set methods ommited public int getArea () { return width * height; } } 

The form class seems to have no purpose. I can not do impementation getArea in the form class, since different forms calculate the area differently. I could just remove the form class and make my code simpler.

So what will the actual goal be in the form of an abstract / class interface? Thanks in advance for any explanation.

+8
java inheritance polymorphism
source share
4 answers

The form class seems to have no purpose. I can not do impementation getArea in the form class, since different forms calculate the area differently. I could just remove the form class and make my code simpler.

Suppose you have an image made up of several shapes - some circles, some rectangles, etc. You can save all these shapes in a List<Shape> , and then calculate the total area using:

 int totalArea = 0; for (Shape shape : shapes) { totalArea += shape.getArea(); } 

How would you do this if you did not have a generic class or Shape interface? Your Picture class should be aware of each individual form class, and not use the commonality between the various form classes to make the code more general.

As another example, consider threads. Imagine that we did not have an InputStream class - we only had separate subclasses. Then, each time you wrote code that was supposed to read some data, you would need to provide an overload for each individual subclass that you would like to process, although the code would be exactly the same in each method. InputStream abstracts the differences, revealing common functionality (reading, skipping, etc.). That way you can write one method that just takes an InputStream and then calls it using FileInputStream or ByteArrayInputStream , etc.) Without a method that should take care of which one it receives.

+16
source share

If you want to pass an arbitrary Shape to the method, you can do:

 public void method(Shape shape) { int area = shape.getArea(); } 

This is called polymorphism. If there is no abstract class or interface, you cannot do this.

+7
source share

You can use an interface or an abstract class if you want to group classes based on the specific types of behavior or properties that they share. This will allow you to use the interface / abstract class as parameter types or in generics. In your case, for example, you can do the following:

  • Creating a list of different forms of List <Shape> .
  • Pass the form to the method. Say you have a getArea() method for your Shape class / interface. You can then use it in the method whichIsGreater (Shape shape1, Shape shape2) , which would use the getArea() method in its implementation.

There is another very important aspect to using interfaces or abstract classes. Having an abstract class (or interface) specific to your classes shows the intent of your code. Someone who takes your code will have a much easier task to understand with a well-defined inheritance.

The goal of good code design is not to write the shortest possible code. This is about efficient, but also clear and self-documenting code.

To complete my answer, I repeated some points raised in other answers - there was no disrespect.

+5
source share

In your particular case, the Shape should be an interface and not an abstract class . interface is basically an abstract class with only public methods and no implementations. abstract class makes sense when you can implement a specific method that is right for everyone, or at least for most implementing subclasses. This is the Do Not Repeat Yourself (DRY) application in this case.

Why bother with abstract classes or interfaces?

Because now you can only have a List<Shape> , where you can insert all kinds of different shapes, and you do not need to worry about which shape. The JVM will do your work and choose which implementation of getArea it should choose.

+2
source share

All Articles