my dog learned polymorphism
The moose likes Beginning Java and the fly likes Java In General Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java In General" Watch "Java In General" New topic

Java In General

parnini gavande

Joined: Mar 31, 2001
Posts: 5
class A
System.out.println("Inside A");
public class B extends A
System.out.println("Inside B ");
public static void main(String args[])
B b=new B();
//In above program after running output obtained is :
Inside A
Inside B
My Question is, if I am calling Constructor of only B how it is calling
Constructor of A also?
Even if I add parameter in one of Constructors to identify them separately ,even then I am getting the same output. How?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
If you don't insert a call to the superclass constructor in your constructor, the JVM implicitly inserts a call toe the no-arg constructor of the superclass.


So much trouble in the world -- Bob Marley
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Remember B "is an" A. In order for it to BE an A you need to construct an A part of B then the B part.

"JavaRanch, where the deer and the Certified play" - David O'Meara
raimondas zemaitis
Ranch Hand

Joined: Feb 23, 2001
Posts: 104
Here is in plain English: superclass' constructor is ALWAYS called when class is instantiated. It does not depend what constructors you define in your class. More to that, constructors of ALL SUPERCLASSES uphierarchy are called. So, there's at least constructor of Object called each time you instantiate class.
More to that, superclass' constructor is ALWAYS called before the constructor of the class being instantiated (simplified) is executed fully.
parnini gavande

Joined: Mar 31, 2001
Posts: 5
if superclass' constructor is ALWAYS called before the constructor of subclass then what is use of super()
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9056
If you do not specify super( args ), you will call the superclass' no arg constructor. If that is what you want, you do not need to specify super(). But you need to be sure that the superclass has a no arg constuctor that can be called.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
sanjays samadder

Joined: Mar 30, 2001
Posts: 24
follow these rules:
the sub class calls the super class constructor ,the default constructor ,each time you extend the sub class.
if you declare even one constructor java does not make any default constructor.
if the sub class constructor does not call the super();as the first statement in that constructor it searches the default constructor which is not there .compiling such a class will give an error :cannot resolve symbol.
to avoid this declare the default constructor .(if you do not call the super ()
if you do so then you need not define the default constructor.
if you have a multilevel hierarchy the chain folllows the same rule.
if you have multiple constructors(overloaded) in the subclasses then in each of them you have to call at least one form of the super class constructor or else declare the default constructor.
hope i ma right.
I agree. Here's the link:
subject: Java In General
It's not a secret anymore!