Module 7 Study Guide
Module 7 Study Guide
2. Method Overriding
Providing a different implementation of a method in a subclass of the class that originally
defined a method.
Overloading Overriding
Overloaded functions supplement each other. Overriding function replaces the function it
overrides.
Overloaded functions can exist, in any number, Each function in a base class can be overridden
in the same class. at most once in any one derived class.
Overloaded functions must have different Overriding functions must have argument lists
argument lists. of identical type and order.
The return type of an overloaded function may The return type of an overriding method must
be chosen freely. be identical to the function it overrides.
This ability of our reference to change behavior according to what object it is holding is called
polymorphism. Polymorphism allows multiple objects of different subclasses to be treated as
objects of a single superclass, while automatically selecting the proper methods to apply to a
particular object based on the subclass it belongs to.
Abstraction
The Abstract Class and Interface:
Abstract Class – contains one or more abstract methods and, therefore, can never be
instantiated. It is defined so that other classes can extend them and make them
concrete by implementing the abstract methods.
Take Note:
All abstract classes are public by default and cannot be instantiated. Constructors and
public methods cannot be declared as abstract.
An abstract class is a class that cannot be instantiated. It often appears at the top of an object-
oriented programming class hierarchy, defining the broad types of actions possible with objects
of all subclasses of the class.
Those methods in the abstract classes that do not have implementation are called
abstract methods.
Coding Guidelines:
Use abstract classes to define broad types of behaviors at the top of an object-oriented
programming class hierarchy, and use its subclasses to provide implementation details of the
abstract class.
Interfaces
An interface is a special kind of block containing method signatures (and possibly constants)
only. Interfaces define the signatures of a set of methods without the body.
Interfaces define a standard and public way of specifying the behavior of classes. They allow
classes, regardless of their location in the class hierarchy, to implement common behaviors.
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.
Interface – an abstract class that represents a collection of method definitions and constant
values. It can later be implemented by classes that define the interface using the implements
keyword.
Why do we use Interfaces?
We need to use interfaces if we want unrelated classes to implement similar methods.
Thru interfaces, we can actually capture similarities among unrelated classes without artificially
forcing a class relationship.
Another reason for using an object's programming interface is to reveal an object‘s
programming interface without revealing its class.
As we can see later on the section Interface vs. Classes, we can actually use an interface as data
type.
Finally, we need to use interfaces to model multiple inheritance which allows a class to have
more than one superclass.
Multiple inheritance is not present in Java, but present in other object-oriented languages like
C++.
Another common characteristic is that both interface and class can define methods.
However, an interface does not have an implementation code while the class have one.
When your class tries to implement an interface, always make sure that you implement all the
methods of that interface, or else, you would encounter this error,
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.
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.
Another thing to note about the relationship of interfaces to classes is that, a class can only
EXTEND ONE super class, but it can IMPLEMENT MANY interfaces.