• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can you help me to understand this inheritance code ?  RSS feed

 
samar das
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Output coming is

I am A
I am B
8

We normally create objects by Parent p = new Child()  or by Child c = new Child();

But here it is created by   new B12(5)

Here are we creating an object of type Parent  or  object of type Child

This i can't figure out
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The above line invokes the B12(int) constructor. It immediately calls

which invokes the no-arg constructor if B12. Since that does not explicitly call any super constructor, it calls its superclass's default constructor:

which writes "I am A"
Then the body of B's default constructor is executed, printing "I am B".
then the rest of B's one-argument constructor is executed, writing "5".

I don't understand the reference to "Parent" and "Child", since there is no Parent or Child class in code you show.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
samar das wrote:Here are we creating an object of type Parent or object of type Child
  • Here you are creating an object of child class, in your case an object of B12.
  • It is called anonymous object of B12 class, only difference is that, you are not storing reference of newly created object of B12 in any reference variable.
  •  
    samar das
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Ganish Patil

    Can you elaborate a bit more on anonymous object of B ( or B12 ) class ? I cannot find good examples on Google on this

    What are advantages and disadvantages of creating this ?

    Is   new B12(5)   completely same as  B12 obj = new B12(5) ?
     
    samar das
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The anonymous object is created and dies instantaneously - what is the meaning of this statement ?
     
    Campbell Ritchie
    Marshal
    Posts: 56525
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    samar das wrote:The anonymous object is created and dies instantaneously . . .
    Please always say where such quotes come from, to avoid copyright problems.
    How can an object die? What is happening is that you create an object without giving it a reference and using it. Once that line has completed, the object becomes unreachable and is eligible for GC. Since you are completing the main method at the same time, the garbage collector will probably not run, but the heap memory is returned to the control of the operating system.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is how calls get executed, in pictorial form would be easy to understand.
    Output.png
    [Thumbnail for Output.png]
     
    samar das
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Campbell Ritchie I saw that statement is made by a Quora user
     
    samar das
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Ganish Patil great explanation

    Just one question why Line 10 will not be executed ? ( I am an absolute beginner )
     
    Campbell Ritchie
    Marshal
    Posts: 56525
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    GP's arrow explain everything. The superclass(int) constructor is never called.
     
    Campbell Ritchie
    Marshal
    Posts: 56525
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    samar das wrote:@Campbell Ritchie I saw that statement is made by a Quora user
    Was it here? The post you quoted doesn't look very good. Many people would not use the term anonymous object, and it is not true that anything “dies” instantaneously. In fact an object without a name can have a long lifetime:-There, the MyGUI instance has no reference kept to it, but its lifetime may be hours or weeks, depending on how long you keep it running. You can even have it in existence after the main thread terminates. In this old style (before Java8) version, the Thread and the Runnable associated with it are also objects created with no references, but last after the main Thread exits:-Try it and see it stay running for ages after the Exception. See if you can write crappier code yourself I have a worse version myself.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!