• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Overriding doubt

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can any of you java scholars please help me understand why the compilation fails?

I read in K&B that the overriding method can throw fewer or narrower exceptions than those declared by the overridden method of the superclass. So it should basically run fine since the superclass method doIt() throws an exception whereas the subclass doesn't.

But in reality the code doesn't compile.



Thanks.

 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well you cannot really put two public classes in the same source file
that it where you are stuck

and also
this is really interesting
maybe the experts will be able to solve this
when I change the code

SuperClass sb = new SubClass();


to

SubClass sb = new SubClass();



then it works just fine
experts please help
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi to all,
I agree with prasad and i am also expect the solution.
Cheers Munees
 
Muneeswaran Balasubramanian
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi chow,
If you override the method the compiler doesn't know which method will be executed at the time of compilation.In your code you mention

public int doIt(String str, Integer... data)throws Exception


you throw the exception.
So you must handle or throws the exception where you call this method.
so only it makes compile time error.By the two ways you can run your code successfully,They are

1.Leave the excption as unchecked. by throws the exception in main method like public static void main(String... args)throws Exception
2.Handle the exception using try catch for the method doit("hello",3)



Cheers Munees
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes , you must either declare the Exception or handle it from the place you are calling the
method doIt() bec compiler will check the method declared in the class of the reference type
which is SuperClass here. Which of the methods will actually be called will depend on
the actual object type which is SubClass here.
 
carrie chow
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Kharkar wrote:well you cannot really put two public classes in the same source file
that it where you are stuck


Thanks for replying Prasad. Well the two public classes is just telling a scenario and not from one java source file.
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Carrie,

The reason is this. Just look at your statement


Now, we can see it is a polymorphic statement, but the actual object will be bound at run-time not at compile time.
At compile time, the compiler sees that you are declaring the super-class reference and that super-class has a method
which is throwing a checked exception. In this case you have two solutions, either you handle the checked exception or
throw the checked exception,but in this case as it is the main thread which is called, who else will handle the exception.
That's why compiler is stopping you, that either handle the exception or declare it and let someone else handle it, even
though the actual object is the sub-class object.

If we put the statement as


In this snippet, the compiler knows that you are calling the version which is ovveriden by the sub-class and it doesn't throw
an exception so it will compile and work fine.

Hope this helps,
 
carrie chow
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Kharkar wrote:
when I change the code

SuperClass sb = new SubClass();


to

SubClass sb = new SubClass();



then it works just fine
experts please help



Thanks Munees and Simran, but I am actually now trying to figure out Prasad's doubt.. Why is the subclass object solves everything?
 
carrie chow
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prithvi Sehgal wrote:the compiler knows that you are calling the version which is ovveriden by the sub-class and it doesn't throw
an exception so it will compile and work fine.



Oh thanks a lot Prithvi for sorting this out!

 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You are welcome Carrie.

Happy Preparation,
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic