Title: Object Oriented Programming
1Object Oriented Programming
2Topics To Be Covered Today
- Method Overriding
- Abstract Classes
- 3 uses of Final
3Method Overriding
- 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
4Example Hiding with Overriding
5Example Hiding with Overriding
6Example Hiding with Overriding
- 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.
7Super and Overriding
- The hidden super-class method may be invoked
using super - The super-class version of show() is called
within the sub-classs version.
8Overriding versus Overloading
- 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
9Overriding versus Overloading
- The show() method in B takes a String parameter,
while the show() method in A takes no parameters
10Overriding versus Overloading
- The two invocations of show() are resolved
through the number of arguments (zero versus one)
11Dynamic Method Invocation
- 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.
12Example Dynamic Invocation
13Example Dynamic Invocation
- Two sub-classes B and C
- B and C override the As callme() method.
14Example Dynamic Invocation
- 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
15Polymorphism again
- 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.
16Example Polymorphism
- A class that stores the dimensions of various
2-dimensional objects
17Example Polymorphism
- Rectangle is a sub-class of Figure
18Example Polymorphism
- Triangle is a sub-class of Figure
19Example Polymorphism
- Invoked through the Figure variable and
overridden in their respective subclasses, the
area() method returns the area of the invoking
object
20Abstract Method
- 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.
21Example Abstract Method
- The area method cannot compute the area of an
arbitrary figure - Instead, area should be defined abstract in
Figure
22Abstract Class
- 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.
23Abstract Sub-class
- A sub-class of an abstract class
- implements all abstract methods of its
super-class, or - is also declared as an abstract class
24Abstract and Concrete Class
- Abstract super-class, concrete sub-class
25Abstract and Concrete Class
- Calling concrete and overridden abstract methods
26Example Abstract Class
- Figure is an abstract class it contains an
abstract area method
27Example Abstract Class
- Rectangle is concrete it provides a concrete
implementation for area
28Example Abstract Class
- Triangle is concrete it provides a concrete
implementation for area
29Example Abstract Class
- Invoked through the Figure variable and
overridden in their respective subclasses, the
area() method returns the area of the invoking
object
30Abstract Class Reference
- 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())
31Uses of Final
- 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
32Preventing Overriding with final
- A method declared final cannot be overridden in
any sub-class - This class declaration is illegal
33Final and Early Binding
- 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.
34Preventing Inheritance with final
- 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.
35Object Class
- 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.
36Questions?