Best example of Abstract class in Android
Asked Answered
D

3

14

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.

Darin answered 17/12, 2015 at 9:43 Comment(3)
"i don't understand my abstract method is not getting called." - You're not calling it anywhere; you're just defining it.Aquitaine
yes , because i dont know how to call , can you please let me know how to do thatDarin
where to call , from which activityDarin
M
10

Register and Unregister any BroadcastReceiver

Here is an example which you can use to register and un-register ANY BroadcastReceiver using an Abstract class:

BaseClass:

public abstract class BaseReceiverActivity extends AppCompatActivity{

    private BroadCastReceiver receiver;
    private IntentFilter filter;

    public abstract BroadCastReceiver getReceiver();
    public abstract IntentFilter getFilter();

    @Override
    public void onStart(){
        super.onStart();
        configureReceiver()
        registerReceiver(receiver, filter);
    }

    @Override
    public void onStop(){
        super.onPause();
        unregisterReceiver(receiver);
    }

    private void registerMyReceiver(){        
        registerReceiver(receiver, filter);    
    }

    private void configureReceiver(){
         receiver = getReceiver();
         filter   = getFilter();
    }

}

Child class:

public class WifiScanner extends BaseReceiverActivity{

    @Override
    public void onCreate(Bundle sis){
         super.onCreate(sis);
         setContentView(R.layout.yourLayout);
    }

    @Override
    public BroadCastReceiver getReceiver(){
         return new YourReceiver();
    }

    @Override
    public IntentFilter getFilter(){
         return IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    }

}

Full working code

Here

Mosqueda answered 16/4, 2019 at 8:56 Comment(0)
C
46

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.

Caesium answered 17/12, 2015 at 10:35 Comment(3)
Instead of "Non abstract method of abstract class always call method of Super class" , i think "Non abstract method of Derived class will always call method of Super class" should come. ... Am I correct ?Dodecasyllable
Hey nice example .... I m in middle of an abstract class issue in my android project how can I use method startActivityforresult() in a Class which extends an abstract classCladding
But what if there is another activity implements myView() & activityCreated() then which Activity methods get called.Lxx
F
17

I want to complete Hiren Partel Answer with an example.

  • Abstract method of abstract class must be Override in Derived class
  • Non abstract method of abstract class always call method of Super class
  • If abstract class Implements an Interface it is possible to not to Implement methods and let the finally driven class to Implement the Interface methods

for example GOD controls his creature on Earth by implementing this interface ( :D) :

public interface َAliveCreature{

    void breath();
    void eat();
    void move();
    void die();

}

And this is abstract class live that have one public method and one abstract method:

public abstract class MammalAbstract implements َAliveCreature{

public void feedBabyWithMilk(){
    log.i(TAG,"baby was fed");
}

abstract void haveDream();
//this is an abstract method and had to implement in the consumer class
}

and this is finally driven class, human:

public class Human extends MammalAbstract {

    @Override
    void die() {

    }

    @Override
    public void breath() {

    }

    @Override
    public void eat() {

    }

    @Override
    public void move() {

    }

    @Override
    public void haveDream() {

    }
}

as you can see human had to implement abstract method haveDream() and also implement abstactclass interface methods! so this is the power of an abstract class that can handle and add some method and pass rest Interface methods to consumer and very use-full for writing libraries.

Ferromagnetic answered 24/6, 2017 at 8:34 Comment(0)
M
10

Register and Unregister any BroadcastReceiver

Here is an example which you can use to register and un-register ANY BroadcastReceiver using an Abstract class:

BaseClass:

public abstract class BaseReceiverActivity extends AppCompatActivity{

    private BroadCastReceiver receiver;
    private IntentFilter filter;

    public abstract BroadCastReceiver getReceiver();
    public abstract IntentFilter getFilter();

    @Override
    public void onStart(){
        super.onStart();
        configureReceiver()
        registerReceiver(receiver, filter);
    }

    @Override
    public void onStop(){
        super.onPause();
        unregisterReceiver(receiver);
    }

    private void registerMyReceiver(){        
        registerReceiver(receiver, filter);    
    }

    private void configureReceiver(){
         receiver = getReceiver();
         filter   = getFilter();
    }

}

Child class:

public class WifiScanner extends BaseReceiverActivity{

    @Override
    public void onCreate(Bundle sis){
         super.onCreate(sis);
         setContentView(R.layout.yourLayout);
    }

    @Override
    public BroadCastReceiver getReceiver(){
         return new YourReceiver();
    }

    @Override
    public IntentFilter getFilter(){
         return IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    }

}

Full working code

Here

Mosqueda answered 16/4, 2019 at 8:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.