Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best example of Abstract class in Android

I am trying to design one Abstract class and method in Android and call those methods by extending the class from my parent Activity class but I don't how to call my abstract method.

MyCode :

MainActivity.java

public class MainActivity extends MyActivity {

    @Override
    public void onTest() {

       Log.d("MyLog", "onTest");

    } }

MyActivity.java

public abstract class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

    }

public abstract void onTest(); }

So this is the above code snippet , please let me know , how to use Abstract in Android because i have never done this before.

like image 926
deeptimancode Avatar asked Dec 17 '15 09:12

deeptimancode


People also ask

Which is the best example of an abstract class?

The best example of an abstract class is GenericServlet . GenericServlet is the parent class of HttpServlet . It is an abstract class.

Which are abstract classes in Android?

A class which has “abstract” declaration after access modifier (public, protected, private) is called an abstract class. An abstract class can have any kind of method with the method body, it also can have methods with the only function declaration, but those methods must be declared as an abstract method.

What is abstract class in OOP with example?

An abstract class is a class that contains at least one abstract method. An abstract method is a method that is declared, but not implemented in the code.

What is real time example of abstraction?

Making coffee with a coffee machine is a good example of abstraction. You need to know how to use your coffee machine to make coffee. You need to provide water and coffee beans, switch it on and select the kind of coffee you want to get.


1 Answers

I have developed Example for Abstract Class:

Abstract class:

public abstract class BaseActivity extends Activity {

    public static final String TAG = "Test";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(myView());
        activityCreated();
    }

    public void printMessage(String message){
        System.out.print(message);
    }


    public abstract int myView();
    public abstract void activityCreated();

}

Non Abstract class which extends Abstract class:

public class TestActivity extends BaseActivity {

@Override
public int myView() {
     return R.layout.activity_main;
}

@Override
public void printMessage(String message) {
    super.printMessage(message);
}

@Override
public void activityCreated() {
    Log.i("TestActivity", "Created");

    printMessage("Hello Hiren !!!");
  }
}

Conclusion:

  • Abstract method of abstract class must be Override in Derived class
  • Non abstract method of abstract class always call method of Super class

Hope this will make sense sure.

like image 129
Hiren Patel Avatar answered Sep 18 '22 16:09

Hiren Patel