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.
The best example of an abstract class is GenericServlet . GenericServlet is the parent class of HttpServlet . It is an abstract class.
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.
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.
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.
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:
Hope this will make sense sure.
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