Sharp Tutorial
final keyword in java

Python

Java

C

C++

HTML/CSS

Java Script

PHP

SQL

C Programs

Programming in Java

  • Java Home
  • Introduction To Java
  • JDK, JRE, JVM
  • First Programm in Java
  • Data types and Operators
  • IF-ELSE, SWITCH-BREAK
  • WHILE, DO WHILE, FOR LOOP
  • Array In Java

Object Oriented in Java

  • Object and Class
  • Constructor in Java
  • Overloading in Java
  • Inheritance in Java
  • Overriding in java
  • super in Java
  • final in Java

Advanced topics in Java

  • Package in Java
  • Access Specifier in Java
  • Interface in Java
  • Exception Handling in Java
  • IO in Java
  • Applet in Java
  • String Handling
  • File Handling
  • Multithreading in Java
  • Awt and Events in Java
  • Collection in Java
  • GUI programming using Swing
  • Java Database Connectivity (JDBC)

final in java

Final variable in Java

In java, we can not change the value of final variable.

 class FinalVarExample
{
public static void main(String args[])
{
final double pi=3.14;
int i=100;
i=200; // we can change i value because it is not final variable
// pi=4.13; as pi is final variable so we can not change its value.
System.out.println("i= "+i +" pi= "+pi);
}
}

output :  i=200  pi=3.14

Final class in Java

Declaring final class in preventing the inheritance .So if we declare a class as final no other clas may inherit it . A class me be declared as final by keyword final before class.

 final class A
{

}

class B //extends A [As A class is final no class may inherit A class]
{

}

final Method in java

final method is used to prevent the override the methods in sub classes . Means if we declare a method final then sub class may not have same name method or we can say final method may not be overridden.

 class A
{
void display()
{
System.out.println("final method in super class");
}
final void show()
{
System.out.println("Not final method so it may be overridden");
}
}
class B extends A
{
void display()
{
System.out.println("Overridden method in sub class");
}
/*void show()
{
System.out.println("This method will give error as we cannot override final method");
} */
}
class Main
{
public static void main(String args[])
{
B obj= new B();
obj.display();
obj.show();
}
}

output :  final method in superclass

overridden method in subclass

 

Enquiry about Course

Ask your Question

Click Here

Sharp (2) Tutorials

Video/ C Introduction

Watch video in full size

Video tutorial

Follow by Email
Facebook
Facebook
fb-share-icon
YouTube
Copyright © Sharp Tutorial
Build with WordPress
Wordpress Social Share Plugin powered by Ultimatelysocial
Sharp Tutorial