aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Java Object Creation Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Java Object Creation" Watch "Java Object Creation" New topic
Author

Java Object Creation

Mahesh Bamane
Ranch Hand

Joined: Mar 12, 2008
Posts: 66
Let's say I've Class A, Class B and Class C.

Now C->extends B -> Extends A

Now I write C c = new C();

Now how many objects will be created over here?

There is nothing like trying, either you do it or you don't.
SCJP 1.5
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
Mahesh Bamane wrote:Let's say I've Class A, Class B and Class C.

Now C->extends B -> Extends A

Now I write C c = new C();

Now how many objects will be created over here?


Only one. Class C's object...
Mahesh Bamane
Ranch Hand

Joined: Mar 12, 2008
Posts: 66
Thanks for your reply Gaurang, one more doubt to add here. Here a call to super constructor will also go right? Whats the use of calling super? Just wanted to understand what happens underneath? Why there is need of calling super while creating an object?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32833
    
    4
Have a look at the Java Language Specification, which explains the order in which objects are created. You see you must call a superclass’ constructor. The super(); or super(x, y, z); calls tell the compiler which superclass’ constructor to call.
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
Mahesh Bamane wrote:Here a call to super constructor will also go right?

Yes

Mahesh Bamane wrote:
Whats the use of calling super?

super keyword is used to access Superclass variables,methods and also constructor(But only from Subclass' constructor)....

Mahesh Bamane wrote:
Just wanted to understand what happens underneath? Why there is need of calling super while creating an object?

Just to initialize a variables(will be inherited by subclass from Super class)

For More Information:
Campbell Ritchie wrote:Have a look at the Java Language Specification, which explains the order in which objects are created. You see you must call a superclass’ constructor. The super(); or super(x, y, z); calls tell the compiler which superclass’ constructor to call.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 4905
    
    7

Mahesh Bamane wrote:Now how many objects will be created over here?

This sounds suspiciously like an SCJP question (or OCJP now, I presume). Would you like me to move it to the relevant forum? You may get a better response.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Mahesh Bamane
Ranch Hand

Joined: Mar 12, 2008
Posts: 66
Hi Winston, sure you can do that. Thanks.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 4905
    
    7

Mahesh Bamane wrote:Hi Winston, sure you can do that. Thanks.

Done.

Winston
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 895

only one object will be created that of C class. when main method is invoked by jvm first of all class initialization takes place. class initialization means all the static initializers of the class are executed. static initializers are for example static int a = 6 , it also included static initialization blocks. also before the class is initialized its superclass is initialized . after the class is initialized and when C c = new C() is encountered , the no-arg constructor of C class is executed which will execute the superclass constructor.

Also from the JLS

A class or interface type T will be initialized immediately before the first
occurrence of any one of the following:
• T is a class and an instance of T is created.
• T is a class and a static method declared by T is invoked.
• A static field declared by T is assigned.
• A static field declared by T is used and the field is not a constant variable

OCPJP 6(100 %) OCEWCD 6(91 %)
Mahesh Bamane
Ranch Hand

Joined: Mar 12, 2008
Posts: 66
Thanks for your reply.

If only one object is created then why super is called? (might be a stupid doubt)
I mean doesn't super() creates super class object? What is the benefit of calling super()?
If I put S.O.P in class A, B anc C's countructor and create on C's object i.e. C c = new C(), all three are printed. Doesn't that mean 3 objects are created?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 895

Mahesh Bamane wrote:Thanks for your reply.

If only one object is created then why super is called? (might be a stupid doubt)
I mean doesn't super() creates super class object? What is the benefit of calling super()?
If I put S.O.P in class A, B anc C's countructor and create on C's object i.e. C c = new C(), all three are printed. Doesn't that mean 3 objects are created?



super does not create superclass objects. only one object is created of C class. when an object of subclass is created then that object IS -A type of superclass. for example Dog is an Animal. so when Dog object is created all the animal parts in Dog would have to be initialized to make it a complete Dog object. same rule applies in your question.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Mahesh Bamane wrote:
If only one object is created then why super is called?

do you see the link what Campbell Ritchie given to you?
From JLS:
Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden


So, only one object is created. that is C and this contains B's values as well as A's values. and calling super class constructor initialize the super class instance variables so that C object can hold the values in it.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Java Object Creation
 
Similar Threads
constructor called?
method conversion question
Casting question
Java Multiple inheritance
Javaranch Tips/Tricks question