Tuesday, 30 August 2016

final class

1.       final classes:-
final class is a java class, it will not allow inheritance that is it will not allow sub classes.

Ex 1:-

final class A
{
        ----
}
class B extends A
{
        ----
}

Status:- Compilation Error.

Ex 2:-

final class A
{
        ----
}
final class B extends A
{
        ----
}

Status:- Compilation Error.

Ex 3:-

class A
{
        ----
}
final class B extends A
{
        ----
}

Status:-  No Compilation Error.

To declare constant variable in java applications, java has provided a conversion like to use “public static final”.

Ex code :-

class User
{
        public static final int MIN_AGE=18;
        public static final int MAX_AGE=30;
}
class Test
{
        public static void main(String[] args)
        {
                        System.out.println(User.MIN_AGE);
                        System.out.println(User.MAX_AGE);
        }
}

Output:-
18
30
Where the main intention of declaring constant variables “public” is to make available constant variables throughout the java application.
Where the main intention to declare constant variable “static” is to access constant just by using classes name and without creating object.
Where the main intention to declare constant variable as “final” is to fix a particular value to the variable throughout the java application.
javap java.lang.System

Note:-
In java, some of the predefined variable like in, out, err in system class are declared as final variables.
class finalSystem
{
                public static final InputStream in;
                public static final PrintStream out;
                public static final PrintStream err;
                ---
                ---
               
}

In thread class, the variable like MIN_PRIORITY, NORM_PRIORITY, MAX_PRIORITY are declared as “public static final”.
public class Thread implements Runnable
{
                public static final int MIN_PRIORITY=1;
                public static final int NORM_PRIORITY=5;
                public static final int MAX_PRIORITY=10;
                -------
                -----

}
Ex code :-
class User_Status
{
                public static final String AVAILABLE="User Available";
                public static final String BUSY="User Busy";
                public static final String IDLE="User Idle";
}
class Test
{
                public static void main(String[] args)
                {
                                System.out.println(User_Status.AVAILABLE);
                                System.out.println(User_Status.BUSY);
                                System.out.println(User_Status.IDLE);
                }
}
Output:-
User Available
User Busy
User Idle
Date :august-10-2016

To declare the constant it we use the above approach we are able to get the following drawbacks.
1.       For each and every constant variable we have to provided “public static final”.
2.       The above approach is able to allow different data types like int, float, Boolean,… to declare constant it will reduce typedness in java applications.
3.        In the above approach, if we access constant variable then the values and the constant variables, are displayed, here constant variable values may or may not reflect the actual meaning of the constant variables.

To overcome the above problems, we have to use “enums”.

No comments:

Post a Comment