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 … Read more

Difference between abstract class and interface in Python

What is the difference between abstract class and interface in Python? 8 s 8 What you’ll see sometimes is the following: class Abstract1: “””Some description that tells you it’s abstract, often listing the methods you’re expected to supply.””” def aMethod(self): raise NotImplementedError(“Should have implemented this”) Because Python doesn’t have (and doesn’t need) a formal Interface … Read more

When to use: Java 8+ interface default method, vs. abstract method

Java 8 allows for default implementation of methods in interfaces called Default Methods. I am confused between when would I use that sort of interface default method, instead of an abstract class (with abstract method(s)). So when should interface with default methods be used and when should an abstract class (with abstract method(s)) be used? … Read more

Can an abstract class have a constructor?

Yes, an abstract class can have a constructor. Consider this: abstract class Product { int multiplyBy; public Product( int multiplyBy ) { this.multiplyBy = multiplyBy; } public int mutiply(int val) { return multiplyBy * val; } } class TimesTwo extends Product { public TimesTwo() { super(2); } } class TimesWhat extends Product { public TimesWhat(int … Read more