Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes getting access to grandfather's constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "getting access to grandfather Watch "getting access to grandfather New topic
Author

getting access to grandfather's constructor

Shay Gaghe
Ranch Hand

Joined: Sep 03, 2001
Posts: 102
Hi
I have a class named Father the extends Grandfather , inside this class Father I have an inner class named Son, how can I get access the Grandfather constructor from the Son class?(code please)
Thanks
Shay Gaghe
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

Originally posted by Shay Gaghe:
I have a class named Father the extends Grandfather , inside this class Father I have an inner class named Son, how can I get access the Grandfather constructor from the Son class?(code please)

This is the code for constructor calling from Son. However, if Father extends GrandFather and Son is a non-static inner class, its creation by default will call all the constructors from top order; GrandFather, Father and then Son. Is this what you want?

HTH,
- Manish
Shay Gaghe
Ranch Hand

Joined: Sep 03, 2001
Posts: 102
Hi
In my particular case I have class named A that extends Jlabel, and in the A I have another class named B that extend Jlabel as well. in order to perform image displaying from B I meant to call (by super(image) to the Jlabel constructor but I cant because A is the superclass, how can I do such a performance calling the Jlabel constructor(which is the A superclass)?
(I don�t know why its not calling the B superclass which is jlabel as well, I try it but I receive compiling error)
Thanks
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

Originally posted by Shay Gaghe:
In my particular case I have class named A that extends Jlabel, and in the A I have another class named B that extend Jlabel as well. in order to perform image displaying from B I meant to call (by super(image) to the Jlabel constructor but I cant because A is the superclass, how can I do such a performance calling the Jlabel constructor(which is the A superclass)?
(I don�t know why its not calling the B superclass which is jlabel as well, I try it but I receive compiling error)

OK.
In this case you need to ensure 2 things
1) Class A has default, no parameter constructor, else call its parameterized constructor appropriately.
2) While calling super(Icon image) in the inner class constructor, image shouldn't be an instance variable of the inner class B, or its outer class A.
HTH,
- Manish

[This message has been edited by Manish Hatwalne (edited October 15, 2001).]
Shay Gaghe
Ranch Hand

Joined: Sep 03, 2001
Posts: 102
Hi Manish
itried to run this code and i got the following error:

errors:
"Father.java": Error #: 300 : variable Father not found in class Father.Son at line 18, column 7
"Father.java": Error #: 479 : illegal qualifier; class java.lang.Object is not an inner class at line 18, column 14

thanks
Shay Gaghe
Ranch Hand

Joined: Sep 03, 2001
Posts: 102
Hi , look in this please:

thanks
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Shay Gaghe:
I have a class named Father the extends Grandfather , inside this class Father I have an inner class named Son, how can I get access the Grandfather constructor from the Son class?(code please)
You can't, except by creating a new Grandfather object. There is no way to directly call a constructor, with one exception: in the very first line of a constructor, you can call a different constructor in the same class by calling this() or the superclass constructor by calling super().
- Peter
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

The first code you had posted (Father - Son) compiled on machine w/o any problem. (win 2k, JDK 1.3)
As for your second code snippet, since outer class extends JPanel and inner class extends JLabel, it shouldn't be a problem. Like Peter said you'll neeed to call it in the constructor as the very first line, with a value which is not an instance variable of that class.
Here is a code snippet I have tried on my machine which compiles w/o a problem. Of course, you'll need to make sure that Icon img in the outer class holds a proper value.

Let us know if it helps,
- Manish
[This message has been edited by Manish Hatwalne (edited October 15, 2001).]
 
Consider Paul's rocket mass heater.
 
subject: getting access to grandfather's constructor
 
Similar Threads
Subclass Vs Superclass
super keyword
JDBC and use of SQL CONNECT BY and PRIOR for hierachy (son,father)
extend from abstract class: son still has father's data members?
Can someone explain the error @ Line-03