When and Why we Use Abstract Class and What is the Benefit of Abstract Class
Azam
An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class.example Bank Account:Bank account can be an abstract class and all the other specialized bank accounts inherit from bank account. People can have their own account like saving,checking or credit account. but not a generic bank account.
Abstract classes are classes that contain one or more abstract method. an abstract method is a method that is declared but contains no information.
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
An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
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(); }
when we want to make a class that could't not be initiated and for using the propertis of that class we must neet to inherit that class then we make that class as abstract class.
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