• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java In General

 
parnini gavande
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A
{
A()
{
System.out.println("Inside A");
}
}
public class B extends A
{
B()
{
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
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Bosun
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
raimondas zemaitis
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if superclass' constructor is ALWAYS called before the constructor of subclass then what is use of super()
 
Marilyn de Queiroz
Sheriff
Posts: 9061
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sanjays samadder
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic