From: Javas on
public class Qn24 {

static class A
{
void process() throws Exception { throw new
Exception(); }
}


static class B extends A
{
void process() { System.out.println("B "); }
}


public static void main(String[] args)
{
A a = new B();
a.process();
}



}


Why does the above code results in a compilation error? since it is
always invoking class B's process() method which doesnt throw any
exception.
From: markspace on
Javas wrote:

> static class A
> {
> void process() throws Exception { throw new
....
> A a = new B();
> a.process();

> Why does the above code results in a compilation error? since it is
> always invoking class B's process() method which doesnt throw any
> exception.


Well, I'm not an expert, but Java's type system is often called "static"
by those who are. In other words, Java doesn't look to see what type is
actually assigned to "a". It just sees that a is type A, and that A
could throw an exception. So from a point of view that is strictly
static in type, it seems very reasonable to me that Java says an
exception could be thrown there.

From: Lew on
Javas wrote:
> public class Qn24 {

Say, I'm just taking a WAG here, but this wouldn't happen to be question
number 24 on your homework assignment, would it?

> static class A
> {
> void process() throws Exception { throw new
> Exception(); }

Do us a favor here on Usenet and please keep indentation down to a maximum of
four spaces per level. Eight is too wide for readability in a newsgroup.

> }
>
> static class B extends A
> {
> void process() { System.out.println("B "); }
> }
>
> public static void main(String[] args)
> {
> A a = new B();
> a.process();
> }
> }
>
> Why does the above code results in a compilation error? since it is

When asking questions like this, saying "a compilation error" is waaaaay to
vague. What is the error? *Copy and paste* the *exact* text of the error in
these cases.

> always invoking class B's process() method which doesnt throw any
> exception.

The key is that it's a compilation error, not a runtime one. The fact that
a.process() is polymorphically calling B's version is a runtime consideration,
not a compilation one. See markspace's answer.

It becomes quite important in Java to keep compile-time and run-time realities
separate in your mind.

--
Lew
From: Lew on
Please reply to the list.

Here is what you sent, with attributions:

Javas wrote:
>> > > public class Qn24 {

Lew wrote:
> > Say, I'm just taking a WAG here, but this wouldn't happen to be question
> > number 24 on your homework assignment, would it?

Javas wrote:
>> > > static class A
>> > > {
>> > > void process() throws Exception { throw new
>> > > Exception(); }

Lew wrote:
> > Do us a favor here on Usenet and please keep indentation down to a maximum of
> > four spaces per level. Eight is too wide for readability in a newsgroup.

Javas wrote:
>> > > }
> >
>> > > static class B extends A
>> > > {
>> > > void process() { System.out.println("B "); }
>> > > }
> >
>> > > public static void main(String[] args)
>> > > {
>> > > A a = new B();
>> > > a.process();
>> > > }
>> > > }
> >
>> > > Why does the above code results in a compilation error? since it is

Lew:
> > When asking questions like this, saying "a compilation error" is waaaaay to
> > vague. What is the error? *Copy and paste* the *exact* text of the error in
> > these cases.

Javas wrote:
>> > > always invoking class B's process() method which doesnt throw any
>> > > exception.

Lew:
> > The key is that it's a compilation error, not a runtime one. The fact that
> > a.process() is polymorphically calling B's version is a runtime
consideration,
> > not a compilation one. See markspace's answer.
> >
> > It becomes quite important in Java to keep compile-time and run-time
realities
> > separate in your mind.

Javas wrote:
> Guruji! Don't try to act smart. Just try to share your views and
> convey your requests in a cordial mannar. Hope you understand your
> mistakes. I'll correct me from my next post onwards. Anyways, Thanks
> for your answer.

#1: If you're so smart, why are you asking us for help?

#2: What wasn't cordial about my manner?

#3: What mistakes? Trying to help you, apparently.

--
Lew
From: Javas on
On Mar 10, 9:31 am, Lew <no...(a)lewscanon.com> wrote:
> Please reply to the list.
>
> Here is what you sent, with attributions:
>
> Javas wrote:
>
>  >> > > public class Qn24 {
>
> Lew wrote:
>
>  > > Say, I'm just taking a WAG here, but this wouldn't happen to be question
>  > > number 24 on your homework assignment, would it?
>
> Javas wrote:
>
>  >> > >         static class A
>  >> > >         {
>  >> > >                 void process() throws Exception { throw new
>  >> > > Exception(); }
>
> Lew wrote:
>
>  > > Do us a favor here on Usenet and please keep indentation down to a maximum of
>  > > four spaces per level.  Eight is too wide for readability in a newsgroup.
>
> Javas wrote:
>
>  >> > >         }
>  > >
>  >> > >         static class B extends A
>  >> > >         {
>  >> > >                 void process() { System.out.println("B "); }
>  >> > >         }
>  > >
>  >> > >         public static void main(String[] args)
>  >> > >         {
>  >> > >                 A a = new B();
>  >> > >                 a.process();
>  >> > >         }
>  >> > > }
>  > >
>  >> > > Why does the above code results in a compilation error? since it is
>
> Lew:
>  > > When asking questions like this, saying "a compilation error" is waaaaay to
>  > > vague.  What is the error?  *Copy and paste* the *exact* text of the error in
>  > > these cases.
>
> Javas wrote:
>
>  >> > > always invoking class B's process() method which doesnt throw any
>  >> > > exception.
>
> Lew:
>  > > The key is that it's a compilation error, not a runtime one.  The fact that
>  > > a.process() is polymorphically calling B's version is a runtime
> consideration,
>  > > not a compilation one.  See markspace's answer.
>  > >
>  > > It becomes quite important in Java to keep compile-time and run-time
> realities
>  > > separate in your mind.
>
> Javas wrote:
> > Guruji! Don't try to act smart. Just try to share your views and
> > convey your requests in a cordial mannar. Hope you understand your
> > mistakes. I'll correct me from my next post onwards. Anyways, Thanks
> > for your answer.
>
> #1:  If you're so smart, why are you asking us for help?

I never advertised to you that I am smarter than
you. I guess you are trying to get personal.

> #2:  What wasn't cordial about my manner?

You could have just answered the questions and
stopped there, instead of worrying about whether's it my homework or
assignment work. Or atleast you could have suggested me to name the
class properly. But you chose the wrong path.

> #3:  What mistakes?  Trying to help you, apparently.

You had some other intentions too other than
helping one alone.

Above all, I just wanted to convey it to you.
That's the reason why I sent it to you alone.
> --
> Lew
 |  Next  |  Last
Pages: 1 2 3 4 5
Prev: Hashcode and Equal
Next: Speaking of thread safety?