官术网_书友最值得收藏!

Overriding and Hiding Methods

When extending a class, it is possible to redefine some of the methods that are part of it. Overriding means to rewrite something's functionality. This is done by making a new declaration of the method with the same name and properties of the method from the original class. This is demonstrated in the following example. Note that we're continuing, for the sake of clarity, with Computer and Tablet, but they have been cleaned up so as not to make the example programs too long.

class Computer {

    public void whatIsIt() {

        System.out.println( "it is a PC");

    }

}

class Tablet extends Computer {

    public void whatIsIt() {

        System.out.println( "it is a tablet");

    }

}

class Example06 {

    public static void main(String[] args) {

        Tablet myTab = new Tablet();

        myTab.whatIsIt();

    }

}

Since Tablet extends Computer, you could modify the main class in the program to be as follows:

class Example06 {

    public static void main(String[] args) {

        Computer myTab = new Tablet();

        myTab.whatIsIt();

    }

}

Technically, tablets are computers, which means that you can create an object of the Tablet class by defining it as Computer in the first place. The result for both cases will be the same:

it is a tablet

Process finished with exit code 0

The result is the same for both classes because both the child and parent classes include a non-static method called whatIsIt(). When calling the method, the overriding one will have priority. This is done by the JVM at runtime. This principle is what we call runtime polymorphism. There can be multiple definitions of the same method, and which definition will be executed is decided during the execution of the program.

But what would happen if the method you called was static? This could be a design decision taken by the developer who is creating the class you are extending and therefore is a situation out of your control. In this case, it is not possible to override the method. The child class can, however, hide the method defined by the parent using the same mechanism. The next code listing demonstrates this.

class Computer {

    public static void whatIsIt() {

        System.out.println( "it is a PC");

    }

}

class Tablet extends Computer {

    public static void whatIsIt() {

        System.out.println( "it is a tablet");

    }

}

class Example07 {

    public static void main(String[] args) {

        Computer myTab = new Tablet();

        myTab.whatIsIt();

    }

}

The outcome of this example is:

it is a PC

Process finished with exit code 0

The decision of what method should be used with static methods is not taken at runtime but during compilation, and this ensures that the method from the parent class is the one being called. This action is called hiding instead of overriding. It is still possible to call the method in the Tablet class. To do so, you should modify the code in the main class to the following:

class Example07 {

    public static void main(String[] args) {

        Computer myTab = new Tablet();

        Tablet.whatIsIt();

    }

}

Note how we clearly specify the actual class you call for this. The result of the modified example is:

it is a tablet

Process finished with exit code 0

Avoiding Overriding: Final Classes and Methods

If you want to stop other developers from overriding parts of your class, you can declare the methods you want to protect as final. An example of this could be a class that deals with temperature. The method that converts from Celsius into Fahrenheit is final, as it makes no sense to override such a method.

class Temperature {

    public double t = 25;

    public double getCelsius() {

        return t;

    }

    final public double getFahrenheit() {

        return t * 9/5 + 32;

    }

}

class Example08 {

    public static void main(String[] args) {

        Temperature temp = new Temperature();

        System.out.println( temp.getCelsius() );

        System.out.println( temp.getFahrenheit() );

    }

}

This program will give this result:

25.0

77.0

Process finished with exit code 0

Note

Alternatively, you can declare a whole class final. A final class cannot be extended. An example of such a class is String. You could ask whether it defeats the purpose of object-oriented programming to have a class that cannot be extended. But there are some classes that are so fundamental to the programming language, such as String, that they are better kept as they are.

主站蜘蛛池模板: 岳普湖县| 抚松县| 枝江市| 广德县| 霍林郭勒市| 伊金霍洛旗| 丰台区| 新泰市| 广河县| 长子县| 武宁县| 安顺市| 娱乐| 吉水县| 江北区| 苍梧县| 渭南市| 邮箱| 柳江县| 揭阳市| 新安县| 红安县| 蒙阴县| 锦州市| 锡林郭勒盟| 罗甸县| 宜良县| 永泰县| 克什克腾旗| 诸暨市| 公主岭市| 齐齐哈尔市| 股票| 南通市| 香港| 凤凰县| 都匀市| 邵武市| 涞水县| 嘉禾县| 江阴市|