I get the idea of abstraction in programming, but I'm struggling to understand how interfaces only consist of abstract methods. Also, is it possible to instantiate an object from a class that has an abstract method?
2 Answers
So basically, if you have a method like `blorp()` in a base class, subclasses can choose to override it, but they don't have to. However, if the method is declared as abstract, then the subclass must provide an implementation for it. All methods in an interface are also implicitly abstract, meaning they need to be implemented by any class that uses the interface.
And just to note, Java 8 introduced default methods, so some methods can have a body in interfaces, but this doesn't change the core idea.
Totally! It's really similar to C++. You can't create an instance of a class that has pure virtual (abstract) methods because that makes it an abstract class, which can't be instantiated until all abstract methods are implemented in a subclass.
Exactly! Abstract classes can't be instantiated directly.