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

implementation of Interface

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In one the test which i took had a question on interface. the answerrs that were in close relation to that question were an abstract class that implemented the interface and another one was an interface that extended the interface.

i marked the answer as the abstact class that implemented the interface but i was given wrong.

my doubt is whether an abstract class implementing an interface has to implement all its method or else should it redefine those methods if it is not implementing that method.

please clear me
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Selve,

Was it a mock question? The question could be ambiguous. You may want to post the mock question here.

An abstract class, just like any concrete class, can implement any number of interfaces. The difference is an abstract class can choose to implement part of an interface or simply leave it out for the concrete subclasses to implement.

An interface extends another interface.

Joyce
[ September 07, 2004: Message edited by: Joyce Lee ]
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Selva Prasad Rajendran:
my doubt is whether an abstract class implementing an interface has to implement all its method or else should it redefine those methods if it is not implementing that method.



If abstract class is implementing any interface it is free to implement its zero or more methods. But there is no compulsion in implementing any interface method.

But the concrete class which extends this abstract class has to implement all the abstract methods of the class plus the interface methods which are not implemeted be the abstract class

Hope this makes your doubt clear.
 
Selva Prasad Rajendran
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got the doubt cleared and i under stood but the question in one of the test engine was this


Given the following interface definition, which definition are valid


interface I
{ void setvalue(int val);
int getvalue( );
}


a) class A extends I {
int value;
void setValue(int val){
value=val;
}
int getvalue( ){
return value;
}
}

b) interface B extends I{
void increment( );
}



c)abstact class C implements I{

int getvalue( ){ return 0;}
abstract void increment( );
}

d)
interface D implements {

void increment( );
}


e) class E implements I {
int value;
public void setvalue(int val){ value= val;}
}



In this above question i marked b and c but in the answers its given that only b is the correct answer please explain me why c cant be the answer. but i feel that c is also correct because the absract calss can implement the interface and pass the impementation to the next concrete subclass that extends the abstact class.
please any one clear me
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An interface's methods are implicitly public abstract.

Class C declares getValue() with default package access, which makes it an illegal implementation of interface I.
[ September 08, 2004: Message edited by: Jimmy Praet ]
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic