aspose file tools*
The moose likes Beginning Java and the fly likes inheritence related questions in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inheritence related questions in Java" Watch "inheritence related questions in Java" New topic
Author

inheritence related questions in Java

Jayaraman Suresh Babu
Greenhorn

Joined: Jun 15, 2006
Posts: 11
Hi,

I just was reading SunJava tutorial for refreshing and a few questions came to my mind.

Before I post my questions, let me give a you an excerpt from the Sun Java tutorial (from their website) on which my questions are based upon. In the Language basics chapter, Sun says that every class implicitly extends the Object class (i.e. Object is the superclass of everyclass) - agreed. Also, the tutorial says that in general when one class extends a parent class, if the subclass does not provide a default constructor, when instantiating an object of the subclass, the compiler will check and use the default no-arg constructor of its superclass. They have also mentioned that assuming one does not define a default constructor in the subclass, the compiler will verify and complain if its superclass also does not have default constructor.

Now here are my questions:

1. I defined two simple classes and inherited one class from the other. First I declared a default constructor in the parent class and no constructor in the subclass. When I instantiated a sub-class object, as the tutorial said, the compiler printed out the statement inside parent classes's no-arg constructor. However, the second time I removed the constructor from the superclass as well and compiler. This time, contrary to what the tutorial says, the compiler did not complain and the program ran fine. So is the tutorial wrong here? In the second case, is the compiler calling the constructor of the Object class?

2. I know that a subclass can I have only one superclass i.e. I cannot extend more than one class. However, every class extends Object.class as well. This means that even if I have my own subclass extending my own parent class, since implicitly every class in Java also extends Object, does this contradict what the restriction of inheriting only one parent class?

Sorry if these questions are stupid. All of your insights are appreciated.

Thanks,
Jayaram
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

i can answer 2 quite easily. each class can only DIRECTLY derive from one other. but you can chain them as far up or down as necesary. So, if your class B derives from your class A, your class A then derives from Object. So, B is a (grand)child of Object. if you make a Class C derive from B, then it is a child of B, a (grand)child of A, and a (great-grand)child of Object.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jayaraman Suresh Babu
Greenhorn

Joined: Jun 15, 2006
Posts: 11
Hi Fred,

That really cleared things up. Thank you very much.

- Jayaraman
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
First let's get the terminology straight: a no-arg constructor is a constructor that doesn't get any arguments. The default constructor is the no-arg constructor the compiler generates if you don't provide any constructor manually. With other words, you can't write a default constructor, only a no-arg constructor. (Some older Sun articles may use a different terminology, though - they later decided to introduce some consistency...)

Originally posted by Jayaraman Suresh Babu:
Also, the tutorial says that in general when one class extends a parent class, if the subclass does not provide a default constructor, when instantiating an object of the subclass, the compiler will check and use the default no-arg constructor of its superclass.


This sounds a little bit misleading, as the compiler will generate a default constructor for the subclass, which in turn will call the no-arg constructor of the superclass. The latter doesn't have to be a default constructor.


They have also mentioned that assuming one does not define a default constructor in the subclass, the compiler will verify and complain if its superclass also does not have default constructor.


This sounds outright wrong. Are you sure you didn't confuse something here?

1. I defined two simple classes and inherited one class from the other. First I declared a default constructor in the parent class and no constructor in the subclass. When I instantiated a sub-class object, as the tutorial said, the compiler printed out the statement inside parent classes's no-arg constructor. However, the second time I removed the constructor from the superclass as well and compiler. This time, contrary to what the tutorial says, the compiler did not complain and the program ran fine. So is the tutorial wrong here? In the second case, is the compiler calling the constructor of the Object class?


What happens is that the compiler generates a default constructor for the subclass, which calls the generated default constructor for the parent class, which in turn calls the no-arg constructor of Object.

Whether the tutorial is wrong, or you simply misunderstood it, I can't tell...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

First Jayaram, try to understand one thing clearly.

Whenever you are creating a class, the compiler puts its default construcror, if you fail to provide one.

you can provide a constructor for your class and that time it will be called as n-arg constructor (based on the argument).

IMO, whatever the compiler is providing is called as default constructor.

Regarding your second question, a class can extend a parent class and this process can be go for any long. for example a class can have parent class, grand-parent class, great grand-parent etc..

I hope now you are clear


Thanks, Shiv
SCJP, OCE - JSP & Servlets Developer
Jayaraman Suresh Babu
Greenhorn

Joined: Jun 15, 2006
Posts: 11
Thank you Guys for your response
Jayaraman Suresh Babu
Greenhorn

Joined: Jun 15, 2006
Posts: 11
Ilga,

I agree with your first point. The compiler will call the default constructor which in turn will call the no-arg constructor of super-class. Howver look at the last line below. Here is the excerpt copied from Sun's Tutorial Page:

---------------
You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does
---------------------------

Please read the last line. Even if the super-class's no-arg constructor was missing, the compiler did not complain.
Jayaraman Suresh Babu
Greenhorn

Joined: Jun 15, 2006
Posts: 11
Ilga,

But I think I understand why: probably it did not complain because it would have used the no-arg constructor of the Object class, the parent of my super class.

Thanks.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Jayaraman Suresh Babu:
But I think I understand why: probably it did not complain because it would have used the no-arg constructor of the Object class, the parent of my super class.


No, this can't happen. A constructor in the superclass must be called. If you don't call one explicity in the first line of your constructor, it will implicitly call the no-arg constructor in the superclass.

Since the compiler isn't complaining, one of these must be true:
  • You are calling an explicitly-defined no-arg constructor in the superclass.
  • You are calling the implicit no-arg constructor created for the super class because it has no explicit constructors.
  • You are calling a constructor in the super class that takes one or more args.


  • Note that AFAIK the term explicit has no formal meaning in this context. By explicit I mean a constructor that you can see when you look at the source code.
    [ January 12, 2007: Message edited by: Brian Cole ]

    bitguru blog
    Jayaraman Suresh Babu
    Greenhorn

    Joined: Jun 15, 2006
    Posts: 11
    Hi Brian,

    Here are my answers to your questions:

  • You are calling an explicitly-defined no-arg constructor in the superclass. - No, I did not define any constructor in my subclass or superclass
  • You are calling the implicit no-arg constructor created for the super class because it has no explicit constructors.
  • You are calling a constructor in the super class that takes one or more args. - No, I did not define any constructor in my subclass or superclass


  • So then the second bullet ought to be happening. However, when you say it is taking the "implicit no-arg, constructor (default??) of super class" where is this coming from? If I am understanding right, its from the parent above it (which is Object? correct?), i.e. the grand father of my sub-class right?

    Sorry If I am not getting this. But your answer is making me think again

    Thanks
    Anton Uwe
    Ranch Hand

    Joined: Jan 10, 2007
    Posts: 122
    If you write

    the compiler will "change your code" to
    Brian Cole
    Author
    Ranch Hand

    Joined: Sep 20, 2005
    Posts: 862
    Originally posted by Jayaraman Suresh Babu:
    So then the second bullet ought to be happening. However, when you say it is taking the "implicit no-arg, constructor (default??) of super class" where is this coming from?


    Every class you write has at least one constructor. Either it has one or more explicit ones that you wrote yourself, or else it has a default one that the compiler creates for you when it compiles your class.

    If you write

    it compiles the same as if you had written


    If I am understanding right, its from the parent above it (which is Object? correct?), i.e. the grand father of my sub-class right?


    No. The grandfather's constructor is out of the picture from the grandchild's point of view.

    Presuming the father is not Object, the grandfather's constructor will be invoked, but only indirectly because the father must call it.

    [edit: Anton Uwe beat me to the punch while I was composing this, but at least he and I agree. His code calls super() explicity, while mine leaves it implicit.]
    [ January 12, 2007: Message edited by: Brian Cole ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: inheritence related questions in Java