Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it recommended to make the class that extends Room Database to abstract?

What is the reason behind it ? Is it for performance or compulsion? Please Explain

like image 954
Dharmik Thakkar Avatar asked Jul 02 '19 10:07

Dharmik Thakkar


People also ask

Why do we extend an abstract class in Java?

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.

Why should we use abstract class instead of normal 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.

What is the main reason for using abstract classes?

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.

What happens when a class extends an abstract class?

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.


1 Answers

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

}

like image 139
Israel dela Cruz Avatar answered Sep 22 '22 10:09

Israel dela Cruz