0% found this document useful (0 votes)
5 views5 pages

Java Research 7

The document discusses the use of 'final' in Java to prevent method overriding and inheritance, enhancing performance through early binding and inlining. It also explains local variable type inference in relation to inheritance, emphasizing that inferred types depend on the declared type of the initializer. Additionally, it covers the Object class, which is the superclass of all classes in Java, and highlights key methods like equals() and toString().

Uploaded by

Asim Dinda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Java Research 7

The document discusses the use of 'final' in Java to prevent method overriding and inheritance, enhancing performance through early binding and inlining. It also explains local variable type inference in relation to inheritance, emphasizing that inferred types depend on the declared type of the initializer. Additionally, it covers the Object class, which is the superclass of all classes in Java, and highlights key methods like equals() and toString().

Uploaded by

Asim Dinda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Because meth( ) is declared as final, it cannot be overridden in B.

If
you attempt to do so, a compile-time error will result.
Methods declared as final can sometimes provide a performance
enhancement: The compiler is free to inline calls to them because it
“knows” they will not be overridden by a subclass. When a small final
method is called, often the Java compiler can copy the bytecode for the
subroutine directly inline with the compiled code of the calling method,
thus eliminating the costly overhead associated with a method call.
Inlining is an option only with final methods. Normally, Java resolves
calls to methods dynamically, at run time. This is called late binding.
However, since final methods cannot be overridden, a call to one can be
resolved at compile time. This is called early binding.

Using final to Prevent Inheritance


Sometimes you will want to prevent a class from being inherited. To do
this, precede the class declaration with final. Declaring a class as final
implicitly declares all of its methods as final, too. As you might expect, it
is illegal to declare a class as both abstract and final since an abstract
class is incomplete by itself and relies upon its subclasses to provide
complete implementations.
Here is an example of a final class:
As the comments imply, it is illegal for B to inherit A since A is declared
as final.

Note Beginning with JDK 17, the ability to seal a class was added to Java.
Sealing offers fine grained control over inheritance. Sealing is
described in Chapter 17.

Local Variable Type Inference and


Inheritance
As explained in Chapter 3, JDK 10 added local variable type inference to
the Java language, which is supported by the context-sensitive keyword
var. It is important to have a clear understanding of how type inference
works within an inheritance hierarchy. Recall that a superclass reference
can refer to a derived class object, and this feature is part of Java’s support
for polymorphism. However, it is critical to remember that, when using
local variable type inference, the inferred type of a variable is based on the
declared type of its initializer. Therefore, if the initializer is of the
superclass type, that will be the inferred type of the variable. It does not
matter if the actual object being referred to by the initializer is an instance
of a derived class. For example, consider this program:
In the program, a hierarchy is created that consists of three classes, at
the top of which is MyClass. FirstDerivedClass is a subclass of MyClass,
and SecondDerivedClass is a subclass of FirstDerivedClass. The
program then uses type inference to create three variables, called mc,
mc2, and mc3 by calling getObj( ). The getObj( ) method has a return
type of MyClass (the superclass), but returns objects of type MyClass,
FirstDerivedClass, or SecondDerivedClass, depending on the argument
that it is passed. As the output shows, the inferred type is determined by
the return type of getObj( ), not by the actual type of the object obtained.
Thus, all three variables will be of type MyClass.

The Object Class


There is one special class, Object, defined by Java. All other classes are
subclasses of Object. That is, Object is a superclass of all other classes.
This means that a reference variable of type Object can refer to an object
of any other class. Also, since arrays are implemented as classes, a
variable of type Object can also refer to any array.
Object defines the following methods, which means that they are
available in every object.
The methods getClass( ), notify( ), notifyAll( ), and wait( ) are
declared as final. You may override the others. These methods are
described elsewhere in this book. However, notice two methods now:
equals( ) and toString( ). The equals( ) method compares two objects. It
returns true if the objects are equal, and false otherwise. The precise
definition of equality can vary, depending on the type of objects being
compared. The toString( ) method returns a string that contains a
description of the object on which it is called. Also, this method is
automatically called when an object is output using println( ). Many
classes override this method. Doing so allows them to tailor a description
specifically for the types of objects that they create.
One last point: Notice the unusual syntax in the return type for
getClass( ). This relates to Java’s generics feature, which is described in
Chapter 14.

You might also like