Module 3
Module 3
Module 3
Integrative Coding
At the end of this module you are expected to:
• Defined the importance of using design patterns and list the motivation for using
each of the following design patterns.
• Described what a programming interface is and why it is important to
programming and give an example of where the use of a programming interface
simplified the development of a system.
• Defined the concept of inheritance and describe how it can be applied to
encourage code reuse.
• Designed an abstract class and use inheritance to create a class that extends the
abstract class.
Introduction
Design patterns
1. Creational patterns: used to help make a system independent of how its objects
are created, composed and represented.
2. Structural patterns: concerned with how classes and objects are organized and
composed to build larger structures.
3. Behavioral patterns: used to deal with assignment of responsibilities to objects
and communication between objects.
Creational Patterns
Behavioral Patterns
INTERFACES
• Are sets of requirements that govern how one application can talk to another
• Applications to share data and take actions on one another’s behalf without
requiring developers to share all of their software’s code
• Define exactly how a program will interact with the rest of the software world –
saving time and resources
Examples:
System-level APIs. Cut and paste LibreOffice document into an Excel
spreadsheet
Facebook APIs. Facebook users sign into many apps and websites using
their Facebook ID
Web APIs. Games let players chat, post high scores and invite friends to
play via Facebook, right there in the middle of the game.
Why And When To Use Interfaces?
1) To achieve security - hide certain details and only show the important details of an
object (interface).
2) Java does not support "multiple inheritance" (a class can only inherit from one
superclass). However, it can be achieved with interfaces, because the class
can implement multiple interfaces. Note: To implement multiple interfaces, separate
them with a comma (see example below).
Multiple Interfaces
interface FirstInterface {
interface SecondInterface {
System.out.println("Some text..");
class MyMainClass {
myObj.myMethod();
myObj.myOtherMethod();
INHERITANCE
Types of Inheritance
• Abstract Method: a method with only signature (i.e.,the method name, the list of
arguments and the return type) without implementation (i.e., the method’s body)
• Use the keyword abstract to declare and abstract method
Abstract Class
Notes:
@Override
System.out.println("Punch!");
@Override
public void executeAction() {
System.out.println("Fly!");
dukeMascot.executeAction();
juggyMascot.executeAction();
Punch!
Fly!
Because of their specific implementations, both Duke and Juggy’s actions will be
executed.
Another example:
class Animal {
class MyMainClass {
myPig.animalSound();
myDog.animalSound();
Interfaces
An interface is a completely "abstract class" that is used to group related methods with
empty bodies:
Example
// interface
interface Animal {
}
To access the interface methods, the interface must be "implemented" (kinda like
inherited) by another class with the implements keyword (instead of extends). The body of
the interface method is provided by the "implement" class:
Example
// Interface
interface Animal {
System.out.println("Zzz");
}
class MyMainClass {
myPig.animalSound();
myPig.sleep();