Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 8

Java Final Keyword

•A java variable can be declared using the keyword final. Then the final variable can be
assigned only once.
•A variable that is declared as final and not initialized is called a blank final variable. A
blank final variable forces the constructors to initialise it.
•Java classes declared as final cannot be extended.!
•Methods declared as final cannot be overridden.
•final parameters – values of the parameters cannot be changed after initialization.
•Java local classes can only reference local variables and parameters that are declared
as final.
•A visible advantage of declaring a java variable as static final is, the compiled java
class results in faster performance. This optimization applies only to primitive types
and String constants, not arbitrary reference types.
•You cannot initialize final static variables inside a constructor.
A final static variable must be definitely initialized either
during declaration  or
in a static initialize (static block) of the class in which it is declared otherwise, it
results in a compile-time error.
public class Demo {
final int x;//blank final
Demo() {
x = 2;
}
Demo(int i) {
x = i;
}
}

public class Demo {


static final int x;//blank final
Demo() {
x = 2; // complier exception:- cannot assign a value to final variable x
}
Demo(int i) {
x = i; //error
}
}
// Fix
public class Demo {
static final int x;//blank final
static{
x=2;
}
// or assign value at declaration time static final int x=2;
}
Interfaces
An interface is a reference type, similar to a class.
An interface is similar to a class in the following ways:
• An interface can contain any number of methods.
• An interface is written in a file with a .java extension, with the name of the interface matching the name
of the file.
• The bytecode of an interface appears in a .class file.
• Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure
that matches the package name.
However, an interface is different from a class in several ways, including:
• You cannot instantiate an interface.
• An interface does not contain any constructors.
• All of the methods in an interface are abstract(Implicitly).
• An interface cannot contain instance fields. The only fields that can appear in an interface must be
declared both static and final(Implicitly).
• An interface is not extended by a class; it is implemented by a class using implements keyword.
• An interface can extend multiple interfaces.
• The interface keyword is used to declare an interface.
• private access modifiers can not use with method in interface.
• All methods of interface must be non static.
• Implemented class must override all the methods of interface or class itself abstract.
public interface TestInterface {
//Any number of final, static fields
//Any number of abstract method declarations
}
interface Animal {

public void eat();


public void voice();
}
class Dog implements Animal{
@Override
public void eat() {
System.out.println("Dog eats veg and non veg");
}
@Override
public void voice() {
System.out.println("Dog is barking");
}
}
When implementation interfaces there are several rules:
•A class can implement more than one interface at a time.
•A class can extend only one class, but implement many interfaces.
•An interface can extend another interface, similarly to the way that a class can extend another
class.
Extending Interfaces:
An interface can extend another interface, similarly to the way that a class can extend another class.
public interface Sports
{
public void setHomeTeam(String name);
public void setVisitingTeam(String name);
}

public interface Football extends Sports


{
public void setHomeTeamGoal(int points);
public void setVisitingTeamGoal(int points);
}
public interface Cricket extends Sports
{
public void setHomeTeamRun(int points);
public void setVisitingTeamRun(int points);
}
Extending Multiple Interfaces:
A Java class can only extend one parent class. Multiple inheritance is not allowed. but
an interface can extend more than one parent interface.
public interface Olympic extends Football, Cricket, Hockey

Abstract Methods and Classes


An abstract class is a class that is declaredx abstract—it may or may not include
abstract methods. Abstract classes cannot be instantiated, but they can be
subclassed.
An abstract method is a method that is declared without an implementation (without
braces, and followed by a semicolon), like this:
abstract void moveTo(double deltaX, double deltaY);
If a class includes abstract methods, then the class itself must be declared abstract, as
in:
public abstract class GraphicObject { // declare fields // declare nonabstract methods
abstract void draw();
}
When an abstract class is subclassed, the subclass usually provides implementations for all of the
abstract methods in its parent class. However, if it does not, then the subclass must also be
declared abstract.

• Abstract Classes Compared to Interfaces


• Abstract classes are similar to interfaces. You cannot instantiate them, and abstract class may
contain a mix of methods declared with or without an implementation.
• In abstract classes, you can declare methods that are not static and final same in interface.
• In abstract classes you can define public, protected, private, static and final concrete
methods but not in interface.
• With interfaces, all fields are automatically public, static, and final, and all methods that you
declare or define (as default methods) are public. In addition, you can extend only one class,
whether or not it is abstract, whereas you can implement any number of interfaces.
Thank You….

You might also like