Java keyword: final

The final keyword can be applied to declaration of classes, methods and variables.

 

  • Final class: if a class is marked as final, it cannot be subclassed/inherited by another class. For example:

    1

    final class A { }

    then the following code will not compile:

    1

    class extends A {} // compile error

  • Final method: when a method is final, that means it cannot be overriden, neither by methods in the same class or in sub class. For example:

    1

    2

    3

    class C {

        final void foo() { }

    }

    the subclass D attempts to override the method foo(), but fail because foo() is marked as final:

    1

    2

    3

    class extends C {

        void foo() { } // compile error

    }

  • Final variable: if a variable is marked as final, its reference cannot be changed to refer to another object, once initialized. For example:

    1

    final String message = "HELLO";

    Once the variable message is initialized and marked as final, the following code attempts to assign another value to it, will fails:

    1

    message = "BONJOUR";    // compile error

Note: a class cannot be both abstract and final.(because the purpose of an abstract class is to be inherited.)

How Java final modifier affect your code

1. Final Class

When a class is marked as final, it cannot be subclassed. In other words, if we don’t want a class to be able to be extended by another class, mark it as final. Here’s an example:

 

1

2

public final class PrettyCat {

}

If we try to extend this final class like this:

 

1

2

class MyCat extends PrettyCat {

}

Compile error: error: cannot inherit from final PrettyCat

 

So the final modifier is used to prevent a class from being inherited by others. Java has many final classes: String, System, Math, Integer, Boolean, Long, etc.

 

 

 

NOTES:

 

There is no final interface in Java, as the purpose of interface is to be implemented.

- We cannot have a class as both abstract and final, because the purpose of an abstract class is to be inherited.

 

 

2. Final Method

 

When a method is marked as final, it cannot be overridden. Let’s look at the following example:

 

1

2

3

4

5

public class Cat {

    public final void meow() {

        System.out.println("Meow Meow!");

    }

}

Here, the meow() method is marked as final. If we attempt to override it in a subclass like this:

 

1

2

3

4

5

class BlackCat extends Cat {

    public void meow() {

        System.out.println("Gruh gruh!");

    }

}

The Java compiler will issue a compile error: error:

 

1

meow() in BlackCat cannot override meow() in Cat

So use the final keyword to modify a method in case we don’t want it to be overridden.

 

3. Final Variable

When a variable is marked as final, it becomes a constant - meaning that the value it contains cannot be changed after declaration. Let’s see an example:

 

1

final float PI = 3.14f;

If we try to assign another value for this final variable later in the program like this:

 

1

PI = 3.1415f;

The compiler will complain:

 

1

error: cannot assign a value to final variable PI

For reference variable:

 

1

final Cat myCat = new Cat();

After this declaration, myCat cannot point to another object.

 

In Java, constants are always declared with the static and final modifiers, for example:

 

1

static final float PI = 3.1415f;

The final modifier can be also used in method’s arguments. For example:

 

1

2

void feed(final Cat theCat) {

}

This means code in the method cannot change the theCat variable.

 

 

 

转载于:https://my.oschina.net/weaver/blog/1488695

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值