What’s the difference between abstract and non-abstract methods in Java?

0
0
Asked By TechWhiz_829 On

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

Answered By CodeNinja_451 On

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.

Answered By DevGuru_337 On

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.

CleverCoder_123 -

Exactly! Abstract classes can't be instantiated directly.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.