Introduction to Java Override Methods: Android Studio Crash Course (Free Tutorial)

If you’ve ever been using a method whilst coding and suddenly changed your mind, this is the article for you. Today we’ll learn how to override methods in Android Studio using the Java programming language. To learn more coding for FREE, check out our 30-minute beginners course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

Today’s example is of a user who inputs the brand, model, and display of a phone, and we want Java to display those inputs as a message.  First we’ll try a method that doesn’t work. Then we’ll override it. To follow along in Android Studio, go into Project view. Then go to app > java. Right-click on the topmost com.example.zebra.demo. Select New > Java Class.

The tab “Create New Class” will pop up. Let’s name our class “Phone”. You should see the following on your screen:

public class Phone {

}

As you can see, we have created our new class. It will also appear in Project view inside the same folder that contains the MainActivity class.

Let’s declare some fields. As in this example, we usually make fields private so that they are not accessible from outside this Phone class. Declare two private strings: brand, for the brand of phone, and model, for the model of phone. Also declare the private double variable displayInInches. Specifying the double variable datatype will allow our value to have decimal places. Note that each line should end in a semi-colon.

private String brand;
private String model;
private double displayInInches;

As well in the Phone class, we need to code for a constructor. When an object is created, Java first executes the code in the constructor. The constructor will then invoke that object. Create the public constructor Phone by typing the following:

public Phone (){

}

Note that a constructor takes the same name as its class. In the parentheses of the constructor, let’s set these parameters: String brand, String model, double displayInInches

We want to make sure we initialize the fields brand, model, and displayInInches. Furthermore, we have to set up the fields to their parameters. For instance, we need to set up the field called “brand” to the parameter called “brand”. In order to do this, add the following on a line within your Phone constructor:

this.brand

This refers to the private field brand. Set up the field equal to the parameter brand, which represents the value that’s input by the user.

this.brand = brand;

Do the same thing with the two other fields:

this.model = model;
this.displayInInches = displayInInches;

Next go to app > java > (topmost) com.example.zebra.demo > MainActivity. Beneath setContentView(R.layout.activity_main);, create a new object called myPhone. Use the constructor Phone:

Phone myPhone = new Phone();

We also want to display a message on the screen. For this, we can use the Toast utility in Android Studio. On the next line, type in “Toast”, select “Create a new Toast”, and hit Enter. Android Studio will auto-complete the following code:

Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();

Replace the quotation marks with myPhone.toString() to display the specifications of the phone. With this method, the emulator will return a string, and a string will be displayed. However, this string will not represent the specifications or fields from myPhone, including brand, model, and displayInInches.

By running the emulator, we can see what is actually displayed. Zoom in to see a code show up on the screen that represents the reference in the memory where the Phone class is kept: com.example.zebra.demo.Phone@b898a9

That’s not very useful. How do we show our specifications? We can give new meaning to our used method by overriding it.

Go back to the Phone class. Because we want this method to return a string, create the public string toString beneath your constructor.

public String toString(){

}

To specify that this method overrides the method that was previously created, add @Override on a line above the new string.

In addition, we must create a message. On a line underneath public String toString({, create the new string message. Set it equal to the three fields we specified in our class.

String message = "Brand: " + this.brand +
\n"Model: " + this.model +
\n"Display in inches: " + this.displayInInches;

Note that because we added \n to the last two lines, our message will be displayed on three separate lines.

Beneath that, we need to code for our message to display on the screen. Use the following format:

return message;

This function will display a message with the three fields we specified. Run the emulator and zoom in. You will see the following displayed on the screen:

Brand: Samsung
Model: Galaxy SS3
Display in inches: 4.7

There you go! If you want to learn even more coding for FREE, check out our 30-minute beginners course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

Mammoth Interactive Favicon

Why you NEED to take this course :

Get in Touch.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

EMAIL US

support@mammothinteractive.com