1

I have seen a few questions on the topic, but they all assume knowledge of inheritance. The example in my book is before the inheritance chapter, so the parent class is java.long.Object.

1. Scenario: my class FotoApparat has no custom constructor or any constructor at all and I create an instance of FotoApparat with FotoApparat meinFotoApparat = new FotoApparat()

Question: As my class has no constructor and also no super() call, I assume the program checks the parent Object class for a suitable constructor, which should be new Object(), right? If yes, is this still considered an "implicit" super() call?

2. Scenario: I create a custom constructor (by using eclipse source) which takes on parameters. In the generated constructor the super() call is added in the very beginning, which I assume is the actual implicit call I keep reading about. I read on javapoint that when an instance of a class is created, an instance of the parent class is also created, which is referenced by super().

Question: I read that this super() call can be removed from the constructor, but if it is removed and I use a constructor that takes on parameters, then (without super()) how is this parent object created ?!

3
  • "when an instance of a class is created, an instance of the parent class is also created, which is referenced by super()", not really the best way to describe it: only one instance is created, but this instance is both an instance of the class and an instance of its parent at the same time. Commented Aug 25, 2020 at 19:54
  • By "and also no space() call" did you perhaps mean "and also no super() call"? (if not then could you clarify what you mean by space() call)? Commented Aug 25, 2020 at 20:19
  • Yes, sorry, I ment super() Commented Aug 26, 2020 at 7:52

2 Answers 2

4

Scenario 1: If you don't define any constructor, a default, no-argument, constructor is created for you. That's the one that is called when using new FotoApparat(). This default constructor then calls the constructor on Object (see scenario 2.)

Scenario 2: If you don't explicitly call super(), this call is still done implicitly. It is possible however that the parent object does not have a constructor without arguments, in which case you are required to call a specific constructor.

Sign up to request clarification or add additional context in comments.

Comments

1

As my class has no constructor and also no space() call, I assume the program checks the parent Object class for a suitable constructor

Not quite. If you don't define a constructor, the compiler creates one for you. This constructor takes no arguments, and the only thing it does is call the super class constructor super().

when an instance of a class is created, an instance of the parent class is also created

Not quite: only one instance is created. There is no separate parent class instance.

The statement is not entirely incorrect because thanks to inheritance, the one instance of the child class that is created is also an instance of the parent class.

I read that this super() call can be removed from the constructor, but if it is removed and I use a constructor that takes on parameters, then (without super()) how is this parent object created ?!

In this scenario the compiler inserts a call to the no-argument super class constructor super(). But this does not create a separate "parent object" - only one object is created.

What your studies may not have made clear is the distinction between object creation and initialization. Calling a constructor does not "create" an object. An object is created by reserving space for it in memory. After the memory has been reserved, the constructor is called to "initialize" the object.

4 Comments

@Rick @Joni: You both say if there is no constructor, the compiler creates one for me - Question: I assume this is referred to as "run time", correct? Where are the entities created during run time created? (like a hidden file or something) Regarding creation and initialization: if we take a primitive data type, creation (reserving memory) would be int number; and initialization would be int number = 123;. If I understand correctly, the equivalent of this for objects is that new FotoApparat(); or this implicit super() call only reserves memory ?
And a constructor that takes parameters is then the initialization of the object, correct? When is an object initialized if you use the new FotoApparat() constructor? When you assign a value to one of the attributes maybe ?
The default constructor is created at compile time, not run time. It's as if you had added it in the source code yourself. Regarding primitive values such as 7 or 3.14 or 'S', they simply exist without needing to "create" them. Don't confuse values with variables: variables are "created" when their container is created. Instance variables are created when class instance is created, local variables are created when the method is called.
Running the code in a constructor always initializes an object. It doesn't matter if it's the default no-parameter constructor generated by the compiler, or one that you wrote yourself. "Initialize" means "set the fields in the object to initial values so that it can be used by the rest of the program."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.