aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes no argument  constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "no argument  constructor" Watch "no argument  constructor" New topic
Author

no argument constructor

naraharirao mocherla
Ranch Hand

Joined: Aug 16, 2005
Posts: 45
Hi guys

Here I've a question on inheritance...

I'm getting the output as

No argument constructor
Base
Sub

Eventhough I've specified the Base() (No arg constructor);
Can anyone explain what's happening here..
why am I not getting the output as "Baseclass".


Thank you in advance...


[/CODE]
class Base implements I1 {
Base(){
System.out.println("Baseclass");
}


Base(int j){

System.out.println("Base class's j is"+j);

}
}

class Sub extends Base implements I2 {


Sub(){

System.out.println("Subclass");
}

Sub(int i){
//super();

System.out.println("Sub class's i"+i);
}
}
public class Gray {

public static void main(String []args) {
Base b1 =new Base(2);
Sub s1= new Sub(3);

[CODE}
Ryan Kade
Ranch Hand

Joined: Aug 16, 2005
Posts: 69
naraharirao,

Your code is incomplete (and your [code] tags are messed up as well), so it's a little hard to tell what's supposed to happen. I'm not clear on your question, either. As near as I can tell, the order of the constructor calls should be as follows:

Base b1=newBase(2) will call Base(int)
Sub s1=newSub(3) will call Base() first, and then Sub(int)

So your output should be similar to:

Base class's j is 2
Baseclass
Sub class's i is 3
naraharirao mocherla
Ranch Hand

Joined: Aug 16, 2005
Posts: 45
Hi Ryan
thanks for ur reply.

I was expecting the same result as u gave in the reply...
but i'm getting it as

the output is ...
No argument constructor
Base
Sub



naraharirao mocherla
Ranch Hand

Joined: Aug 16, 2005
Posts: 45
HI Ryan

If i remove the numbers 2 and 3 from base class constructor and Subclass constructor respectively..

like

Base base= new Base();
Sub sub=new Sub();

//The output is

Base
Base
Subclass


Base base= new base(2);
Sub sub = new Sub(3);
//The output is

No argument constructor
Base
Sub

Eventhough I provided the Base class default constructor it is not printing out the print stmt I have provided in the Base class constructor
it is just giving the output as "Base".(instead of "Baseclass").Why???

Thanx in advance
Ryan Kade
Ranch Hand

Joined: Aug 16, 2005
Posts: 69
Hmmm, maybe someone else can help out here. I'm not having any problems with your code (I do have to eliminate the two "implements" statements since I don't have those interfaces). Is there pertinent code you've eliminated from your example?

If you have a call (explicit or implicit) to a non-existant no-arg constructor (that is, neither you nor the compiler provided one), then you should get a compile-time error. The fact that you're not suggests to me that there's more than meets the eye to this code.
raghu babu
Ranch Hand

Joined: Jul 05, 2005
Posts: 60
I'm having a hardtime to understand the code you mentioned and the pertinent answers you are expecting. Only if you could repost your code with your doubs, would be of any help.
naraharirao mocherla
Ranch Hand

Joined: Aug 16, 2005
Posts: 45
Hello Vneeth
here is my code.Sorry I'm troubling you guys a lot.(with bad indentation)..
This is my own code (just modified DanChisholm's question)



For this code I'm getting the output as

No argument constructor
Base
Subclass's i is 3 // fine

// If i remove the int values 2 an 3 from the Base class and Sub constructors.
like this...
Base base= new Base();
Sub sub= new Sub();

Then I'm getting the output as

Base
Base
Subclass

Why The Base class constructor(default constructor) is not getting
invoked???
Ryan Kade
Ranch Hand

Joined: Aug 16, 2005
Posts: 69
naraharirao,

I tried your code again (I had to add a few closing braces to make it compile) and had no problem at all. Are you sure you don't have conflicting class names or something like that? Maybe the Base class you're actually instantiating is defined in a different location (I don't how that could happen w/o a compile error, but I'm just throwing it out there). Also, although I don't really think this has anything to do with it, what version of the JDK are you using?

Lastly, what is the original Dan Chisholm question, unmodified? Maybe if we see how you're modifying it we could help identify the problem.
 
 
subject: no argument constructor