Mohamad Ziadeh
Mohamad Ziadeh

Reputation: 7

Overriding parent method in child class. Child method not doing anything

I am having some trouble. I followed every guide online showing how to override a parent method in a child class. I have done everything I was told to do, yet my child function does nothing.

My MainActivity(Parent) class:

package com.example.flashcards;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    DatabseHelper DB = new DatabseHelper(this);

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

        changeText();
        changeText2();
    };
    public void changeText(){}
    public void changeText2(){}
        String [] columns = new String[] {
                DatabseHelper.FLASHCARD_QUESTION,
                DatabseHelper.FLASHCARD_ANSWER
        };

    @Override
    public void onClick(View v) {

    }
}

My child class (TextC)

package com.example.flashcards;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TextC extends MainActivity {

    @Override
    public void changeText() {
        super.changeText();
        final String[] revertText = {"H2O", "What elements does water consist of?"};

        final TextView textChange = findViewById(R.id.flashcard1);
        Button change = findViewById(R.id.answer1);

        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int random = (int) (Math.random() * revertText.length);
                textChange.setText(revertText[random]);
            }
        });
    }

    public void changeText2() {
        super.changeText2();
        final String[] revertText = {"2,200° F", "How hot does lava get?"};

        final TextView textChange = findViewById(R.id.flashcard2);
        Button change = findViewById(R.id.answer2);

        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int random = (int) (Math.random() * revertText.length);
                textChange.setText(revertText[random]);
            }
        });
    }
}

My changeText() function does nothing. I am not getting any errors, so I can not tell what I am doing wrong. Do I need to create an onCreate method for the child class? But I am extending MainActivity which has it.

Any ideas on why my method overriding is not working?

Upvotes: 0

Views: 2661

Answers (2)

Geo
Geo

Reputation: 756

Modify parent class

package com.example.flashcards;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    DatabseHelper DB = new DatabseHelper(this);

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

        //changeText(); freeze or remove these callings
        //changeText2();
    };
    public void changeText(){}
    public void changeText2(){}

}

And add some code to your child class

package com.example.flashcards;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TextC extends MainActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    changeText(); 
    changeText2();
};

    @Override
    public void changeText() {
        super.changeText();
        final String[] revertText = {"H2O", "What elements does water consist of?"};

        final TextView textChange = findViewById(R.id.flashcard1);
        Button change = findViewById(R.id.answer1);

        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int random = (int) (Math.random() * revertText.length);
                textChange.setText(revertText[random]);
            }
        });
    }

    public void changeText2() {
        super.changeText2();
        final String[] revertText = {"2,200° F", "How hot does lava get?"};

        final TextView textChange = findViewById(R.id.flashcard2);
        Button change = findViewById(R.id.answer2);

        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int random = (int) (Math.random() * revertText.length);
                textChange.setText(revertText[random]);
            }
        });
    }
}

In the above method there is no point declare changeText(); and changeText2(); in parent activity. For the sake of reusability, we can use abstract classes and methods.

Do some changes to your parent activity as you see below.

public abstract class MainActivity extends AppCompatActivity implements View.OnClickListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      changeText(); // they dont have default implimentation in parent so it will be invoked from child class where these methods implimented
      changeText2();
   };


public abstract void changeText(); //there is no default implimentation

public abstract void changeText2();

}

And in child activity, you have to implement those methods.

public class TextC extends MainActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //changeText(); no need to call these methods bcz its already called in parent onCreate()
    //changeText2();
};

    @Override
    public void changeText() {
        super.changeText();
        final String[] revertText = {"H2O", "What elements does water consist of?"};

        final TextView textChange = findViewById(R.id.flashcard1);
        Button change = findViewById(R.id.answer1);

        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int random = (int) (Math.random() * revertText.length);
                textChange.setText(revertText[random]);
            }
        });
    }

    public void changeText2() {
        super.changeText2();
        final String[] revertText = {"2,200° F", "How hot does lava get?"};

        final TextView textChange = findViewById(R.id.flashcard2);
        Button change = findViewById(R.id.answer2);

        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int random = (int) (Math.random() * revertText.length);
                textChange.setText(revertText[random]);
            }
        });
    }
}

Upvotes: 0

www.hybriscx.com
www.hybriscx.com

Reputation: 1129

With inheritance and overriding concepts, you need to override onCreate function in your child class. And from that overridden method, you can make a call to super.onCreate or you can do this.chnageText and this.changeText2 from child class.

Otherwise when you call onCreate function, it will call changeText and changeText2 from super class only.

In your child class

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

        this.changeText();
        this.changeText2();
    };

Upvotes: 1

Related Questions