What is the reason behind it ? Is it for performance or compulsion? Please Explain
Abstract class in java can't be instantiated. An abstract class is mostly used to provide a base for subclasses to extend and implement the abstract methods and override or use the implemented methods in abstract class.
The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.
An abstract class is used if you want to provide a common, implemented functionality among all the implementations of the component. Abstract classes will allow you to partially implement your class, whereas interfaces would have no implementation for any members whatsoever.
Abstract classes can include abstract methods. Any class that extends a class with an abstract method must implement that method. For example, our Mammal class includes an abstract speak() method. Any class that extends Mammal must implement the speak method, and that implementation must have the same signature.
Because RoomDatabase
has defined methods, the inheriting class cannot be an interface. And because you write your undefined method in that abstract class, it cannot be a class.
public interface MyDatabase extends RoomDatabase // extend is not allowed for interface
public class MyDatabase extends RoomDatabase {
public abstract MyDao myDao(); // abstract method is not allowed for non abstract class
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With