• 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

overriding/loading

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A bit confused on this bit of code.
class Base{
public Base(int i){}
}
public class over2 extends Base{
public static void main(String arg[]){
over2 m = new over2(20);
over2 n = new over2(" ",10);
}
over2(int i){
super(i);
System.out.println(i);
}
over2(String s, int i){
this(i);
Base b= new Base(30);
System.out.println(i);
}
}
Why is the output of this is:20,10,10.
20 is easily comprehendible, but the others are a bit hazy to me.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you have this(i), you are calling the constructor in the same class with the integer argument. That's why you get two 10 in the output. The first 10 is generated by the over2(int i), the second is self explanetary.
However I want to call you attention when overriding constructor in the subclass. If you comment out the this(i) line, you will get a compiling error. That is because that Java invokes super() implicitly if you do not do so explicitly in the first
line of the subclass's overidden constructor. That's why you see super(i) in the first constructor. However there is one exception. If the first line of a constructor C1, uses the this() syntax to invoke another constructor C2, of the class, Java relies on C2 to invoke the superclass Constructor,
and does not insert a call to super() into C1.Reference. Therefore, it's always recommended to explicitly have your default(no argument)constructor ready in the superclass.

Hope this helps.
Tom
 
sonu shawnee
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a bunch again,
But I am still a bit confused as regards this and super.
Does super(int i) mean that that the subclass integer i is being assinged the superclass integer i, or is it vice-versa. Also what is the significance of assigning using super. How does it matter otherwise.
As fas as "this" is concerned my understanding is that
if you have a class "aclass", which has two constructors
aclass(int i){} and
aclass(int i, int j) {
this(i);
} then this constructor will refer to the int i in the same class. Now what does that mean. Does it mean that when you construct a new object using the second constructor, you don't have to specify the value or something for int i. I am kinda confused on this funda. Also can you help see the significance of having this(i).
Thanks.
 
Tom Tang
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For the sake of simplicity, think of super() as "new Sup()"; this() as "new Sub()" in this program. But don't take this analogy too far, because super and this has other use as well.
For you second question super(a) will certainly use the Sub constructor's argument int a. Don't let the same letter i mislead you.
When you define a class, java guarantees that the class's constructor is called whenever an instance of that class is created(Actually the compiler will call the constructor as soon as you define a class in order to guarantee the above point). It also guarantees that the constructor is called when an instance of any subclass is called. In order to guarantee this second point, Java ensures that every constructor method calls its superclass constructor method. If the first statement in a constructor is not an explicit call to a superclass constructor using the super keyword, then Java implicitly inserts the call super(). This causes a compilation error if the superclass doesn't have a no-argument constructor( Of course, you will have the default constructor, but as soon as you create your own constructor with arguments, you lose the default one).
The use of this(i) is about one exception to the above rule. Please refer to my last post for detailed info.
Tom

 
reply
    Bookmark Topic Watch Topic
  • New Topic