Module 7 Polymorphism and Abstraction
Module 7 Polymorphism and Abstraction
(Java)
Module 7
Polymorphism and Abstraction
Apply method overriding and method overloading in
changing the default behavior of a given class
7.1
Implementing Polymorphism
1. Method Overloading
Using one method identifier to refer to multiple
functions in the same class, In the Java
programming language, methods can be
overloaded but not variables or operators.
Method Overloading
Ø Constructor Overloading
- creating more than one constructor in a class
Ø Method Overloading
- creating multiple methods having same name in
one class.
public Student(){
…
}
Implementing Polymorphism
2. Method Overriding
Providing a different implementation of a
method in a subclass of the class that originally
defined a method.
7.2
Take Note:
Coding Guidelines:
Note that interfaces exhibit polymorphism as well, since program may call
an interface method and the proper version of that method will be
executed depending on the type of object passed to the interface method
call.
As we can see here, both of the classes have some similar methods which
compares them from other objects of the same type, but they are not
related whatsoever.
As we can see later on the section Interface vs. Classes, we can actually use
an interface as data type.
Coding Guidelines:
Use interfaces to create the same standard method definitions in may
different classes.
Once a set of standard method definition is created, you can write a
single method to manipulate all of the classes that implement the
interface.
Polymorphism and Abstraction
Relationship of an Interface to a Class
As we have seen in the previous section, a class can implement an interface
as long as it provides the implementation code for all the methods defined
in the interface.
Take note that an interface is not part of the class inheritance hierarchy.
Unrelated classes can implement the same interface