Unit 4-1
Unit 4-1
Unit 4-1
• The second form of super acts somewhat like this, except that it
always refers to the superclass of the subclass in which it is used.
• This usage has the following general form:
super.member
• member can be either a method or an instance variable.
• This second form of super is most applicable to situations in which
member names of a subclass hide members by the same name in the
superclass.
Example: accessing superclass members using super
keyword
void show() {
class A {
System.out.println("i in superclass: " + sup
int i;
i);
}
System.out.println("i in subclass: " + i);
// B issubclass by extending class A. }
class B extends A { }//endof class B
int i; // this i hides the i in A class UseSuper {
B(int a, int b) { public static void main(String args[]) {
super.i = a; // i in A B subOb = new B(1, 2);
i = b; // i in B subOb.show(); Output:
} } i in superclass: 1
i in subclass: 2
}
Creating a Multilevel Hierarchy
BoxX
BoxWeight
Shipment
• Example
Dynamic Method Dispatch
• Dynamic method dispatch is the mechanism by which a call to an
overridden method is resolved at run time, rather than compile time.
• Dynamic method dispatch implements run-time polymorphism
• When different types of objects are referred to, different versions of
an overridden method will be called.
• The type of the object being referred to (not the type of the
reference variable) that determines which version of an overridden
method will be executed.
• Example
Using Abstract Classes
• Abstract class is a superclass that declares the structure of a given
abstraction without providing a complete implementation of every
method.
• It defines a generalized form that will be shared by all of its
subclasses, leaving it to each subclass to fill in the details.
• This is useful when a superclass is unable to create a meaningful
implementation for a method.
Figure
General form of abstract method declaration: (Abstract class)
abstract type name(parameter-list);
Triangle Rectangle
Using Abstract Classes
• Any class that contains one or more abstract methods must also be
declared abstract.
• To declare a class abstract, abstract keyword is uaed in front of the
class keyword at the beginning of the class declaration.
• There can be no objects of an abstract class.
• That is, an abstract class cannot be directly instantiated with the new
operator.
• Such objects would be useless, because an abstract class is not fully
defined.
• Example
Using final with Inheritance
• The keyword final has three uses.
• First, it can be used to create the equivalent of a named constant.
This use was described in the preceding chapter.
• The other two uses of final apply to inheritance.
• Using final to Prevent Overriding (method level)
• Using final to Prevent Inheritance (class level)
Using final to Prevent Overriding
• final can be used to disallow a method from being overridden
• If final is specified as a modifier at the start of its declaration, such
methods cannot be overridden.
Example:
class A {
final void meth() {
System.out.println("This is a final method.");
}
} class B extends A {
void meth() { // ERROR! Can't override.
System.out.println("Illegal!");
}
}
Special features of final methods (inline)
• 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 only an option 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
• To prevent a class from being inherited final can be used.
• Declaring a class as final implicitly declares all of its methods as final,
too.
• 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.
Example : final class
final class A {
// ...
}
// The following class is illegal.
class B extends A { // ERROR! Can't subclass A
// ...
}