Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Java by (13.1k points)

Can anyone help me understand what is reference variable in Java? I was going through and I don't able to understand this line:

it is the type of object being referred to (not the type of reference variable) that determines which version of an overridden method will be executed.

1 Answer

0 votes
by (26.7k points)

Basically, it is referred to as polymorphism. You can take the below code as your reference:

public class Person {

    public Person() {

    }

    public void introduceYourself() {

    } 

}

public class Texan extends Person {

    public Texan() {

    }

    public void introduceYourself() {

        System.out.printLn("Howdy y'all!");

    } 

}

public class NewYorker extends Person {

    public NewYorker() {

    }

    public void introduceYourself() {

        System.out.printLn("Yo. You got a problem with that?");

    } 

}

So reference variable will help you determine which instance is called and it will provide the result accordingly.

I hope this will help.

Want to know more about Java? Prefer this tutorial on Java Tutorial.

Want to become a Java Expert? Join Java Course now!!

Related questions

Browse Categories

...