• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

OO Design

 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Suppose i have a design where in the parent class has a set of attributes and there are few child classes derived from the parent class.

Now I have a req where i would like to inherit an attribute from the parent in one of the child classes but not in other child classes.



Thx,
A Kumar
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's difficult to give you an anwser without knowing the exact circumstances you are facing.

Anyhow, I guess you should re-think your desing:
- can you separate the responsibilities of your root class so that problem will not arraise?
- can you introduce an additional layer to avoid it?
- perhaps you can use object composition instead of inheritance?
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there are several ranchers that for sure will have an expert view about your case

for me, just my two cents:

why dont you remove your atribute from super class and put in the child class that needs it?
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose I were to have a parent class A- with 3 attr..

Now There are two child classes of this...

I inherit them,,,and do the necessary logic...Later on...the client

asks for certain modifications...which makes it necessary for me..to change my parent class...

What i want to know is that...

Its easy if we want to add attributes to an existing design structure but not scale down an existing parent class more so if there are
numerous sub classes...

Except of course if u are willing to spend time on lot of refactoring ..

Thnk u
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Later on...the client

asks for certain modifications...which makes it necessary for me..to change my parent class...


once again, just my two cents

i guess in that case the use of interfaces is much better then inheritance; have a look at Strategy Pattern
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank u..

 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Changes to a parent class are risky and sometimes expensive. There are pretty good reasons to prefer "implements" or composition over "extends", especially when you are tempted to extend a concrete class or build a deep tree of inheritance.

The Liskov Substition Principle tells us that any code written to use class A should work equally well with any class B that extends A. If you write B so it doesn't provide all the functionality of A you'll break the rule at your own peril. This may be telling you that B shouldn't extend A, or maybe nothing should extend A and you should build up the "child" classes through composition.

Give us a holler if any of that raises more questions than it answers.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by A Kumar:
Except of course if u are willing to spend time on lot of refactoring ..



I'd spend as much time refactoring as necessary to make sure that the next time a similar feature request comes, it would be dead easy to implement.

Can you tell us more about the kind of requests you get?
reply
    Bookmark Topic Watch Topic
  • New Topic