why is inheritance important?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2009
Posts: 148
Reputation: mahela007 is an unknown quantity at this point 
Solved Threads: 1
mahela007 mahela007 is offline Offline
Junior Poster

why is inheritance important?

 
0
  #1
Feb 14th, 2009
Why is inheritance an important aspect of object oriented programming?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,606
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: why is inheritance important?

 
2
  #2
Feb 14th, 2009
Consider this:

Say (just pretend) you have a objects called CD with the following attributes:

shape
size
capacity

[Actions/Methods]
Spin
Record

Then you would have to implement all of those properties and actions. Now say you wanted an object called DVD with the exact same attributes. Ordinarily, you would have to reimplement all of those attributes, even though shape, size, and Spin are the same.

Using inheritance, you can "inherit" DVD from CD and automatically get all of its attributes; the only ones you would have to re-implement are capacity and Record since they are different.

That's what inheritance does in a nutshell. It allows you to reuse the code of other objects where the code would be exactly the same.

Note: I don't vouch for the technical correctness of my example.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: why is inheritance important?

 
1
  #3
Feb 14th, 2009
Just to add to scru's point: Being able to re-use code is an extremely important but sometimes overlooked feature of programming. Its main value is not saving time, but eliminating bugs.

That is, if the code for CD works provably correctly (or testably correct even), then a DVD class built on top of it will be solid so long as DVD doesn't muck with the internals.

Jeff
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 173
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: why is inheritance important?

 
0
  #4
Feb 14th, 2009
Originally Posted by jrcagle View Post
Just to add to scru's point: Being able to re-use code is an extremely important but sometimes overlooked feature of programming. Its main value is not saving time, but eliminating bugs.

That is, if the code for CD works provably correctly (or testably correct even), then a DVD class built on top of it will be solid so long as DVD doesn't muck with the internals.

Jeff
Case in point are Python's own modules, thoroughly tested and optimized. You can inherit any of those classes into your own class and customize from there.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 907
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: why is inheritance important?

 
0
  #5
Feb 15th, 2009
And you can add to things as well and change things around. So you can have a class of Line, that has:

Attributes
start
end

And if you wanted to make box, well a box is just a complicated set of lines, so if you inherit from lines then you already have some stuff sorted out, though you can overwrite anything in lines by redefining it in box.

So box could have

Attributes
list_of_lines
area
curcumference

And then you can have a cube, which is just a number of boxes joined together, i guess i should have called box square huh? BUt it means that when making the cube you do not have to worry about lines, they are already dealt with, you do not worry about the squares being put together, they are already working, so all you have to do is work out the arrangement of the squares.

Classes generally are there and tend to (if used well) make your job a lot easier.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC