943,650 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 937
  • Python RSS
Feb 14th, 2009
0

why is inheritance important?

Expand Post »
Why is inheritance an important aspect of object oriented programming?
Similar Threads
Reputation Points: 16
Solved Threads: 2
Posting Whiz in Training
mahela007 is offline Offline
214 posts
since Feb 2009
Feb 14th, 2009
2

Re: why is inheritance important?

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.
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Feb 14th, 2009
1

Re: why is inheritance important?

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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Feb 14th, 2009
0

Re: why is inheritance important?

Click to Expand / Collapse  Quote originally posted by jrcagle ...
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.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Feb 15th, 2009
0

Re: why is inheritance important?

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.
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Help with == Operator and If Statements
Next Thread in Python Forum Timeline: Class properties (how?)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC