-1

what is the use of abstract class in android.

For code reusablity I can create base class that contains common method and it extended in other class.I would like to know what is the exact use of abstract class in android

public class Base{

   public void test(){
     System.out.prinltn("test"); 
   }

}

public class main extends Base{
    test();
}
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
  • you can outsource code of classes wich got same elements and keep your code withour duplicates. `DRY-principle`. Please also have a look on the `SOLID-Principle` when you got basic questions like this. It´s a kind of architectre. – LenglBoy Mar 22 '18 at 07:04

1 Answers1

1

When there is some part of the implementation (let's call it A) which you want to be provided by the sub classes (in future as per requirements) and also there is some comman part of the implementation shared by all sub classes (let's call it B). Now part A is not known as how to be implemented as it will depend on future requirements hence it's best to have it as abstract. By having abstract methods all the sub classes are forced to provide the implementation.

Abstract class can contain all the common states and methods and can abstract the methods which will have different implementation as per the sub class implementation. The common methods also can be override if needed.

Also you can refer template design pattern, it's intent and implementation as it is a very good example of usuage of abstract class.

nits.kk
  • 5,204
  • 4
  • 33
  • 55
  • can you provide the concepts in simple manner . It helps me to understand the concept – Dineshkumar Arunachalam Mar 22 '18 at 07:28
  • so the answer will be avoid the object creation for base class. i would prefer abstract class.Is my assumption is correct? – Dineshkumar Arunachalam Mar 22 '18 at 09:11
  • Suppose you are writing a class which has a behaviour whose implementation you do not know and want the sub classes to decide the implementation. Now of course as one of the behaviour does not have implementation hence you would not like the objects of such a class to exist (abstract class can not be instantiated). Read factory pattern and template pattern, they will be helpful in seeing usuage of abstract class. I will try to edit my answer to provide some more details later – nits.kk Mar 22 '18 at 09:20