Replies
Welcome, guest
Join CrazyEngineers to reply, ask questions, and participate in conversations.
CrazyEngineers powered by Jatra Community Platform
-
@ishan-nohePN • Sep 3, 2011
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-iGmS6y • Sep 3, 2011
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-r2Hoep • Sep 3, 2011
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-97fAOs • Sep 3, 2011
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-NsBEdD • Sep 4, 2011
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-iGmS6y • Sep 4, 2011
Thankyou Simplycoder..That's the answer I wanted...why..? Now it's clear.. -
@vinci-e4PtMU • Sep 4, 2011
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-e4PtMU • Sep 4, 2011
also we use abstract class when we know absolutely that the sub classes will implement abstract methods in different ways -
@manishks-AZybhs • Sep 4, 2011
,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-CCq72H • Jul 26, 2012
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-uI3S2X • Jul 27, 2012
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..