5 Things no one told me about Abstract Classes
1. The difference between Abstract classes and Interface
As a beginner in java we always feel like abstract classes and interfaces are similar and yet different. But what the difference is , one finds it hard to comprehend, at least that was the case for me.
Interface is basically a contract for the classes that extend it. Whereas Abstract isn’t entirely like that. Abstract classes can have a body for their methods which aren’t marked abstract. And if those methods with body are declared as public, they can be accessible to other classes in the same package.
2. We cannot instantiate an abstract class Object
We will not be able to instantiate an abstract class object. If in the example above if we try to create an abstract class object in the class which is extending it we get an error.
3. An abstract class can have member variables and static variables
An abstract class can have public, private ,protected and also static variables in it.
4.Abstract class can implement an interface and not use its methods
Usually a class which extends an interface should always implement it methods. But that’s not the case with abstract classes , they can extend an interface but not implement its methods.
5.Main method
We can have a main method in abstract classes