Forum guidelines
AuthorQuestion
Advantages of abstraction
Posted on: 30 Jul 2008

Hello

I have a question about abstraction. I just finished reading OOPS Concepts and .NET Part 2: Inheritance, Abstraction, & Polymorphism which was a really good article and very helpful start to OOP capabilities but I still don’t understand the advantages of abstraction.

 For example, say I have a parent class called A with an abstract method getInfo(). Say the Class A has 2 child classes called B and C. Both B and C have an overridden method called GetInfo().// code in blue

The code in orange doesnt have abstraction, just 2 classes B and C inheiting a property form parent class A. so my question is why the code in blue better than the code in orange, in other words what is the advantages of abstraction.

                                                Class A
                                                _property1;
                                                getInfo() //abstract method

Class B                                                                                    Class C
getInfo() //override method                                                         getInfo() //override method


                                                            Class A
                                                            _property1;

Class B                                                                                                                                          Class C

GetB() //just another function which has the same code as ClassB.getInfo()     
                                GetC() //just another function which has the same code as ClassC.getInfo()

                                       

 Thanks in adavance and i hope that my question makes sense



AuthorReply
Re: Advantages of abstraction
Posted on: 30 Jul 2008  
Hi, well we know that we cant create the object of abstract class but it has to be Inherite,I will tell you one real time Example like a bank account. People can have savings accounts, checking accounts, credit accounts, investment accounts, but not the generic bank accounts. In this case, bank account can be an abstract class and all the other specialized bank accounts inherit from bank account, thats all
Thanks

Re: Advantages of abstraction
Posted on: 30 Jul 2008  
Thank you khan for your reply but i still dont get the advantage of abstraction. i mean in the example you mentioned u can use inheritance why would use abstraction too??? i mean whats the point of declaring some method in a base class abstract that has no code in it  and then inherit that method in a sub class  and then have the code there.. i just dont see the point in inheriting something thats empty and cannot be instantiated.. i mean the abstract method is totally useless so why write the extra code.

thanks again

Re: Advantages of abstraction
Posted on: 30 Jul 2008  

Abstraction in OOP is used by advanced designers and architects when they design their class libraries that may be used by some developers and used to enforce some design rules. If you have a class library and you want to make sure the users of that class library must inherite a class and must override some methods before they can even use the library. By enforcing this rule, you may be doing login in this class ( say initialization of some engine defined ), your class library makes sure it has what it needs to execute next peice of code.

Here is a detailed reading on abstract classes:

Abstract Classes And Methods

This is a detailed analysis of Abstract classes and methods in C# with some ... The keyword abstract can be used with both classes and methods in C# to ...


C# Corner
Re: Advantages of abstraction
Posted on: 31 Jul 2008  

I suggest you read Design Patterns along with your studying. This will much help on your understanding of abstraction, interfaces, overriding and other OOAD concepts.

Thanks!



Sunny Chen


> National Certified System Analyst
> MCP/MCAD
> Consultant of China System Analyst Institution


Personal Site:
http://www.sunnychen.org

http://www.codeplex.com/gulu
http://www.codeplex.com/StoreDDD



SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.