• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java In General

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if superclass' constructor is ALWAYS called before the constructor of subclass then what is use of super()
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic