This site javatpoint is very useful for programming languages like JAVA,PYTHON,C,C++, Data Structures etc.Java learning Concept also provides technical materials , which are related to latest technologies. We provides very easy lectures to our viewers from beginning to advance level.We focus logical content instead of theoretical content.

Monday, 30 October 2017

//write a program to show final key word effects




//write a program to show final key word effects

We can declare a program to show that the Final keyword effect, to show the final keyword effect, so we can declare class, and inside the class we can declare variables and methods, if we can declare class as a final then couldn't inherit any other class, if we declare variable as a final then couldn't change the value of the variable and we declare methods as final then the method wouldn't be override in he sub class.

final class use
{
  final int marks=75;
int marks=80;
final void show()
{
System.out.println(" hello");
}
}
class dus extends use
{
void show()
{
System.out.println("good morning");
}
public static void main(String args[])
{
dus d1=new dus();
d1.show();
}
}

Explanation : we can declare class use and in the class we have declared variable max, these max variable also declare the as a final. Then we couldn't change the value of the max variable. Similarly we can declare method
In the sub class of use, in the sub class we can declare same name method but this method can't be override.

output:




C:\Users\admin22.DESKTOP-Q312MFE.000>javac dus.java
dus.java:6: marks is already defined in use
int marks=80;
    ^
dus.java:12: cannot inherit from final use
class dus extends use
                  ^
dus.java:14: show() in dus cannot override show() in use; overridden method is final
void show()
     ^
3 errors



//if we are not use final key word then these program look like these



 class use
{
 int marks=75;

 void show()
{
marks =80;
System.out.println(" hello");
}
}
class duse extends use
{
void show()
{
System.out.println("good morning");
}c
public static void main(String args[])
{
dus d1=new dus();
d1.show();
}
}

output: Now see a another program, in these program we can't use of the final keyword, if we can't use final keyword then we can change the value these variable and method is always override in her sub class. And then if we can't  declare final class then it can be access in her subclass.


good morning.










Adbox