0% found this document useful (0 votes)
12 views

Module 7 Study Guide

Uploaded by

202310943
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Module 7 Study Guide

Uploaded by

202310943
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Module 7 Study Guide

Polymorphism and Abstraction

Polymorphism is the ability of objects belonging to different types to respond to


methods of the same name, each one according to the right type-specific behavior.
It is the ability to redefine methods for derived classes.
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.

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.

Syntax for Declaring Abstract Class:

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++.

Interface vs. Abstract Class


The following are the main differences between an interface and an abstract class:
interface methods have no body,
an interface can only define constants and
an interface have no direct inherited relationship with any particular class, they are defined
independently.

Interface vs. Class


One common characteristic of an interface and class is that they are both types. This means
that an interface can be used in places where a class can be used.
For example, given a class Person and an interface PersonInterface, the following declarations
are
valid:
PersonInterface pi = new Person();
Person pc = new Person();
However, you cannot create an instance from an interface. An example of this is:
PersonInterface pi = new PersonInterface(); //COMPILE
//ERROR!!!

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.

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.
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.

Interfaces are not part of the class hierarchy.


However, interfaces can have inheritance relationship among themselves.

You might also like