• 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

no argument constructor

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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}
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic