Monday 11 June 2018

java - How should I have explained the difference between an Interface and an Abstract class?

In one of my interviews, I have been asked to explain the difference between an Interface and an Abstract class.



Here's my response:




Methods of a Java interface are implicitly abstract

and cannot have implementations. A Java abstract class can have
instance methods that implements a default behaviour.



Variables declared in a Java interface are by default final. An
abstract class may contain non-final variables.



Members of a Java interface are public by default. A Java abstract
class can have the usual flavours of class members like private,
protected, etc.




A Java interface should be implemented using keyword “implements”; A
Java abstract class should be extended using keyword “extends”.



An interface can extend another Java interface only, an abstract class
can extend another Java class and implement multiple Java interfaces.



A Java class can implement multiple interfaces but it can extend only
one abstract class.





However, the interviewer was not satisfied, and told me that this description represented "bookish knowledge".



He asked me for a more practical response, explaining when I would choose an abstract class over an interface, using practical examples.



Where did I go wrong?

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...