When to use inheritance and when not to?

I've just been trying find out when to use inheritance and when not to use inheritance? I know that inheritance gives advantages such as code reuse and maintainability. But when are their times not to use inheritance that may effect the design of a project?
Also when should one consider aggregation over inheritance(is-a relationship)?
thanks,
plandis

Hi
Answering this question usually requires a few books, so, I'm sorry, it's not really as simple as choosing between using a screwdriver vs. a hammer.
But, very short, inheritance should ideally only be used when there exist a strict 'is-a' relationship. However, sometimes it may be clever to inherit when you have a 'is-almost-a' relationship... And sometimes this is a stupid move. This depends on the context/circumstances.
Aggregation is normally considered a 'has-a' relationship, thereby differing clearly from inheritance. The old 'car' model says "a car is-a vehicle and has-some (at least) wheels."
The wheels are aggregated to the car class, that itself is inherited from vehicle.
However, this is just a good rule-of-thumb and there may be circumstantial reasons for not following these guidelines.

Similar Messages

  • When to use inheritance and When to use Composition

    java support both inheratiance and composition .I also know "is a" and
    "has a realitionship" but there is always a problem with me to understanding whethere i have to use extends or having a object of a class has a member of othere class.
    and also "A pure OOP must support polimorphisim,inheretiance,encapluction,Abstraction and Composition" correct me if i am wrong
    thank you and have a nice day.

    Bruce Eckel, author of Thinking In Java, has this to say about composition vs. inheritance:
    When deciding between inheritance and composition, ask if you need to upcast to the base type. If not, prefer composition (member objects) to inheritance. This can eliminate the perceived need for multiple base types. If you inherit, users will think they are supposed to upcast.
    Choose composition first when creating new classes from existing classes. You should only used inheritance if it is required by your design. If you use inheritance where composition will work, your designs will become needlessly complicated.
    Bill Venners: Composition versus Inheritance
    Use inheritance (of implementation) only when the class satisfies the following criteria:
    1) "Is a special kind of," not "is a role played by a";
    2) Never needs to transmute to be an object in some other class;
    3) Extends rather than overrides or nullifies superclass;
    4) Does not subclass what is merely a utility class (useful functionality you'd like to reuse); and
    5) Within PD: expresses special kinds of roles, transactions, or things.
    -- from Java Design: Building Better Apps and Applets (2nd Edition), by Peter Coad and Mark Mayfield

  • Masterdata - when to use it and when not to use it

    Hi!
    I have had some discussion with my colleagues about the use of masterdata on Oracle databases. Some of them tells me that I should use it if the data don't change often (ofcourse.....). This should ALWAYS be done they say. But I don't agree on this..
    Is it really necessary to make a lot of masterdata (and also views that has to be used) if the data is only to be loaded once a day, and the number of records is max 1000?
    I can't see the effect on this...
    Can someone give me a guideline for when to use masterdata, and when I don't need to use masterdata (if there are any article on this, please give it to me).
    Best Regards
    Helge

    Hi
    Answering this question usually requires a few books, so, I'm sorry, it's not really as simple as choosing between using a screwdriver vs. a hammer.
    But, very short, inheritance should ideally only be used when there exist a strict 'is-a' relationship. However, sometimes it may be clever to inherit when you have a 'is-almost-a' relationship... And sometimes this is a stupid move. This depends on the context/circumstances.
    Aggregation is normally considered a 'has-a' relationship, thereby differing clearly from inheritance. The old 'car' model says "a car is-a vehicle and has-some (at least) wheels."
    The wheels are aggregated to the car class, that itself is inherited from vehicle.
    However, this is just a good rule-of-thumb and there may be circumstantial reasons for not following these guidelines.

  • WHEN TO USE SYS_REFCURSOR AND WHEN NOT TO USE REFCURSOR

    SYS_REFCURSOR is a Weakly Defined Cursor
    DECLARE emp_refcur SYS_REFCURSOR;
    when this is so simple
    why this
    DECLARE TYPE emp_cur_type IS REF CURSOR RETURN emp%ROWTYPE; my_rec emp_cur_type; ?
    Can anyone explain in what situations we should prefer among these ..when both can give same result set .

    > WHEN TO USE SYS_REFCURSOR AND WHEN NOT TO USE REFCURSOR
    When the requirements define that one is better suited than the other.
    There are no hard and fast rules about when to use a specific tool. There are however basic concepts of what the purpose of the tool is, and what typical problems the tool can address.
    Ref cursors in PL/SQL.. usually not the best of ideas. Ref cursors are designed for client-server processing.
    1) The client makes a request for data/information.
    2) PL applies the back-end server logic and construct a SQL statement for the client.
    3) PL passes the SQL to the SQL engine where it is parsed and a SQL cursor created.
    4) PL passes a reference handle to SQL cursor back to the client
    5) The client can now fetch the rows output from the cursor.
    Advantages.
    - The client does not need to know SQL, db structures, business logic, etc
    - The PL code can be improved, modified, new business logic added, without even touching a single byte of client code
    So what are the advantages where the "client" in the above is another PL/SQL program? None really.
    A "real" client (an external one like Java, C#, Delphi, etc) can receive the ref cursor handle and dynamically at run-time determine the projection (output) of the cursor.
    PL code cannot (unless using the latest 11g features). In PL code, a ref cursor is actually implemented as a DBMS_SQL cursor. A DBMS_SQL cursor is to PL code what ref cursor is to Java/C#/etc.
    So whether you define a strongly typed or weakly typed ref cursor in PL/SQL code... typical response is "so what?" as ref cursors are not really intended for PL code. Not that there are exceptions - they do come in handy at times.. but as exceptions to the rule.
    So my response is "so what?" - it is more important to *correctly" chose what type of client cursor data type you want to use to interact with the back-end SQL cursor. Once that decision has been made (based on technical sound logic), then one can look at  (minor) issues such as weak vs. strong ref cursors.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • When to use jsp,and when to use servlet?

    I think that jsp and servlet can realize the same functions, because when run a jsp, it is transferred to a servlet program, so when to use jsp and when to use servlet?
    I am now developing the input interface for a website, I just use jsp and javabean to connect to weblogic and database, and I didn't use servlet, Is there any unseemliness?
    Thank you!

    IMHO I use servlets to control the flow between my jsp's based on a number of factors in a webapp. For instance, user authorization. If a user has the authorization to conduct various administrative functions on an application (like change user rights, reset passwords etc) they will have access to specific buttons or links on some screens that others will not. I use servlets to establish what access rights a user has and direct them to the appropriate pages. I also use servlets to test data validity on form input screens. I know that I can also do this with JavaScript but that can be disabled by the client and in order to prevent that I also double check the form input from a servlet. All my jsp's do is display the results of a business process (which is held in a JavaBean or EJB) and the servlets act as the controllers for the application, connecting to multiple databases, verifying application state, flow control etc. I try to keep the jsp as simple as possible as some of them are maintained by html developers who lack the necessary experience to write java code. I hope this helps.

  • When to use interface and when Abstract Class?

    In a recent interview I was asked "When to use interface and when Abstract Class?" Explain with an example.
    Also in what situations a class should be made final(real time example)

    Interface is a pure contract with no implementation. Typically used to define a communication contract between two different sub-systems. Example EJB home interface. This also allows the design to change as long as the contract is met.
    Abstract class is when there exists a lot of common functionality already known and can be coded. However, a few unknowns exists (typically about data) for which abstract methods need to be defined and implemented by the sub class.
    Example: Consider a workflow engine. A great example for abstract class. The workflow process has lot of common code that is independent of the workflow type (vendor flow, contract flow, payment flow etc). However, certain decisions on the route to take will depend on value of data being submitted. So the base class will define a abstract Data getData() method and proceed assuming data will come. The implementing subclass will provide the actual logic for getting the data.
    Also see the "Template" design pattern.
    Note: To some extent the common code design drives the behavior of the abstract methods. So if the design changes then so "might" the behavior expected from the abstract methods.

  • When to use wsimport and when to use wsgen?

    When do you use wsimport and when do you use wsgen?
    Thanks.

    JBStonehenge wrote:
    You are right: wsgen is on the server side, wsimport is on the client side.guess that's what i get for not reading both doc pages. i've used them before, but couldn't remember which was which. i did a quick check of the wsimport docs and misread the page, posting my original reply above ( [wsgen |http://java.sun.com/javase/6/docs/technotes/tools/share/wsgen.html] docs for reference as well).

  • When to use @Resource and when to use @EJB for dependency injection

    Hi,
    When do you use
    @Resource and when do you use @EJB for dependency injection?
    Thanks

    Captain obvious: Use @EJB for injection of EJBs, and @Resource for everything else.
    There was a discussion about this very topic quite recently, perhaps you can find it through the search.

  • Check box in tabel view?when to use itereator and when to use table view

    hi,
    I want to have a check box along with the other 7fields  in the table view.
    when the checkbox is checked and the merge complete push button is clicked, the respective code for merge is to be executed.
    how can I do this?
    could anyone tell me hw to get a check boz in table view
    when to use a  iterator in BSP, how table view n iterator in comparision ae used or function/work.
    Regards,
    Pavan P.

    Hi Pavan,
    Table View is an BSP element used to display mass data in a layout similar to a table (table view).
    <b>Iterator</b>is an attribute to modify rendering row-by-row, and make it dependent on the clicked row. In this way, you define an action from a particular line. This action is defined in columnDefinitions or overWriteDefinitions.
    <htmlb:tableView id = "tvX"
                     headerText          = "Department List"
                     design              = "standard"
                     headerVisible       = "true"
                     width               = "30%"
         selectedRowKeyTable = "<%= selectedrowindextable %>"
         onRowSelection      = "MyEventRowSelection"
         sort                = "server"
         keepSelectedRow     = "TRUE"
         selectionMode       = "MULTISELECT"
         table               = "<%= i_dept %>" >
    </htmlb:tableview>
    In your <b> OnInputProcessing </b>, Use this Iterator table to get the data of selected records.
    IF selectedrowindextable[] IS NOT INITIAL.
    DESCRIBE TABLE selectedrowindextable LINES no .
    Rgds,
    Jothi.
    Pls do close the thread if ur problem is solved.

  • When to use AMF and when to use RTMP??

    Hello can someone explain the difference??
    I am using all the defaults for the configurations files
    (remoting and datamanagement) and my app works perfectly on
    development mode where everything is on localhost, but when we try
    it on pre-production it keeps throwing an error saying that it
    couldn't connect to the RTMP channel. The only thing that changes
    between these two modes is that on development i access everything
    on localhost and on pre-production we point to another server.
    I am totally clueless why this is happening, i am bit
    confused why data-management uses RTMP and why remoting uses AMF,
    so if anyone could explain it to me it would be great.
    Thanks

    There's no special reason why one uses RTMP vs. AMF. Data
    management service can also use AMF and Remoting can also use RTMP.
    The only real requiement is that Data management service needs to
    be able to receive updates from the FDS/LCDS server. This can be
    achieved by using a channel that is capable of receiving pushed
    updates from the server (eg. RTMP) or a channel that is capable of
    polling for updates on the server (i.e. AMF-polling). Therefore,
    when you use AMF with Data management service, you need to have
    polling enabled with a polling interval.
    In terms of why AMF works but RTMP doesn't is that AMF
    channel uses AMF format over the HTTP protocol whereas RTMP channel
    uses simple TCP socket (and not HTTP). Therefore in some
    environments, RTMP will be blocked by the firewalls but AMF won't
    since it's like regular HTTP traffic. In this case, you can use AMF
    polling instead or use RTMPT which is new in LCDS 2.5.

  • When to use FLVPlayback and when to use MediaPlayback component

    Hi,
    I looked at at the FLVPLayback component and the
    MediaPlayback Component and I wonder when one should be used over
    the other? Any experienced designer out there that can tell me?
    thanks
    -Malena

    Hey, My problem with using flvplayback is that SOME windows
    vista computers, and i seriously cannot figure out exactly what it
    is but they will play the flv very choppy and so i had to use
    mediaplayback and it works fine on all computers that i have tested
    it on. it was VERY annoying because my client kept attempting to
    watch video from his computer and it came through as garbage and
    when i watched it, well it was perfect. so i tested it out on
    several different computers and operating systems and found that
    certain computers were just not compatible. I am assuming they were
    just not up to date? i dunno, i have a mac, dont have those
    problems :)

  • When to use PGA_AGGREGATE_TARGET and when to use temporary table space?

    Hi, all.
    I am a little confused with regard to when to use pga work area, and
    when to use temporary tablespace?
    Is there any one who could give me an advice or a document?
    Thanks in advance.
    Best Regards.
    Message was edited by:
    user507290

    You are more than a little confused... What has one to do with the other, do you think?
    All Oracle documentation is at http://tahiti.oracle.com - I suggest you start with the Concepts manual of your version

  • When to use Initiate and when Start Trigger?

    Hello I am talking about Scope5122 and DigitalIO6552.
    Im little confuse, what is the relation between Initiate VI to Start Trigger trigger.
    Thanks.

    HaD,
    Basically, the Initialize VI just gives you a session reference to the device where you can modify properties of the device and configure the device (sample rate, number of sample to acquire, channels to use, etc.). These properties all have to be configured before the device begins to acquire or generate. (Note: some properties can be modified or accessed after the device is acquiring or generating, but not all)  The Initiate VI will basically start the acquisition or generation to the device if some sort of trigger is not already configured. The Start Trigger VI allows you to configure a trigger for the device, if you do configure a trigger you will still have to call the Initiate VI, but only once you call this Initiate VI will it begin to wait for a trigger. If you do not configure a trigger the acquisition / generation would begin as soon as the Initiate VI was called. Hope this helps.
    Aaron W.
    National Instruments
    CLA, CTA and CPI

  • Hi ! when to use @Local and when to use @Remote?

    Hello,
    Say suppose i want to develop a multi tiered web application with presentation on 1 server, business logic on 1 server and database on 1 server, thus 3 servers altogether. But my all beans reside on single server ie 2nd server, so should i mark my beans as @Remote? or @Local? because my presentation is on 1st server and it will call bean on 2nd server. So, different JVM altogether.
    Thanks in advance!

    Why do you have to give any advise that is not related to the question?Why to get side track? If you know the answer and if you have time then answer it else wait and watch, you will learn something from other people's experience. Let others come and answer. But kindly don't teach me philosophy like read books and you will find answer there and this and that. If it was clear to me why do you think i would ask question and waste your as well as my time? I come from .Net and things in JEE are very different.
    This is a place to share knowledge and let others grow. I was just trying to self teach me reading tutorials here and there and asked question because it was not clear from the tutorial. Now, i know due to you nobody will come in this thread and answer this question.
    Thanks
    Edited by: Salman4u on Jan 27, 2010 12:57 AM

  • When to use BPEL and when to use workflow

    Could some one give me advice on this topic? BPEL workflow editor looks much nicer, but as far as I can see the workflow definitions and workflow instances are outside of the database.

    I had the same question before. You might like to look over these threads
    Automating Business Process.. should we go for BPEL??
    Automating Business Process.. Workflow or BPEL??
    we might together reach a better clear idea since Oracle has two products:
    Oracle Workflow
    Oracle BPEL
    This really made me confused a lot, but I beleive the above thread will make things more clear. Moreover, I could be open for everybody here to participate in order to make the picture more clear to us and others

Maybe you are looking for

  • J2I9 program J_1I57AE Proforma Register ecs and sh ecs issue

    Hi All, I have a requirement to display the opening and closing balnace of ECS and SH ECS along with the opening and closing CENVAT balances for input and capital goods. Can anyone tell me what logic can be used to get ECS and SH ECS. As per my under

  • ADF - View Objects

    Hi All, We are working on building a web application using on ADF. The landing page of our application is a dashboard screen that is read-only with 100 to 500 records displayed in a table. User can move to other pages and create requests. 1) When use

  • How can use the icons from keyboard?

    I want the little characters on my keyboard how go by getting this?

  • Import Server - Multiple Qualified Table - Map Crash

    Hello, Requirement: I need to update multiple qualified table using single source file. Preparation: I have created a map where mapping to multiple qualified table fields maually.                    Have created a Inbound Port using the above map. Pr

  • I am new to iPhone

    Hello, I am new to iPhone. I just bought a brand new iPhone 5s 32gb. Maybe my situation is silly or even absurd but I need some piece of mind. I bought the iPhone yesterday and as soon as I got home I didn't connect it and started using it with the b