“final” Keyword Hints

In java you can use Final keyword in 3 contexts:

  1. Variables

  2. Methods

  3. Classes

Final Variables:

make it constant and it can’t change after it was init. change only one time.

examples:

public class KeywordTest {

    /*
     * have a final keyword for variables.
     */
    private final String str1 = "str init'ed!";
    private final int number = 12;

    public void finalChanger() {

        String str2 = str1;
        str2 = str2.toUpperCase();

        // below line can't be compiled!
        // str1 = str2;

        int num2 = number;
        num2 += 12;

        System.out
                .println("contents are copied and copies were modified,"
+" the first contents referenced with final keywords, are still the same.");
        System.out.println(str2 + "");
        System.out.println(num2 + "");
    }

    /**
     * @param args
     */
    public static void main(String[] args) {

        new KeywordTest().finalChanger();
    }

}

Hint: you can init the final variables in constructor and keep them non-initialized before hand, please see the example below:

you can init the final variables in the constructor and keep them non-initialized before hand, please see the example below:

you can init the final variables in the constructor and keep them non-initialized before hand, please see the example below:

public class KeywordTest {

    public KeywordTest(int number) {
        this.CODE = number;
    }
    
    private final int CODE;

    public void finalChanger() {
        int num2 = CODE;
        num2 += 12;

        System.out.println(num2 + "");
    }

    /**
     * @param args
     */
    public static void main(String[] args) {

        new KeywordTest(12).finalChanger();
    }

}

Final Methods:

if you want to force inherited class/children to use some methods in Parent class without modifying it, this keyword is useful.

Parent Class

public abstract class Parent {
    
    /**
     * final method in parent class.
     */
    public final void dontOverride(){
        System.out.println("In Parent.class");
        System.out.println("dontOverride() one is implemneted in parent class and can't be overriden.");
    }
    
    /**
     * implemented method in parent part.
     */
    public void method1(){
        System.out.println("In Parent.class");
        System.out.println("Method()1 is implemneted in parent class.");
    }
    
    /**
     * this is the abstract method signature in parent part.
     */
    public abstract void method2();
    
}

Child class:

public class Child extends Parent {

    @Override
    public void method2() {
        System.out.println("in Child.class");
        System.out.println("Method()2 is implemneted in Child class, and it was abstract method in Parent class.");

    }

    @Override
    public void method1() {
        System.out.println("in Child.class");
        System.out.println("Method()1 was implemneted in Parent class, and again in Child class was overriden.");

    }

    public void explanation() {
        System.out.println("in Child.class");
        System.out
                .println("Parent.dontOverride() Method can't be accessible to change its signration in other subclasses, theonly way isto use the method by calling the parent class instance.");

        System.out.println("Calling Parent.dontOverride() in Child class using parent reference. ");
        super.dontOverride();
    }

}

Testing Program class

public class ProgramTest {
    public static void main(String[] args) {
        Child c = new Child();
        System.out.println("-------------------");
        c.method1();
        System.out.println("-------------------");
        c.method2();
        System.out.println("-------------------");
        System.out
                .println("Calling final parent method using Child reference, since it's still accibel in the hirarchial order,"
                        + " but it can;t be changed/updated.");
        c.dontOverride();
        System.out.println("-------------------");
        c.explanation();
    }
}

Final Classes:

make the class non-extendable. so none of its functionalities can’t be overridden.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s