The Artima Developer Community
Sponsored Link

Java Answers Forum
I want know the Main Disadvantages using Inheritance

8 replies on 1 page. Most recent reply: Jun 9, 2005 2:42 AM by Denis Krukovsky

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
srinivasa raju

Posts: 1
Nickname: raju17
Registered: Jun, 2005

I want know the Main Disadvantages using Inheritance Posted: Jun 1, 2005 9:20 PM
Reply to this message Reply
Advertisement
I want know the Main Disadvantages using Inheritance . can u plz give me some response.


Shashank D. Jha

Posts: 68
Nickname: shashankd
Registered: May, 2004

Re: I want know the Main Disadvantages using Inheritance Posted: Jun 5, 2005 12:23 AM
Reply to this message Reply
responding to Srinivas
> I want know the Main Disadvantages using Inheritance . can
> u plz give me some response.

Main disadvantage of using inheritance is that the two classes (base and inherited class) gets tightly coupled.

This means one cannot be used independent of each other. This seems trivial and natural outcome of this relationship. But becomes significant when we talk of re-usability of these classes.

Also with time, both base as well as derived classes tends to bloat, when during maintainence phase, or while adding new features they are required to be changed.

Either of both of them are changed to accomodate changes.

This further makes them more tightly coupled and lots of contextual information tends to be added.

This seriously affects their usage across diff application.


regards,

------------
Shashank D. Jha
iCMG
e-mail : shashank@icmgworld.com
Phone : +91-80-98451 87302

Parag Shah

Posts: 24
Nickname: parags
Registered: Mar, 2003

Re: I want know the Main Disadvantages using Inheritance Posted: Jun 6, 2005 5:27 AM
Reply to this message Reply
I am not quite sure I understand the question. Disadvantge or advantage would be context sensitive. I will still try to answer the question.
Inheritence has two uses - it facilitates code reuse and polymorphism. Inheritence should be used to utilize existing code only if there is a IS-A or IS-LIKE-A relationship between the subclass and superclass. Very often people use inheritence to achieve code reuse when such a relationship does not exist. So even though we have reused code the resulting classes lose their coherence and any clear responsibility. This can lead to extremely unmaintainable code and perhaps even buggy code.
Another disadvantage could be that in languages like Java which do not allow multiple inheritence, once we inherit from a class we lose the aility to inherit from another class. Which is why inheritence should be used sparingly.

However when polymorphism is needed, we have to use inheritence. There is no other way.

I am still not sure if generally speaking there any disadvantages to inheritence. Like all tools, if used properly it is very powerfull, and if used improperly then it can result in bad code.

Hope that helps

Shashank D. Jha

Posts: 68
Nickname: shashankd
Registered: May, 2004

Re: I want know the Main Disadvantages using Inheritance Posted: Jun 6, 2005 6:08 AM
Reply to this message Reply
responding to Parag

> However when polymorphism is needed, we have to use
> inheritence. There is no other way.

In java we nay use interface for the same.

Otherwise I agree with whatever you have written.

Coming back to his qs.
What I got from his question was that what are the natural disadvantages of using inheritance?

As mentioned in earlier mail, tight coupling, independently non modifiable and les re-usbaiblity are some of the disadvantages of using inheritance.

------------
Shashank D. Jha
iCMG
e-mail : shashank@icmgworld.com
Phone : +91-80-98451 87302

Parag Shah

Posts: 24
Nickname: parags
Registered: Mar, 2003

Re: I want know the Main Disadvantages using Inheritance Posted: Jun 7, 2005 5:51 AM
Reply to this message Reply
> As mentioned in earlier mail, tight coupling,
> independently non modifiable and les re-usbaiblity are
> some of the disadvantages of using inheritance.

Thats true inheritence does tightly couple the super and sub class. However... and I am just thinking aloud here... if we do not use inheritence, then we have to use composition, which also creates a coupling. So now the question is which of the two couplings is more maintainable.

Aspects that can change in any class are:
- changing the method signature
- adding a method
- deleting a method
- changing the implementation of a method

1. If a method signature is changed then we will be affected in both cases (inheritence & composition)

2. Adding a method will not have a direct effect, unless we discover that the new method does something an old method did in a better way. In this case we may want to refactor our code to use the new method. I guess here the implication is again the same.

3. If a method is deleted in the superclass or aggregate, then we will have to refactor if we were using that method. Here things can get a bit complicated in case of inheritence because our programs will still compile, but the methods of the subclass will no longer be overriding superclass methods. These methods will become independant methods in their own right.

4. Changing the implementation of a method should not have any effect either because the implementation is supposed to be a black box.

To conclude inheritence creates a tighter coupling because of point 4.

Offcourse here we are assuming that we do not need to use polymorphism and we are using inheritence purely for code reuse.

Does this cover all the implications or should we add some more?

nes

Posts: 137
Nickname: nn
Registered: Jul, 2004

Re: I want know the Main Disadvantages using Inheritance Posted: Jun 7, 2005 11:03 AM
Reply to this message Reply
> Aspects that can change in any class are:
> - changing the method signature
> - adding a method
> - deleting a method
> - changing the implementation of a method

good points but are you taking into consideration also if you use the subclass somewhere else?

Class B inherits from A and class D contains C. Considering methods are public:

1. Change method signature. Changes in C only affect D. Changes in A may affect other classes that use B.

2. Adding a method. None is affected.

3. Deleting a method. Idem 1 unless methods are overridden

4. If side effects of a method changes. Methods in C have to be traced back to its uses in D and in turn where they are used elsewhere. Since we are using indirection we might be able to adapt methods in D to hide the changes in C to everybody else. Methods in A have to be traced back to all its direct and indirect calls and those must be adapted unless they are overridden in which case it does not matter.

Shashank D. Jha

Posts: 68
Nickname: shashankd
Registered: May, 2004

Re: I want know the Main Disadvantages using Inheritance Posted: Jun 7, 2005 11:48 AM
Reply to this message Reply
responding to Parag

> Thats true inheritence does tightly couple the super and
> sub class. However... and I am just thinking aloud here...
> if we do not use inheritence, then we have to use
> composition, which also creates a coupling. So now the
> question is which of the two couplings is more
> maintainable.

Composition creates a substitutable coupling. That is whole depends on type of parts (not its actual implementaition). Moreover part can be used independently in another context. Without adding its own added behvaviour to its whole! Unless whole wants to expose those behaviour!

So if you look into current trends for better design there is growing trend for using composition than inheritance.

> Aspects that can change in any class are:
> - changing the method signature
> - adding a method
> - deleting a method
> - changing the implementation of a method

In OO smallest unit of re-usability, or substitutability is an object or a class.

When we abstract problem space in terms of behaviour of abstracted objects, we are sure/ ensure that one logic is abstracted only once within one object.

So when we talk of re-usability etc. naturally we dont talk of re-using a function outside scope of a class/ object.

If we just add/ delete a method its too low level changes to be talk about those things.

When we want to extend or add new behaviour to an object, we may like to extend its behaviour by adding a new type behaviour to it or add new role to an existing object. Or adding some behaviour directly to it. The later case is like extending a class.

In a better programming we call a class should be Open for extension for closed for modification.

In that sense, modifying method signature, adding/ deleting to make it better may be called as "refactoring". Not exactly an issue what we were discussing.

As no doubt better abstraction,design do always changes with time, exp, etc.

As for as better implementation is concerned, as said if that is available as substitutable part, we may not need to open the code of whole as it depends on type of part. and just replace older class with new one and instantiation code of part.



> 1. If a method signature is changed then we will be
> affected in both cases (inheritence & composition)

Too low level change.

> 2. Adding a method will not have a direct effect, unless
> we discover that the new method does something an old
> method did in a better way. In this case we may want to
> refactor our code to use the new method. I guess here the
> implication is again the same.

Extending a class/ object behaviour. Can be better done if this new behaviour could be abstracted as part of another object/ class.

> 4. Changing the implementation of a method should not have
> any effect either because the implementation is supposed
> to be a black box.
> To conclude inheritence creates a tighter coupling because
> of point 4.

Becomes more meaning ful when an object displays more than one role. Then displaying expected behaviour specific to a role or type requires more complex polymorphism, that is better writtten as composition than inheritance to avoid confusion in devlelopment, maintainenece, extense and have loose coupling.

So you rightly concluded that inheritance in this case creates tighter coupling so should be avoided and implemented as aggregation/ composition.

> Offcourse here we are assuming that we do not need to use
> polymorphism and we are using inheritence purely for code
> reuse.

This is the reason. We as inheritance adds up too much contextual information within inherited and may be base class. So try abstract the base class behavour or part of derived class behaviour as a part of some independent object.


------------
Shashank D. Jha
iCMG
e-mail : shashank@icmgworld.com
Phone : +91-80-98451 87302

A.B.GaneshMoorthy

Posts: 3
Nickname: arjuna
Registered: Jun, 2005

Re: I want know the Main Disadvantages using Inheritance Posted: Jun 8, 2005 12:56 AM
Reply to this message Reply
1)
ripple effect which occurs whenever a change is made to the base class which inturn ripple down to all the derived classes.

2)
object transmutation
if frequent changes are made to the base and derived classes.

Denis Krukovsky

Posts: 6
Nickname: dkrukovsky
Registered: Feb, 2005

Re: I want know the Main Disadvantages using Inheritance Posted: Jun 9, 2005 2:42 AM
Reply to this message Reply
I would argue that inheritance resulsts in tight coupling. See
http://dkrukovsky.blogspot.com/2005/06/is-inheritance-bad.html

Denis Krukovsky
http://dotuseful.sourceforge.net/
http://dkrukovsky.blogspot.com/

Flat View: This topic has 8 replies on 1 page
Topic: Permission for file creation Previous Topic   Next Topic Topic: Debugging Large Projects

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use