0

When dealing with abstract classes in Java it is not possible to create an object of that class.

So this would not work AbstractClassName object = new AbstractClassName();.

Why this is working AbstractClassName object = new NonAbstractClassName(); ?

Can you tell me what am I doing by using a constructor from a non abstract class (that extends the abstract one) and assign the object to a abstract class type?

azurefrog
  • 10,785
  • 7
  • 42
  • 56
traian.cj
  • 27
  • 4
  • 1
    Does this answer your question? - https://stackoverflow.com/questions/2700256/why-cant-an-object-of-abstract-class-be-created – rhowell Feb 27 '20 at 16:03
  • You can achieve loose coupling by this. You can assign any class object inheriting from `AbstractClassName`. – Voodoo Feb 27 '20 at 16:04
  • Related: [What does it mean to “program to an interface”?](https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface/384067). Same concept when programming to an abstract class. – azurefrog Feb 27 '20 at 16:05
  • That is polymorphism. – Eduard Feb 27 '20 at 16:08
  • 1
    Does this answer your question? [What does it mean to "program to an interface"?](https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) – Voodoo Feb 27 '20 at 16:09
  • Thanks for you help guys. I found the magic word : "loose coupling". – traian.cj Feb 27 '20 at 17:17

0 Answers0