So In my coding I'm creating an object of the class say "decor", I'm writing this inside of the class say "Furniture". So if I want to create that object of the class Decor then Decor will need the constructor, can I create the constructor for decor inside of the class Furniture or must it be inside the class that the constructor is for ?
To start with the last part, a constructor for a class is always written as part of the class, regardless of where you use objects of it.
There may be some confusion what "constructor" is. For me, it is a method like "public Furniture()" below:
public class Furniture {
public Furniture() { ... }
....
}
When you instantiate a class (with "new Furniture()"), you go from nothing to an object of type Furniture. As part of that creation process, the constructor is called, and its purpose is to initialize the new object, ie give all its data members a value (it has none, so that's quite simple, but see below). After the constructor finishes, you have an object, and you get a reference to the new object from the "new" statement. Usually, the first thing you do is to store that reference, as in "Furniture furniture = new Furniture();"
If a class has variables of its own, like your decor class, there are two ways to initialize the variables.
public class Decor {
private Furniture table;
private Furniture chair = new Furniture();
public Decor() {
table = new Furniture();
....
}
...
}
The Decor class has its own "public Decor" constructor. The statement in it ("table = new Furniture();") constructs a new furniture, and assigns it to the object variable "table". The "private Furniture chair = new Furniture();" here does functionally the same as what "table = new Furniture();" did, it constructs a new furniture object, and assigns it to the "chair" variable.
So this is how it works, each class has its own constructor that initializes all variables of that class. If the type of the variables is again a class, those constructors are executed first to construct the smaller objects first.
The advantage of having a constructor in its own class is that the inner objects don't need to know anything about where they are used. You could use your Furniture class also for furniture at a ship, for example. The ship then needs to know there exists a Furniture class, but the Furniture class doesn't need to know about ships at all.
Does this depend on the access modifier for the class Furniture ?
It doesn't, the access modifier only specifies who can call the method. For example, you could make constructor less available than some other method, like "public int compute(int x)". An external user then cannot create an object of that class by itself (only objects with sufficient access rights to its constructors can call the constructor, and in doing so, create the object), but the external user could call the "compute" method, if it gets a reference to an object of that type. In other words, access modifiers only ensure that "wrong" objects don't have access to methods they are not supposed to use.
This is quite full of holes though, in practice. The various classifiers are not fine-grained enough to catch all bad uses. It does however give you some crude control, anything beyond that you will have to handle manually, mostly by documenting who is supposed to use a method.