• Post Reply Bookmark Topic Watch Topic
  • New Topic

interview questions  RSS feed

 
Jhanavi Shivgami
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

i have faced some of the questions at my interview and i was struggling a lot, pl someone help me.

1)what are all the advantages of using interface instead of abstract class?
2)can an interface to be as final?
3)can i have all unimplemented methods in my abstract class?
4)can i have static variables in my interface?


pl this is very urgent.

Regards

Jhanavi.
 
Arvindu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2)can an interface to be as final?
No
3)can i have all unimplemented methods in my abstract class?
Yes ,the rule is you must have atleast 1 abstact method

4)can i have static variables in my interface

Yes , but default it is considered as final also
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3)can i have all unimplemented methods in my abstract class?
Yes ,the rule is you must have atleast 1 abstact method

- Abastract class can have Unimplemented methods but its not compulsory or rule
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can this be urgent?
Either you blew the interview or you didn't, getting an answer in 10 seconds after posting your question here won't change the outcome...

anyway, this is all pretty basic knowledge.

can an interface be final?
THINK.
interface == pure abstract
abstract == MUST be derived from
final == CANNOT be derived from

can an interface have static variables?
Sure, but only if they're final.

can an abstract class have all unimplemented methods?
Why shouldn't it?

what are the advantages of interfaces over abstract classes?
you can extend one class, implement many interfaces.
Which to use depends on what is most appropriate to the problem, never slavishly use one or the other exclusively.
An interface defines a contract, an abstract class a hierarchy of functionality.
Which is appropriate to use depends on circumstances.
Bear extends Animal
Bird extends Animal
TeddyBear extends Bear implements Pet
Canary extends Bird
TweetyBird extends Canary implements Pet

A Pet is a set of defined behaviours, doesn't provide that behaviour as it will differ widely between your TeddyBear and your TweetyBird (TeddyBear might be cuddly but doesn't sing).
 
Jhanavi Shivgami
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Jeroen Wenting. it was really useful.

Originally posted by Jeroen Wenting:
how can this be urgent?
Either you blew the interview or you didn't, getting an answer in 10 seconds after posting your question here won't change the outcome...

anyway, this is all pretty basic knowledge.

can an interface be final?
THINK.
interface == pure abstract
abstract == MUST be derived from
final == CANNOT be derived from

can an interface have static variables?
Sure, but only if they're final.

can an abstract class have all unimplemented methods?
Why shouldn't it?

what are the advantages of interfaces over abstract classes?
you can extend one class, implement many interfaces.
Which to use depends on what is most appropriate to the problem, never slavishly use one or the other exclusively.
An interface defines a contract, an abstract class a hierarchy of functionality.
Which is appropriate to use depends on circumstances.
Bear extends Animal
Bird extends Animal
TeddyBear extends Bear implements Pet
Canary extends Bird
TweetyBird extends Canary implements Pet

A Pet is a set of defined behaviours, doesn't provide that behaviour as it will differ widely between your TeddyBear and your TweetyBird (TeddyBear might be cuddly but doesn't sing).
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeroen Wenting:
how can this be urgent?
Good question. I usually ignore "urgent" questions :roll:
can an interface have static variables?
Sure, but only if they're final.
Well... it's perhaps more correct to say that any variable you define in an interface is automatically public, static and final. The Sun coding standards actually discourage you from making those modifiers explicit, but not everyone likes that.

can an abstract class have all unimplemented methods?
Why shouldn't it?
They are nonsensical questions, aren't they? You can, but who cares? A purely abstract class doesn't make sense anyway. Such a class wants to be an interface.

what are the advantages of interfaces over abstract classes?
you can extend one class, implement many interfaces.
Another boring question. If you want to show some perceptiveness, launch from the basic answer into a discussion of composition vs inheritance. But make sure you've got what it takes to pull that off though

- Peter
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!