• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overriding

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we have read that the overriding method can throw none or as many exception as the base class but this code does not give me error why???
import java.io.*;
public class base1{
private float f1;
public void base1() throws IOException{
System.out.println("hello from base");
}
public static void main(String args[])
{
}
}
class child extends base{
public static void main(String args[])
{}
public void base1() throws IOException,NumberFormatException
{
System.out.println("hello child");
}
}
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pooja agrawal:
Because NumberFormatException is a RuntimeException (UnChecked Exception - So It is not a must to catch in the programme).which can be put in the the Overridding method even that is not throw in the Overriden method.

Siva.
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because you are allowed to trow one of it's sublcasses as well.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

overriding method can throw none or same or subclass of checked exceptions as of overriden method.NumberFormatException is Runtime exception and there is no restriction for that.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi evbody
in Pooja's example we are overriding tha main method arn't we.
and i think static methods can not be overridden.
then why no error???
please explain.
thanx
shilpa
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shilpa,
static methods cannot be overridden. Here we are just hiding the static method in the superclass with a static method in the subclass. Same is the case with private methods. The methods in both the classes are independent of one another.
HTH.

------------------

***********************************************
Sun Certified Programmer for Java 2 Platform
***********************************************
 
pooja agrawal
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry none of the answers are satisfying because though numberformat is a runtime exception the code is not giving me errors even on changing the error type to arrayindexoutofbounds or interrupted exception please can anybody give me a proper explaination for that
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by pooja agrawal:
sorry none of the answers are satisfying because though numberformat is a runtime exception the code is not giving me errors even on changing the error type to arrayindexoutofbounds or interrupted exception please can anybody give me a proper explaination for that


The answers are perfectly fine. An IndexOutOfBoundsException is a RuntimeException, as a glance in the javadoc would have shown you. An InterruptedException causes a compilation error, as expected.
- Peter
 
pooja agrawal
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks peter. and rest of u for the reply i'm now clear wit hmy concepts of overriding
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic