• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Polymorphism Question

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



output:
super
sub display
Integer
sub


how in the above code the A class constructor gets called ?
the first statment of constructor should be call to this or super,if neither of them are explicitly put the compiler implictly calls super()
so,there should be no call to super as in the above code
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohitkumar gupta wrote:the first statment of constructor should be call to this or super,if neither of them are explicitly put the compiler implictly calls super()


The B() constructor calls B(int). Since that doesn't have a this or super call in it, then as you've just quoted above the compiler implicitly calls super().

A super-class constructor will always be called, one way or another.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohitkumar gupta wrote:

how in the above code the A class constructor gets called ?
the first statment of constructor should be call to this or super,if neither of them are explicitly put the compiler implictly calls super()
so,there should be no call to super as in the above code


What about int - argument constructor?

BTW, Your topic and question seems to be different? And have look on the disp() method of your sub class~! If both are doing same operation, then you can't identify which one get called!~ Change it, and then check it.
 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just read the quote you posted carefully and you should find a solution :-)

By the way, I think you have a mistake in your code. The superclass method disp() should print something like "super display". Then you will see that the constructor of A class is also aware of the polimorphism and will call the B.disp();


Edit: Gosh, Abimaran you were 7 seconds faster :-)

Cheers!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One advise: Don't call the overridden methods within Constructors. But, you can predict which one will be called! Can you?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pedro Kowalski wrote:
Edit: Gosh, Abimaran you were 7 seconds faster :-)
Cheers!



Not that all!
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For compiler this

is equal to this

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for better understanding

 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic