When and Why we Use Abstract Class and What is the Benefit of Abstract Class
Azam
Select an image from your device to upload
Abstract class can have abstract methods or it can have implemented methods it gives generalize definition for the class.
So we can inherit abstract class and provide the implementation to that methods as per the requirments
abstract
An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
abstract void moveTo(double deltaX, double deltaY);
public abstract class GraphicObject { // declare fields // declare non-abstract methods abstract void draw(); }
Suppose if you have a requirement to implement the some common functionality in different modules of the project. But it tedious to code in all the modules, insted you just create an Abstract Class and implement those common functionality and inherit this class whereever applicable and call those methods. You no need to create an object, directly you can call through the class name. Its ease to maintinance.
An abstrct class can contains both methods with and with out implementaion. The Methods which are not having implemetation those are may/may not implemented in derived class.
Adding new methods in abstract class in middle of development life cycle won't break existing sub classes unless these methods are declared as mustoverride.
Abstract class is used when and what methods are used exactly while in interface,the methods are defined for future use.
Benfits:
Core functinality will defined in a single class.
It contains concrete methods.
No need to implement all the methods.
No need to create an object, directly can access through class name.
Can't inherit more than one Abstarct class