Q:

How to create a class that came be used by other classes when there are only a few variable differences in kotlin

I have 4 fragments that all use the same code with the exception of a few variables being different.
the variables differ in name and then there are functions that call those variables. but all the rest of the code is copy and pasted. 99% the same.
How do i properly create a class that other classes can inherit from and override the names of those 2-3 variables and function calls
for example:
    import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView

import kotlinx.android.synthetic.main.activity_multiplication_game.*

class MultiplicationGame : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_multiplication_game)
        supportActionBar!!.hide() // REMOVES ACTION BAR
        val prefs = this.getSharedPreferences("com.QuickMaths", MODE_PRIVATE) // preference to keep track of ad number

        val insertPoint = insert_point as ViewGroup
        val ed: TextView = editTextAnswers as TextView

        val context: Context = this@MultiplicationGame
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

        // gets the number of rows and digits. Now implement the game
        val rowNumber = intent.getStringExtra("rowNumber").toInt()
        val digitNumber = intent.getStringExtra("digitNumber").toInt()
        var numberOfQuestions = intent.getStringExtra("NumberOfQuestions").toInt()
        val atLeastOrAtMostDigits = intent.getStringExtra("NumberOfAtLeastDigits").toInt()
        val atLeastOrAtMostRows = intent.getStringExtra("NumberOfAtLeastRows").toInt()

        //Main menu button
        mainMenu.setOnClickListener {
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
        }

        val game = CreateLayout(rowNumber, digitNumber, insertPoint, context, atLeastOrAtMostDigits,atLeastOrAtMostRows)
        Log.d("poopAdditionGame", atLeastOrAtMostDigits.toString())
        val timeKeeper = KeepTime()

        game.generateViewsWithNumbers(2)

        //gets the number of questions for the counter that sits on top
        val totalQuestions = numberOfQuestions
        showQuestionsLeft.text = numberOfQuestions.toString() + "/" + totalQuestions.toString()

        //checks the answers to see if they are correct
        ed.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                val aftertextchanged = ed.text.toString()
                if (game.totalProduct.toString() == aftertextchanged) {
                    //things happen here

                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
                //To change body of created functions use File | Settings | File Templates.
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                //Nothing
            }
        })

    }

    // the keyboard
    fun numberButton(view: View) {
        val buttonClicked: Button = view as Button
        val viewtext: TextView = editTextAnswers as TextView
        var buttonClickedValue = viewtext.text.toString()

        when (buttonClicked.id) {
            button8.id -> buttonClickedValue += "1"
            button7.id -> buttonClickedValue += "2"
            button6.id -> buttonClickedValue += "3"
            button9.id -> buttonClickedValue += "4"
            button10.id -> buttonClickedValue += "5"
            button11.id -> buttonClickedValue += "6"
            button12.id -> buttonClickedValue += "7"
            button13.id -> buttonClickedValue += "8"
            button14.id -> buttonClickedValue += "9"
            button16.id -> buttonClickedValue += "0"
        }
        viewtext.text = buttonClickedValue
    }

    fun clearButton(view: View) {
        val viewtext: TextView = editTextAnswers as TextView
        viewtext.text = ""
    }

    fun delButton(view: View) {
        val viewtext: TextView = editTextAnswers as TextView
        var mutList = viewtext.text.split("")
        viewtext.text = mutList.toMutableList().dropLast(2).joinToString("")
    }
}

this code is the same exact code for 4 different activities EXCEPT the variable totalSum is changed to product and difference. and game.generateViewsWithNumbers(2) 2 should be a 1 or 3 depending on the activity
the point being how can i recycle the code with just the small differences in mind and change those.

A:

You just need to create a base fragment/activity.
abstract class MultiplicationGame : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_multiplication_game)
        supportActionBar!!.hide() // REMOVES ACTION BAR
        val prefs = this.getSharedPreferences("com.QuickMaths", MODE_PRIVATE) // preference to keep track of ad number 
    //Rest of the code

And implement it in the activities that will be using the same code. i didn't understand 100% what you need to be different regarding totalSum, because i couldn't find one it in your code. 
Regarding this:
 game.generateViewsWithNumbers(2)

If you just need a int value in each activity you can do something like this:
abstract intValue: Int
game.generateViewsWithNumbers(intValue)

And then the child activity just needs to give a value to the variable.

