When is abstract class used?
abstract keyword is used to create a abstract class and method.
Abstract class in java can’t be instantiated. An abstract class is mostly used to provide a base for subclasses to extend and implement the abstract methods and override or use the implemented methods in abstract class.
Not only can we define a template for children class, but Abstract Classes offer the added benefit of letting you define functionality that your child classes can utilize later.
when are abstract methods useful?
Abstract methods are useful in the same way that defining methods in an Interface is useful. It’s a way for the designer of the Abstract class to say “any child of mine MUST implement this method”.
You can’t provide an implementation for an Interface.
Also When should you prefer them over interfaces and when not?
Abstract Classes are a good fit if you want to provide implementation details to your children but don’t want to allow an instance of your class to be directly instantiated (which allows you to partially define a class).
If you want to simply define a contract for Objects to follow, then use an Interface.
Recent Comments