I got several explanations but so far I'm not able to understand that what are the abstract classes and methods in Java.
Some said it has to do something with the security of the program, other said it isn't anything like that.
Even from the Dietel & Dietel's book I don't get it's purpose.
When, where and why do we use it?
Java OOP – Understanding Abstract Classes and Abstract Methods
abstract classjavaobject-orientedobject-oriented-design
Related Topic
- C++ vs Java – Usage Rationale for Abstract Classes and Interfaces
- Abstract Classes vs Interfaces – Key Differences and Usage
- C# – Refactoring Abstract Classes into Helper Classes
- Java Design Patterns – Should Abstract Class Implement Interface?
- What are the pros and cons of using final methods (in abstract classes)
Best Answer
First of all, the examples will be in C#. But I think you'll have no problem understanding it. So...
You CANNOT instantiate objects of an abstract class. You must have a subclass that derives from the class, if you want to instantiate it. But this probably won't help you since you may have come across that before.
So let's try an example.
Suppose you want to lose weight and your nutritionist asks you to track your food intake. When you go to her again, you can't tell her that you ate a "food". Although it's not wrong, since all food has calories, it is too "abstract" for her. So you need to tell her WHICH food.
So, if she were to code that... She'd have an abstract class Food.
Why? Because that wouldn't allow you to say "I ate a food".
So you need concrete classes:
So you'll have to tell exactly which food it is.
But she still needs the food class, it is still interesting to her:
That's an example of when you need an abstract class. When you want to have a base class, but you don't want anyone creating an object of your class.
An abstract CAN have abstract methods. It doesn't need to. Is totally ok to have an abstract class without abstract methods. Speaking of which...
Abstract method is something that it's too broad. There's no similarities on how things work, so the classes that derive from your class that has an abstract method won't be able to call the super class implementation. And you're also forcing all the sub classes to implement their version of that method.
Let's try another example? An Animal Band Simulator. You'd have animals, and each one of them would make a sound. But those are very different "implementations", they have nothing in common A dog differs from a cat, a cat from an elephant, an elephant from a cricket. So you'd have an abstract method MakeSound.
But, if you have an abstract method in your class, that makes the whole class abstract!
So the rest of the code might be:
And the BAND!
So this is an example of when you use an abstract method.
I guess this is the essence. It may be a little forced/convoluted, but I think it gets the point across. Go back to the examples that didn't help you before and see if they make sense now.
I'm go out on a limb and say you're a beginner. This stuff may seem strange to you, and you may not see any benefit in using abstract classes.
It's ok. It's strange for almost all of us when we start. But in time, you'll learn to appreciate it.