This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why compile error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why compile error" Watch "why compile error" New topic
Author

why compile error

amrit singh
Ranch Hand

Joined: Nov 03, 2000
Posts: 42
class subex extends Exception{subex(String s){super(s);}}
class subsubex extends subex{}
class parent{
public void foo(int i)throws Exception
{if(i>10) throw new Exception();}
}
class child extends parent{
public void foo(int i)throws subex
{
if(i>10) throw new subex("oops");
}
}
George Toronto
Ranch Hand

Joined: Dec 30, 2000
Posts: 78
Originally posted by amrit singh:
[B]class subex extends Exception{subex(String s){super(s);}}
class subsubex extends subex{}

You have to give the explicit constructor of the subsubex since its parent class, subex, has a non-default constructor.please refer to Khalid' Book
regds
George
Kirti Dhingra
Ranch Hand

Joined: Dec 13, 2000
Posts: 32
it proves constructor are not inherited
K Singh
Tom Tang
Ranch Hand

Joined: Dec 24, 2000
Posts: 133
Hi:
As long as I create a non-argument constructor in the subex class, it compiles OK.

My question is since we only throw a new subex(string) object later in the code, why does the compile still try to call the default constructor while there is a nice subex(string s) constructor sitting right there. At the same time, I didn't see the (subclass) subsubex's constructor being called at all. Why do we have to define the default constructor in this case?
George. I don't have the Khalid book with me. So would please explain more?


------------------------<BR>Sun Certified Java Programmer<BR>----------------------------------
vadiraj vd
Ranch Hand

Joined: Dec 15, 2000
Posts: 68
Originally posted by Tom Tang:
[B]Hi:
As long as I create a non-argument constructor in the subex class, it compiles OK.

My question is since we only throw a new subex(string) object later in the code, why does the compile still try to call the default constructor while there is a nice subex(string s) constructor sitting right there. At the same time, I didn't see the (subclass) subsubex's constructor being called at all. Why do we have to define the default constructor in this case?
George. I don't have the Khalid book with me. So would please explain more? [/B]


Hi Tom,
How it is possible for a child to exist without a parent?
Even if you create an object on a subclass superclass's object state is created before any
subclass objects are created.
So the compiler explicitly calls the constructor of superclass while we are creating the object on subclass.
Also whenever we don't provide any constructor for a class, a default constructor
is inserted by the compiler.
Do you know how that might look. (see below)
Class YourClass
{
YourClass() // implicit constructor.
{
super();
}
}
Whenever you create an object on any class, all the constructors
of all the superclass's of your classes are called(since super() is in the first line)
This continues till calling the default constructor of Object class.
And the compiler won't insert the default constructor if you provide
one.
Hope this helps.
Regards
------
vadiraj

------------------
****************
There's a lot of I in J.
****************


Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
George Toronto
Ranch Hand

Joined: Dec 30, 2000
Posts: 78
Originally posted by Tom Tang:
My question is since we only throw a new subex(string) object later in the code, why does the compile still try to call the default constructor while there is a nice subex(string s) constructor sitting right there. At the same time, I didn't see the (subclass) subsubex's constructor being called at all. Why do we have to define the default constructor in this case?

Hi, Tom
First, i recommand strongly you buy a Khalid's book which has deep explaination and cool examples. Secondly, I think you have to confuse the complie error and Runtime error. when compiler check your code structure, it doesnot know what the actual statement you will call or excute. That seems like you code a method in a class, but you donot call it when running your application, but you have code it correctly. Right?
Hope it will help you
regds
George
Tom Tang
Ranch Hand

Joined: Dec 24, 2000
Posts: 133

If I comment out the subsubex class, the code also compiles. George, I think I got your point. Even if I didn't create an object of subsubex in the code, the compiler will still call
its default constructor and therefore subex's default constructor. The compiler doesn't know or care what you will
do with subsubex. It just forces you to write the code correctly.
Therefore, the following code also works:

Thanks
Cristi Tudose
Ranch Hand

Joined: Dec 25, 2000
Posts: 53
George,I think you'd like to say:
You have to give the explicit constructor of the subex(the superclass)...
Anyway,Amrit u may check www.enthuware.com/jqplus
-The Default constructor section
rgds,
Cristi
Originally posted by George Toronto:
You have to give the explicit constructor of the subsubex since its parent class, subex, has a non-default constructor.please refer to Khalid' Book
regds
George

Simon Sun
Greenhorn

Joined: Dec 07, 2000
Posts: 20
Hi,
This discussion is interesting. As I understand the default constructor is that
if the parent class's constructor has arguments, his detrived classes can't use
the default constructor. The detrived classes have to explicitly claim the constructor.
The reason, I guess(probably wrong), is that the compiler only insert, implicitly, super(),
it couldn't insert the corresponding parents' constructor with arguments. Therefore,
your detrieved classes have to claim it.
Am I correct, or miss something?
Thanks
Simon
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why compile error
 
Similar Threads
why compile error
about "Exception" ?
Exceptions (Khalid & Rolf 's book)
what is different between throw and throws?
A little Exception trouble...