Why Abstract class being used ?

If abstract class don't have any real implementation then why it is used. What is the benefit of using it if we can do the same program without abstract class.
Please example the benefit of using abstract class..?

Replies

  • ISHAN TOPRE
    ISHAN TOPRE
    I believe Abstract is something similar to polymorphism. So wherever you will apply Polymorphism, you can apply abstract.

    Correct me if I am wrong.
  • TheV
    TheV
    No friend you are wrong...!
    Polymorphism is overloading and overriding.

    Abstract class is something that have abstraction method. That abstract method will be implemented by the sub-classes.

    And What I wanted to know is that what is the benefit...

    I come to know that with an example...

    When we want to buy a car then it have some generic function, but apart from this, each owner want to have their own color,
    no. of seats, horsepower etc. which is implemented by the respective classes.

    This help in structuring the given abstract of the implementation without providing a complete implementation of every method.
  • Manish Goyal
    Manish Goyal
    This example may clear you

    Say i want to calculate area , now area can be of any type it may be of circle , rectangle , now think yourself it is better to define a single function and reuse it or define it everytime
  • rahul69
    rahul69
    In my opinion it just helps us to implement real life situations better as although
    (as for the area example) we have to define the function separately in circle, rectangle etc.
    But it gives a clear view that all these functions defined differently still calculates only the
    area for different shapes. So same "area calculation" is being done by different classes.
  • simplycoder
    simplycoder
    Well I would like to add few things more.
    When we work on a big project and its a team project, we are bound by some limitations. We should know what all functions are there in the program as a complete package and so we have to orgaize things properly.
    In Java, Interface is a 100% abstract class.
    In short, abstract classes are used to organize things to minimize the chaos.
  • TheV
    TheV
    Thankyou Simplycoder..That's the answer I wanted...why..? Now it's clear..
  • vinci
    vinci
    i think in this case we will use the concept of polymorphism and not abstract class.because each methods we implement will do some generic task which must be declared in super class .and hence no use of abstract class here .
  • vinci
    vinci
    also we use abstract class when we know absolutely that the sub classes will implement abstract methods in different ways
  • manishks
    manishks
    ,Yes, i agree. Classes helps to do the task in more organised manner. All the functions and data members will be declared and you,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
  • siddheshRane
    siddheshRane
    I agree with vinci totally. By declaring method as an abstract/pure virtual it mandates child to provide body for the same. otherwise it will be abstract again.

    I will elaborate vinici's point with an example.

    Before that few points to remember
    1. Inheritance enables resuability.
    2.Abstract class can have non abstract methods.

    we have,
    class Animal
    {
    public:
    virtual void speak() = 0;
    virtual void sleep() {
    cout << " I am sleeping";
    }
    };

    consider above class animal. there are 2 behaviors
    1. sleep(). all the derived versions of animals will have default sleep() if it is not overridden.
    i.e. this property is mostly generic.of course one could provide specific behavior in respective child class.
    2. But property like speak is not generic. i.e. we are assuming that every subtype of animal[i.e. derived class of animal like cow,dog,cat etc.] will speak in different way.
    so we declare it as abstract/ pure virtual forcing child class like cow,dog etc. to provide body for the same.
    If these children do not provide the body then those class will also be abstract class

    However, what if I allowed to create Animal object and call speak method
    int main ()
    {
    Animal aObj;//Not allowed. I know but lets assume it is possible
    aObj.speak();// confusion as base class has no valid speak
    }

    One could think of writing default implementation for method speak in Animal class like
    class Animal
    {
    public:
    virtual void speak(){
    cout << "hmm"; //default behavior
    }
    };

    Imagine there are 40 types of animals and we forget to provide specific implementation of speak() to class Human.
    So when Human is called it will speak as "hmm" instead of "HELLO"
    HENCE,
    by making speak as abstract/pure virtual compiler will mandate child class Human to override and provide own definition of speak-->thus avoid undesired effects.
    However, generic behaviors like sleep is still inherited in children class.

    Now, WHY abstract class and not Interface??
    As I mentioned in point 1. Inheritance enables reusability.
    So if Animal class having 20 generic/default behaviors and single specific behavior like speak() go for inheritance rather than Interfaces.

    As in interfaces implementation one should provide all the all the methods. so there will be more code, more bug fixing, more price.
    So instead of why not go for Pure virtual functions/ abstract method.

    I hope it helps.
  • Jalajpachouly
    Jalajpachouly
    Guys,
    Abstract means some details are not complete, and the reason why we make class abstract is , it doesn't know how to fill the incompleteness, like if you want to write a class to create a pizaa, and you want to sell the same to various piza stores, which have there own piza making method, then createPizaa method should be abstract, and every piza store may have there own implementation.
    One thing to remember that they all have some thing to share which is available in the Abstract class, in this case may be Pizza ingradients. If nothing to share , then interface is the right choice. Hope this helps..

You are reading an archived discussion.

Related Posts

Samsung has recently launched a new way of interacting with your phone and it's called "Breeze GUI". Check out the following video demo which shows the new Samsung GUI in...
Hai Guys, I'm doing a project onAutomatic Collision Detection and Acceleration Control using Multi Zone Transmission and Reception through RF technology and GSM communications. It's a project based on the...
uBuntu 11.10 Beta 1 has been released and available for download from uBuntu servers. There are tons of improvements and new features and you can download your copy of uBuntu...
hai friends....this is an electronics and communication final year student....digital signal processing is a wonderful subject that plays a vital role in communications.... l .....DSP finds its applications in various...
hai i am thinking to do mini project so for that i selected a simple one "skin response meter" .....this is a simple circuit that can sense whether the body...