You are on page 1of 36

 Method Overriding

 Abstract Classes
 3 uses of Final
 When a method of a sub-class has the same
name and type as a method of the super-class, we
say that this method is overridden.
 When an overridden method is called from within
the sub-class:
◦ it will always refer to the sub-class method
◦ super-class method is hidden
 When show() is invoked on an object of type B,
the version of show() defined in B is used:

 The version of show() in A is hidden through


overriding.
 The hidden super-class method may be invoked
using super:

 The super-class version of show() is called within


the sub-class’s version.
 Method overriding occurs only when the names
and types of the two methods (super-class and
sub-class methods) are identical.
 If not identical, the two methods are simply
overloaded:
 The show() method in B takes a String parameter,
while the show() method in A takes no parameters:
 The two invocations of show() are resolved
through the number of arguments (zero versus
one):
 Overriding is the basis for dynamic method dispatch – a
call to an overridden method is resolved at run-time,
rather than compile-time.
 Method overriding allows for dynamic method
invocation:
◦ an overridden method is called through the super-class
variable
◦ Java determines which version of that method to execute
based on the type of the referred object at the time the
call occurs
◦ when different types of objects are referred, different
versions of the overridden method will be called.
 A super-class A:
 Two sub-classes B and C:

 B and C override the A’s callme() method.


 Overridden method is invoked through the variable
of the super-class type.
 Each time, the version of the callme() method
executed depends on the type of the object being
referred to at the time of the call:
 One interface, many behaviors:
◦ super-class defines common methods for sub-classes
◦ sub-class provides specific implementations for some
of the methods of the super-class
 A combination of inheritance and overriding –
sub-classes retain flexibility to define their own
methods, yet they still have to follow a
consistent interface.
 A class that stores the dimensions of various 2-
dimensional objects:
 Rectangle is a sub-class of Figure:
 Triangle is a sub-class of Figure:
 Invoked through the Figure variable and
overridden in their respective subclasses, the
area() method returns the area of the invoking
object:
 Inheritance allows a sub-class to override the
methods of its super-class.
 In fact, a super-class may altogether leave the
implementation details of a method and declare
such a method abstract:
 abstract type name(parameter-list);
 Two kinds of methods:
◦ concrete – may be overridden by sub-classes
◦ abstract – must be overridden by sub-classes
 It is illegal to define abstract constructors or static
methods.
 The area method cannot compute the area of an
arbitrary figure:

 Instead, area should be defined abstract in Figure:


 A class that contains an abstract method must be
itself declared abstract:

 An abstract class has no instances - it is illegal to


use the new operator:

 It is legal to define variables of the abstract class


type.
 A sub-class of an abstract class:
◦ implements all abstract methods of its super-class, or
◦ is also declared as an abstract class
 Abstract super-class, concrete sub-class:
 Calling concrete and overridden abstract methods:
 Figure is an abstract class; it contains an abstract
area method:
 Rectangle is concrete – it provides a concrete
implementation for area:
 Triangle is concrete – it provides a concrete
implementation for area:
 Invoked through the Figure variable and
overridden in their respective subclasses, the
area() method returns the area of the invoking
object:
 It is illegal to create objects of the abstract class:
 Figure f = new Figure(10, 10);
 It is legal to create a variable with the abstract
class type:
 Figure figref;
 Later, figref may be used to assign references to
any object of a concrete sub-class of Figure (e.g.
Rectangle) and to invoke methods of this class:
 Rectangle r = new Rectangle(9, 5);
 figref = r;
 System.out.println(figref.area());
 The final keyword has three uses:
◦ declare a variable which value cannot change after
initialization
◦ declare a method which cannot be overridden in
sub-classes
◦ declare a class which cannot have any sub-
classes
 A method declared final cannot be overridden in
any sub-class:

 This class declaration is illegal:


 Two types of method invocation:
◦ early binding – method call is decided at compile-time
◦ late binding – method call is decided at run-time
 By default, method calls are resolved at run-
time.
 As a final method cannot be overridden, their
invocations are resolved at compile-time. This is
one way to improve performance of a method
call.
 A class declared final cannot be inherited – has no
sub-classes.
 final class A { … }
 This class declaration is considered illegal:
 class B extends A { … }
 Declaring a class final implicitly declares all its
methods final.
 It is illegal to declare a class as both abstract and
final.
 Object class is a super-class of all Java classes:
◦ Object is the root of the Java inheritance hierarchy.
◦ A variable of the Object type may refer to objects of any
class.
◦ As arrays are implemented as objects, it may also refer
to any array.
Questions?

You might also like