Search code examples
javaclassoopabstract-classabstraction

What is Abstraction in Java i need to understand it properly?


I understand basics of abstraction but still I have some uncertainties.

  1. We cannot create obj of abstract class.
  2. Abstract methods are declared in abstract class but defined in child class.
  3. And we call abstract methods using obj of child class.

I don't get why we using abstract classes if we can do all the things through objects of the child class. We defined abstract methods in child class so it also means we declared it in child class too and we calling that method so it working like normal classes and child.

What is practical purpose of abstract classes?


Solution

  • from what I can see from your question you look at abstract classes as interfaces, but you could and should use them differently.

    In an abstract class there could be not abstract methods, meaning a method where all of the child classes use the super class method, this way you can avoid duplicate code.

    You can take a look here for an explenation : https://softwareengineering.stackexchange.com/questions/106601/in-simple-words-what-are-are-the-purposes-of-abstract-classes-and-or-interfaces