aspose file tools*
The moose likes Java in General and the fly likes Why does a call from a child to a method in parent class,have to go through the parent constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why does a call from a child to a method in parent class,have to go through the parent constructor" Watch "Why does a call from a child to a method in parent class,have to go through the parent constructor" New topic
Author

Why does a call from a child to a method in parent class,have to go through the parent constructor

Ashish Jha
Greenhorn

Joined: Jul 04, 2013
Posts: 4
I am new to java..
please help me understand the steps that follow once i call the method of the parent class.. why does the call have to go through the constructor first in some cases?
in constructor of class B, I tried different ways and the variation of output, I am not able to understand..

here's the code:




Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

Correct. If you don't call the superclass constructor in a subclass constructor, Java adds it for you automatically. The reason is that a child class can't be created unless the parent class exists.

I'm not clear on exactly what you are trying to do. Why do you need to call a method without the constructor being called?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ashish Jha
Greenhorn

Joined: Jul 04, 2013
Posts: 4
no, i was just trying to see the program flow in different situations.. how control flows when making a call in different ways..
thanks for reply Jeanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does a call from a child to a method in parent class,have to go through the parent constructor