Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composition vs Inner Classes

What are the differences and the similarities between composition and inner classes? I am trying to learn the principles of Java and try to figure out the whole image. For me is better to make analogies and to see the differences and similarities between concepts, in order to use them corectly.

Definition of composition: "Composition is the design technique to implement has-a relationship in classes". "Java composition is achieved by using instance variables that refers to other objects"

Definition of inner class(or member class,not anonymous): "A member class is also defined as a member of an enclosing class, but is not declared with the static modifier. This type of inner class is analogous to an instance method or field. An instance of a member class is always associated with an instance of the enclosing class, and the code of a member class has access to all the fields and methods"

So, by confronting the two definitions, i see some similarities:

1. Both have HAS-A relationship
2. Both are strictly dependent on the outer class lifetime
3. can be declared private

Differences:

1. Inner classes uses classes, composition use instances (?!)
2. In composition no restriction to use the variables of the "outer" class

Please correct me if I am totally wrong, I need to trace better the limits of two concepts.

like image 768
Alexis Avatar asked Nov 01 '16 13:11

Alexis


People also ask

Is inner class A composition?

You're right that composition and inner-classes both implement "has-a" schemes, but they have a lot of differences. Composition is when your class has an instance of another class as an instance variable (sometimes called a field). otherObject in this case is just a reference to an instance of another object of type B.

What is the difference between class and inner class?

A class that is defined within another class is called a nested class. An inner class, on the other hand, is a non-static type, a particular specimen of a nested class.

What is the difference between nested and inner classes?

Non-static nested classes are called inner classes. Nested classes that are declared static are called static nested classes. A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private.

What is meant by inner class?

An inner class in Java is defined as a class that is declared inside another class. Inner classes are often used to create helper classes, such as views or adapters that are used by the outer class. Inner classes can also be used to create nested data structures, such as a linked list.


2 Answers

These two concpets are related. To better organize my thoughs let's define A and B:

  • A is a class member composed by an inner class.
  • B is a class member composed by an instance of an external class.

Example:

class Driver {
}

class Car {

    // A
    class Engine {
        void start() {
            if (gasLevel > 0) {
                ...
            }
        }
    }

    //B
    Driver driver;

    int gasLevel;

}

That said:

  • A is a field
  • B is a field
  • A is an inner class
  • B is not a inner class
  • A has access to Car internal state (gasLevel)
  • B don't have access to Car internal state (gasLevel)
  • Engine don't exists without an instance of Car.
  • Driver exists without an instance of Car.

To sum up, composition between two distinct classes and composition with an inner class are ways to control the level of coupling and cohesion of your system.

like image 52
Leonardo Cruz Avatar answered Sep 20 '22 06:09

Leonardo Cruz


You're right that composition and inner-classes both implement "has-a" schemes, but they have a lot of differences.

Composition is when your class has an instance of another class as an instance variable (sometimes called a field).

public class A {
    B otherObject;
    /* Other fields and methods go here */
}

otherObject in this case is just a reference to an instance of another object of type B. otherObject can exist independently of your instance of A, and multiple instances of A can have references to the same B.

An inner class is when the class definition is actually contained within the outer class.

public class C {
    class D {
        /* Inner class stuff goes here */
    }
}

This is used mostly for logical grouping. In this scenario, D is directly associated with C, whereas B may or may not be a logical child of A. Access to D outside of C becomes a little more complicated, but it can only be accessed through an existing instance of C.

So, by confronting the two definitions, i see some similarities: ... 3. can be declared private

You're right that both can be declared as private, but the meaning of private for these two is very different. private as used in composition means that access to that reference is restricted to that class. Say that in your execution code, you have an objectA of type A. You could NOT attempt to access objectA.otherObject. otherObject can only be referenced from the body of A. When you declare an inner class as private, it means that use of that entire class is restricted to the body of A. You couldn't use it anywhere in any other class.

like image 28
Elan Hamburger Avatar answered Sep 21 '22 06:09

Elan Hamburger