1.73M
Category: programmingprogramming

Polymorphism. Lecture 4

1.

Askar Khaimuldin
Senior-lecturer
[email protected]
Astana IT University

2.

Concept of polymorphism
Overriding methods
Polymorphic Behaviour
Type Casting (narrowing conversion)
Abstract class
Interfaces
final methods and classes

3.

Polymorphism means “many forms”
“Program in the general” rather than “program in the specific.”
Polymorphism enables you to write programs that process
objects that share the same superclass, either
directly/indirectly, as if they were all objects of the superclass
Two basic types:
Compile time Polymorphism - Overloading
Runtime Polymorphism - Overriding

4.

Method overriding it is a case of the runtime polymorphism
JVM determines an appropriate method to be called at the
runtime
It is also called dynamic or late binding (Dynamic Method
Dispatch)
Method overriding says child class has the same method as
declared in the parent class
It means if child class provides the specific implementation of the
method that has been provided by one of its parent class, then it is
known as method overriding

5.

It is possible to create a reference of
a base class type which refers to a
concrete object of child class type
This reference (‘animal’) can then be
used to call any method which is
common for every animal
Because the reference itself is of
base class type, it provides only
methods of that base class (e.g.,
Animal)
It means that if a subclass has some
additional methods, it is not available
using that reference of a base class
type

6.

7.

An object of a subclass can be treated as an object of its
superclass, but not vice versa!
Type casting:
Upcasting – casting to a supertype (can be implicit)
Downcasting – casting to a subtype (must be explicit)
Example:
This is redundant, since
upcasting can be done
implicitly
Hint: Whenever you cast to some type,
you must be sure that casting can take
place. Otherwise, ClassCastException
will be thrown at runtime
Although it’s allowed, you should generally avoid downcasting

8.

The implementation of
makeSound() method is not
really making a sound
It was written for the case
when a specific animal`s sound
was not defined
when subclass of animal misses its own
implementation of makeSound()
method
Is it normal to create an object
of Animal?
The class Animal is in fact an
abstraction

9.

“Sometimes it’s useful to declare classes – called abstract
classes – for which you never intend to create objects”
Used only as superclasses in inheritance hierarchies to improve
code reuse and allow polymorphism
Cannot be used to instantiate objects—abstract classes are
incomplete
Subclasses must declare the “missing pieces” to become
“concrete” classes, from which you can instantiate objects.
Otherwise, these subclasses will be abstract too.
An abstract class provides a superclass from which other
classes can inherit and thus share a common design
Classes that can be used to instantiate objects are called
concrete classes

10.

Such classes provide implementations of every method they declare
Abstract superclasses are too general to create real objects – they
specify only what is common among subclasses
You make a class abstract by declaring it with keyword abstract
An abstract class normally contains one or more abstract methods
An abstract method is one with keyword abstract in its declaration, as in
Abstract methods do not provide implementations
A class that contains at least one abstract method must be an abstract
class
Constructors and static methods cannot be declared abstract

11.

It is impossible to instantiate an object
using abstract class
Abstract class may contain both abstract
and non-abstract methods
Abstract class can have constructors and
static methods
The abstract keyword in Java is used to
create
an abstract class
an abstract method

12.

13.

14.

An interface in Java defines a set of services – but without providing an
implementation – it contains only abstract methods
An interface is not a class - so we cannot create objects of an interface
However, an interface defines a type - an interface type - and we can declare
references of this type
A reference of an interface type can denote objects of all classes that
implements the interface
With an interface type reference, we can therefore get access to all services that
the interface defines

15.

Interfaces offer a capability requiring that unrelated
classes implement a set of common methods
Interfaces define and standardize the ways in which
things such as people and systems can interact with
one another
An interface declaration begins with the keyword
interface and contains only constants and abstract
methods
Interface methods are implicitly public and its fields
are implicitly final and static
A class that does not implement at least one
interface method must be an abstract class

16.

17.

18.

final keyword is not only for constants
final Methods Cannot Be Overridden
Methods that are declared static are also implicitly final
final methods are resolved at compile time—this is known as
static binding
A final class cannot be extended to create a subclass
All methods in a final class are implicitly final
Class String is an example of a final class
Hint: unless you carefully design a class for extension, you
should declare the class as final to avoid (often subtle) errors

19.

20.

Java: How to Program (Early Objects), 11th Edition, by Paul Deitel
and Harvey Deitel, Pearson
Chapter 10
English     Русский Rules