Does invoking a constructor mean creating object? - java

When we create a Subclass object which extends an abstract class, the abstract class constructor also runs . But we know we cannot create objects of an abstract class. Hence does it mean that even if a constructor completes running without any exception, there is no guarantee whether an object is created?

Hence does it mean that even if a constructor completes running
without any exception, there is no guarantee whether an object is
created?
Simply speaking, a constructor does not create an object. It just initializes the state of the object. It's the new operator which creates the object. Now, let's understand this in little detail.
When you create an object using statement like this:
new MyClass();
The object is first created by the new operator. Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object.
Now consider the case of Abstract class and it's concrete SubClass, when you do like this:
AbstractClass obj = new ConcreteClass();
new operator creates an object of ConcreteClass, and invokes its constructor to initialize the state of the created object. In this process, the constructor of the abstract class is also called from the ConcreteClass constructor, to initialize the state of the object in the abstract class.
So, basically the object of AbstractClass is not created. It's just that it's constructor is invoked to initialize the state of the object.
Lessons Learnt:
The object is created by new operator, and not by the invocation of the constructor itself. So, the object is already created before any constructor is invoked.
Constructor is just used to initialize the state of the object created. It does not create an object itself.
An object state can also be contained in an abstract super class.
So, the purpose of invocation of Abstract class constructor, is only to initialize the object completely, and no object is created in process.
See:
Creation of new Class Instance - JLS-Section#12.5

But we know we cannot create objects of an Abstract class
Right but JVM can.
does it mean that even if a constructor completes running without any exception , there is no guarantee whether an object is created ?
The object is definitely created internally.
Does invoking a constructor mean creating object?
Not always. you can invoke constructor using super() and this() but it won't instantiate an object. (but will just invoke the constructor)
class AClass
{
AClass()
{
this(1); // will invoke constructor, but no object instatiated.
}
AClass(int a)
{
}
public static void main(String[] args)
{
AClass obj = new AClass(); // one object instantiated.
}
}

Barring any exceptions, the abstract class constructor is only run from within the subclass's constructor (as the first statement). Therefore you can be sure that every time a constructor is run, it is in the process of creating an object.
That said, you may call more than one constructor in the process of creating a single object, such as Subclass() calling Subclass(String) which calls AbstractClass via a super()call, and so forth.

Subclass == BaseClass + Extras you add in sub class
Thus when you create a subclass by calling its constructor, there is a call to base class constructor as well to make sure that all attributes (of the base class) are also properly initialized.

You can only call the abstract class constructor as a part of a concrete subclass constructor. This is OK, since the abstract class is extended into a concrete class and it is an object of that concrete class that is being created.

When you invoke a constructor using new, a new object is being created.
Now, as you probably already know, every constructor of any subclass, either implicitly or explicitly, directly or indirectly, invokes a constructor from the parent class (which, in turns, invokes one from its own parent class, all the way up to object). This is called constructor chaining.
The above, however doesn't mean that multiple objects are created. The object has been created at the new call and all constructors working on that object are already handed an allocated area. Therefore, constructor chaining does not create new objects. One call to new will return you one object.

This is how the flow works when you invoke the constructor of your subclass:
Constructor of Abstract class runs --> the object is half initialized here.
Constructor of subclass finishes execution --> The object is fully initialized and hence completely created here.

I am completely disagree that the object for an abstract class can not be created and only jvm can does it in case of Inheritance even a programmer can do it a t times whenever or wherever he/she intends to do so by using the concept of anonymous class:
Look at this code and try it by your Own
abstract class Amit{
void hai()
{System.out.print("Just Wanna say Hai");}
abstract void hello();
}
class Main{
stic public void main(String[]amit)
{
Amit aa=new Amit(){
void hello(){Sstem.out.print("I actually dont say hello to everyone");
}};
aa.hello(); }}

Related

Java constructors and super()

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 ?!
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.
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.

Does object creation of subclass create object of superclass, if yes Is it possible to access it in subclass?

My question is if we can access and use indirectly/implicitly created super class objects when we instantiate a subclass .
Lets say ClassA is super class of SubClassofA and we instantiate SubClassofA in a client class ClientClass using SubClassofA object = new SubClassofA();
Since whole inheritance hierarchy gets instantiated when we create a subclass objects, I was wondering, is it possible to access object of class ClassA in client class?
If not possible, what might be reasons? Wouldn't it save lots of heap memory if we can access super class objects without recreating those?
I might have misunderstood whole concept of constructor chaining and inheritance hierarchy but please let me know your thoughts on this.
public class ClassA {}
public class SubClassofA extends ClassA {}
public class ClientClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
SubClassofA object = new SubClassofA();
//Above construct means that an instance of super class ClassA exists too
// If we can use those super class instances directly, will it result in memeory saving?
//is it even possible to access implicitly created super class objects tied to subclass?
}
}
Since whole inheritance hierarchy gets instantiated when we create a subclass objects, I was wondering, is it possible to access object of class ClassA in client class?
This is something lot of people get confused. If you create object of subclass, that does not mean it create object of super class.
Its just a call to constructor of super class, just to ensure that all the required fields are initialized in super class, but this does not create object of super class.
This question will help you, understanding the concept.
Check Kevin's answer:
It doesn't create two objects, only one: B.
When inheriting from another class, you must call super() in your constructor. If you don't, the compiler will insert that call for you as you can plainly see.
The superclass constructors are called because otherwise the object would be left in an uninitialized state, possibly unbeknownst to the developer of the subclass.

super() method not giving error when no parent class is defined

class child
{
child()
{
super();
System.out.println("Hello");
}
public static void main(String arg[])
{
child obj=new child();
}
}
In this code when I a create an object of class child , the child constructor is called. But why is it not giving error as there is no parent class. What does the super() do here?
Whose constructor is the super() keyword calling?
In Java every object implicitly extends Object. Calling super here will just call Object's constructor. On another note you should really abide by naming conventions such as capitalizing class names.
It is calling the constructor of the Object class as all object in java extends by default to the Object class.
From documentation:
Class Object is the root of the class hierarchy. Every class has Object as a superclass.
All objects, including arrays, implement the methods of this class.
You're invoking the Object method, from which all other classes descend eventually.
First up, clarifying the class hierarchy in this situation, the inheritance section in the Java tutorial states:
Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.
Then, for the tutorial stuff on using super:
Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.
If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
Every class we create in Java is an implicit descendant of "Object" object (in other words, subclass of Object). Hence when ever you are making a call to super() it implicitly calls the constructor of Object class. The basic reason to have this feature is to generalize the common features like:
synchnronizaton - like wati()
object identity - like hashcode(), equals()
and many more.
Thanks,
JK
The Object class, in the java.lang package, sits at the top of the class hierarchy tree. Every class is a descendant, direct or indirect, of the Object class.
All objects, including arrays, implement the methods of this class. Thats why super() in your case is actually calling the constructor of Object class.

Calling super() with no Superclass?

What happens (if anything) when a constructor calls 'super()' without having any superclass besides Object? Like so:
public class foo implements Serializable, Comparable {
int[] startPoint;
public foo() {
super();
startPoint = {5,9};
}
}
Edit: So if this does nothing, why would anyone explicitly write that in code? Would it make any difference if I just delete that line?
It is always OK to delete the line super(); from any constructor, and there is nothing particular about the constructors of classes that extend Object. The call of the nullary superclass constructor is always implied, so whether you write it down or not, you always get the exact same semantics.
Note that this means that if you omit a call to the superclass constructor that does something big, like start database connections or the whole GUI, all this will happen whether or not you actually write super();.
super() is the first call in constructor either it is done explicitly or implicitly.
(But of course you might need to change parameters to match parent's constructor.)
Your code:
public foo()
{
startPoint = {5,9};
}
What compiler sees from the code above:
public foo()
{
super();
startPoint = {5,9};
}
So super() is invoked whether you put it explicitly in your code or not.
Since all classes derive from Object class, you are calling constructor of Object with your super() call because your class doesn't have any intermediate parents.
There is always a super class called Object, so it will invoke constructor of Object
That just calls the Object() constructor, just as if you had any other superclass with a constructor that has no parameters.
As you stated, there is a super class (Object).
The default object constructor will be called.
It calls Object's constructor, which is empty (does nothing).
super() always call to the constructor to Object class if the class doesn't extends from one class.
Class Object is the root of the class hierarchy. Every class has Object as a super class. All objects, including arrays, implement the methods of this class.
Check this link

How does this object get created?

Many articles say objects get created only after the class's constructor gets called. But I found this snippet and it works fine.
public class A {
public A(){
this.foo();//line #1
}
private void foo() {
System.out.print("without an instance..!!!!");
}
}
class B extends A
{
public static void main(String[] args){
A a = new A(); //line #2
}
}
Here you see, I'm trying to create an object of its super class in line #2 and in its constructor how come its method got called without an instance. What's going on here, is Constructer of instance of A is getting called here.?
The constructor is always called when an object is created. Even if you don't explicitly define a constructor, the compiler will generate one for you with an empty body.
You may call other methods of the class from the constructor. All non-static methods get an implicit (compiler generated) parameter to this, the actual class instance. However, it is important to know that while executing the constructor, the object is not yet fully created, although all data members of the class in question (if there are such) have already been initialized, at least to some default value. Because of this, you
should not publish this (i.e. pass it to other objects / threads) before exiting the constructor call, and
you should not call non-final, non-private methods from the constructor.
Doing either of these (in a non-final class) means that you give access to an object not yet fully constructed, which may result in subtle, hard to find bugs later. E.g. if the virtual method in question is overridden in a subclass and the implementation depends on some member defined and initialized only in the subclass constructor, the method gets called before the subclass member is correctly initialized, thus it won't have the value you would expect.
Because public static void main is the entry point of the programm. So you do not require to create instance of the class B.
The method signature for the main() method contains three modifiers:
* public indicates that the main() method can be called by any object.
* static indicates that the main() method is a class method.
* void indicates that the main() method has no return value.
Read more : Understanding public static void main function
So that Constructor of A is get called when the programm get executed and that it calls the foo method of the super class A.
It's not called without an instance.
You call it on this - that's an instance.
The constructor of A is getting called because you called it directly. However, if you wished to call A through B, within Main [which is called without a current instance of the containing class B (1 because its static, and 2. its reserved for the beginning of the application)] you would just change the "new A()" to "new B()"
Since you're using a constructor with no parameters, a default constructor in automatically generated at compile time.
Whether main() is the entry point or not is not the explanation. The reason is that main() is static, therefore doesn't require an instance of its class.
No instance of B is ever created by this program.
class A is public, and its inherited by class B. class B can instantiate class A, with
A object=new A()
and the object initialization is done by Constructor method defined automatically. Constructor of A in turn calls method foo(), which is private to class A. As far as i know, to call a method from a class which is in the same class scope, no instance is needed.

Categories

Resources