Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

OverLoading

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Animal {
public void eat() throws Exception
{

}
}
class Dog2 extends Animal {
public void eat() { }
public static void main(String [] args)
{
Animal a = new Dog2();
Dog2 d = new Dog2();
d.eat();
a.eat();
}
}
can anyone explain me why is it giving Compiler error
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Here eat() method is overrideen by class Dog2.

Hence according to the overriding rule which version of the method(Animal or Dog2) will be called will de determined at run time depending on the object type and not at compile time.

If the supertype has declared a checked exception the compiler will think that supertype's version is called( in this case Animal's eat method) hence this exception should be caugth.

The following code will compile fine:

class Animal {
public void eat() throws Exception
{

}
}
class Dog2 extends Animal {
public void eat(){ }
public static void main(String [] args)
{
Animal a = new Dog2();
Dog2 d = new Dog2();
d.eat();
try
{
a.eat();
}
catch(Exception e){}
}
}


So even at run time Dog2's eat() version is called at compile time the compiler thinks it is calling Animal's version.

Remember which overridden method is called is determined at runtime and not compile time.

regards

Shiva
 
sravanthi pulukuri
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What i thought is the overriding method sholdnt declare any checked exceptions of its owns
So DOG2 s method sholdnt give any error but why is it giving?is that we are using polymorphic assigment?
Please corerct me if iam wrong
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error is not caused by Dog2's method.

The error is caused by the line

 
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic