Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Overriding in interface  RSS feed

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I have athis piece of code :
class Mammal {
Mammal() throws ColdBloodedException{}
void GiveBirth() {}
}
interface CanFly {
void fly();
}
class Bat extends Mammal implements CanFly {
Bat() throws NoWingsException {}
void GiveBirth() {...}
void fly() {...}
}
when I try to compile , the compiler complaint regarding the fly() method can't be overridiong to be more restrictive. When I put the "public" access will fix it.
Can someone explaint me this ?
Thanks
Ricardo
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All interface members are implicitly public. When you override the fly method using the default modifier, you are making the access more private, which is not allowed.
 
Ricardo Polero
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thaks a lot !
Ricardo
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ricardo Polero:
Thaks a lot !
Ricardo


I agree with what agella says . Well, I tried working out this code. Please tell me how to fix the error that i get on running this code. I worked it out like this.
class Mammal
{
Mammal() throws ColdBloodedException{}
void GiveBirth() {}
}
interface CanFly
{
void fly();
}
class Bat extends Mammal implements CanFly
{
Bat() throws NoWingsException {}
void GiveBirth() {}
public void fly() {}
}
class checkinterface
{
Bat b = new Bat();
Mammal a = new Mammal();
b.fly();
};
The common errors are:
Class NoWingsException not found in throws.
Bat() throws NoWingsException {}
^
Class NoWingsException not found in class checkinterface
Class ColdBloodedException not found in class checkinterface.

Thanks in advance
padmini
[This message has been edited by padmini Babu (edited June 08, 2001).]
[This message has been edited by padmini Babu (edited June 08, 2001).]
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
have u defined the Exception classes ... and if so please check if they are in the classpath
do get back if the problem lies elsewhere
Samith.P.Nambiar
<pre>
\```/
(o o) harder u try luckier u get
-------oOO--(_)--OOo----------------------------
</pre>
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check the below code with the reqd. changes marked in bold


class ColdBloodedException extends RuntimeException { } // this is
UncheckedException
class NoWingsException extends Exception { } // this is a
checked Exception

class Mammal
{
Mammal() throws ColdBloodedException{}
void GiveBirth() {}
}
interface CanFly
{
void fly();
}
class Bat extends Mammal implements CanFly
{
Bat() throws NoWingsException {}
void GiveBirth() {}
public void fly() {}
}

class checkInterFace
{
public static void main( String args[] )
{

Mammal a = new Mammal();

// user had to catch this checked exception

try
{
Bat b = new Bat();
b.fly();
}catch( NoWingsException e )
{
System.out.println(" NoWingsException caught ");
// you insert more exception handling code here
}

}
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!