Results 1 to 5 of 5
  1. #1
    Santoshbk is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default What is the Composition in Java

    Hello to Everybody,

    What is the Composition in Java with respect to inheritance. When should i go for Composition than inheritance. What is its Advantage.

    Waiting for your reply.

    Thanks in Advance.
    Santosh

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I don't see much difference there actually. In composition you have to use an instance variable that are reference to other object. But in inheritance you have to extend the supper class into the sub class.

    Something like this.

    In composition,
    Java Code:
    class One{
    // Called back-end class
    }
    class Two{
    private One objOne = new One();
    // Called front-end class
    }
    In inheritance,
    Java Code:
    class One{
    // Called the supper class
    }
    class Two extend One{
    // Called the sub class
    }
    So what we have to mainly focus on the use of instance than extending a class. Actually it depends on the situation you work on. If you create more instance it can be a mess at all.

  3. #3
    rpwtdj's Avatar
    rpwtdj is offline Member
    Join Date
    Dec 2007
    Posts
    15
    Rep Power
    0

    Default

    If you have to override a final method in class A, you can define a class B that has a instance variable referring an object of class A, and "wrap" the whole class.

    Java Code:
    class A{
    final void methodA(){
     ...
     }
    }
    Java Code:
    class B{
    class A ref;
    final void methodA()
     {
     ...
     ref.methodA();
     ...
     }
    }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    True, but what the Composition means use an instance of a class inside the other.

    This is a good choice, but I'm not how many people really familiar with this. :) ;)

  5. #5
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    Can Any One Say What Is The Defference Between Composition, Association, Aggregation?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •