• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Object Creation

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Mahesh Bamane
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Winston, sure you can do that. Thanks.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Done.

Winston
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Mahesh Bamane
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic