Azam
When and Why we Use Abstract Class and What is the Benefit of Abstract Class
By Azam in OOP/OOD on Feb 12 2010
  • Mallika
    Aug, 2010 20

    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. 

    • 1
  • kanchan setia
    Feb, 2011 3

    Abstract classes are classes that contain one or more abstract method. an abstract method is a method that is declared but contains no information.

    • 0
  • Uday Gaikwad
    Aug, 2010 25

    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

    • 0
  • Ramchandra Desai
    Jun, 2010 15

    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.

    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);
    
    If a class includes abstract methods, the class itself must be declared abstract, as in:
    public abstract class GraphicObject {
    // declare fields
    // declare non-abstract methods
    abstract void draw();
    }
    
    When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.


    Then we can use this draw() method however we want i.e. to draw ellipse or circle or any other shape 

    • 0
  • Niranjan pandey
    Apr, 2010 18

    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.

    • 0
  • siva sankar
    Apr, 2010 7

    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

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS