• 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

SCJP certification

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well,in java we use super(); in the first line of the subclass constructer to call the super class constructer, but if we do not add it why does compiler adds it implicitly.
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the features of Java. If you do not specify any constructor calls in the constructor, the compiler will call super() by default. If you specify any constructor to call, say super(i), then no other constructors will be called implicit.

Thus, this causes compilation error:

This is because there is NO default constructor A() to be called when B() invoke super(). You must explicit invoke the super constructor by:
public B() { super (100); }

Hope this help.

Nick
 
Amitkumar Dhama
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that this is the feature of java, but i wanna know that what is the need, I dont want the answer like 2 call the super class constructer, what is the need of super class con.?Is there any concept of inheritence.please let me know...............
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my view of what you are talking about:

A class Sub inherits from another class Super

=====>
every object of class Sub IS A object of class Super

=====>
to "build" somethig that IS A Sub, you need to execute
a process of contruction of Super, because every Sub IS A Super
( process of construction = one of the constructors )

======>
every constructor of a class must pass through (at least)
one constructor of his superclass


Think:
only Super class can define/know how to build an object of class Super,
and if you, Sub class, "pretends" to be a Super ( declaring to extend Super ), when you build yoursef you must "walk through" one of my constructors!
The part of you, Sub class, that IS A Super must be build by someone
who knows how to do it.
( example: suppose your superclass, from which you inherit, has private "things". Being private, you cannot access them from a subclass...
who will initialize private things? if a constructor of the superclass is not called ? you will get objects with a corrupted state having part of them not correctly constructed)


Hope this is right...and will help.....

Massimo
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have seen the recursive way to calculate n factorial, you will recognize how constructors are organized.

Let's say:
When you say "C y=new C();", you are asking the jvm to build a composite object consisting of an object of type Object, an object of type A, an object of type B, and an object of type C.

Even if A has private fields that are invisible to methods in class C, those fields are part of the composite object and must be initialized. But a private field in A can only be initialized by an A constructor. So every constructor from Object to C must be called.

Now the process starts by new calling the C constructor. How do we get the other constructors called? We require a C constructor to call a B constructor, a B constructor to call an A constructor, and an A constructor to call an Object constructor.

But that is the wrong order! We need to create objects from the top down, first Object, then A, then B, and finally C. Why? Because a C object cannot exist without a B object to inherit from but a B object can exist without a C object.

So Java requires that the very first statement of every constructor be a call to either another constructor of the same class (this();) or a call to a constructor of the parent class (super();. If the first statement is neither one, Java inserts a super(); statement for you.

Now we get to the recursive-like part. When the C constructor is called, it calls a B constructor immediately. The B constructor calls an A constructor before doing any work, and the A constructor calls the Object constructor. When the Object constructor completes its work of initializing an Object, it returns control to the second line of the A constructor. The A constructor can now initialize an object consisting of a fully initialized Object and a partly initialized A. When the A constructor finishes initializing the A object, including any private fields, it returns control to the second line of the B constructor, which finishes initializing the B object, and so on. This way, the composite Object/A/B/C object is initialized in the correct order.

Cool, huh?
[ February 17, 2005: Message edited by: Mike Gershman ]
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please consider using a meaningful subject line.
reply
    Bookmark Topic Watch Topic
  • New Topic