48
1 Introduction and Review of Java: Part 2 Fundamental Concepts (Principles) of Object Oriented Programming Java’s implementation of Object Oriented Programming

Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

  • Upload
    doannhu

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

1

Introduction and Review of Java: Part 2

• Fundamental Concepts (Principles) of Object Oriented Programming

• Java’s implementation of Object Oriented Programming

Page 2: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

2

JAVA OO PROGRAMMING Object – An object has state and behavior.

Class – A class is a blueprint from which objects are created.

Inheritance – Inheritance provides a powerful and natural mechanism for

organizing and structuring your software. Interface – An interface is a contract between a class and the outside world.

Package – A package is a namespace for organizing classes and interfaces in a

logical manner.

Page 3: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

3

WHAT IS AN OBJECT? Object Oriented Technology views software in the same way

we view real-world objects:

• Real-world objects share two characteristics – They all have state and behavior.

• Examples of real-world objects – Dogs – State: name, color, breed, hungry – Behavior: barking, fetching, wagging tail.

– Bicycles – State: current gear, current pedal cadence, current speed – Behavior: changing gear, changing pedal cadence, applying

brakes.

Page 4: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

4

WHAT IS AN OBJECT? – Desktop radio

– States: on, off, current volume, current station – Behavior: turn on, turn off, increase volume, decrease volume,

seek, scan, and tune.

• Some objects will also contain other objects • Example: Automobiles

• States: color, make, model, Transmission, weight, etc. • Behavior: start, brake, increase speed, etc. • Transmission

• States: park, reverse, drive, etc. • Behavior: change gears, increase speed, etc.

Page 5: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

5

A SOFTWARE OBJECT IN JAVA Software objects consist of state and related behavior

– Stores its state in fields (variables) – Exposes its behavior through methods.

Methods

– Operate on an object’s internal state, and

– Serve as the primary mechanism for object-to-object communication.

Data Encapsulation – Hiding internal state and requiring all interaction to be performed through

an object’s methods is known as data encapsulation — a fundamental principle of object-oriented programming.

Page 6: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

6

A SOFTWARE OBJECT REPRESENTATION OF A BICYCLE

• The object remains in control of how the outside world is allowed to use it.

• For example, if the bicycle only has 6 gears, a method to change gears could reject any value that is less than 1 or greater than 6.

Page 7: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

7

BENEFITS OF OBJECT ORIENTATION

Modularity

– The source code for an object can be written and maintained independently of the source code for other objects.

– Once written, the source code for an object can be easily passed around inside the system.

Information-hiding

– By interacting only with an object’s methods, the details of its internal

implementation remain hidden from the outside world

– Reduces complexity.

– The details of its internal implementation can be changed without affecting other objects that use it.

Page 8: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

8

BENEFITS OF OBJECT ORIENTATION

Data Encapsulation

– Only the object's own methods can directly inspect or manipulate its fields.

– Decreases complexity because we don’t have to worry about any code changing the internal state of an object, except for the object’s own methods.

– Not enforced in Java, but considered good OO programming practice.

Code re-use

– A fundamental principle of software engineering is “basing software development of re-usable technology”.

– Reusing already tested/debugged objects leads to higher quality software.

Page 9: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

9

BENEFITS OF OBJECT ORIENTATION

Plug-ability and debugging ease

– Because objects are self-contained and independent, objects of the same type and same signature can be interchanged in an application very simply.

– If you discover an object that is more efficient than yours, you can replace your object with the more efficient object, provided the signature is the same.

– If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement.

– EX: If a bolt breaks, you replace it, not the entire machine.

Page 10: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

10

WHAT IS A CLASS? In the real world, you’ll often find many individual objects all of the

same kind.

There may be thousands of other bicycles in existence, all of the same make and model.

Each bicycle was built from the same set of blueprints and therefore contains the same components.

In object-oriented terms, we say that your bicycle is an instance of the class of objects known as Bicycle.

A class is the blueprint from which individual objects are created.

Page 11: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

11

JAVA CODE FOR Bicycle CLASS class Bicycle {

int cadence = 0;

int speed = 0;

int gear = 1;

void changeCadence(int newValue) {

cadence = newValue;

}

void changeGear(int newValue) {

gear = newValue;

}

void speedUp(int increment) {

speed = speed + increment;

}

void applyBrakes(int decrement) {

speed = speed - decrement;

}

void printStates() {

System.out.println("cad:"+cadence+" spd:"+speed+" gear:"+gear);

}

}

Page 12: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

12

Bicycle CLASS DECLARATION class Bicycle {

int cadence = 0;

int speed = 0;

int gear = 1;

void changeCadence(int newValue) {

cadence = newValue;

}

void changeGear(int newValue) {

gear = newValue;

}

void speedUp(int increment) {

speed = speed + increment;

}

void applyBrakes(int decrement) {

speed = speed - decrement;

}

void printStates() {

System.out.println("cadence:"+cadence+" speed:"+speed+" gear:"+gear);

}

}

Page 13: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

13

Bicycle CLASS FIELDS: STATE class Bicycle {

int cadence = 0;

int speed = 0;

int gear = 1;

void changeCadence(int newValue) {

cadence = newValue;

}

void changeGear(int newValue) {

gear = newValue;

}

void speedUp(int increment) {

speed = speed + increment;

}

void applyBrakes(int decrement) {

speed = speed - decrement;

}

void printStates() {

System.out.println("cadence:"+cadence+" speed:"+speed+" gear:"+gear);

}

}

Page 14: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

14

Bicycle CLASS METHODS: BEHAVIOR class Bicycle {

int cadence = 0;

int speed = 0;

int gear = 1;

void changeCadence(int newValue) {

cadence = newValue;

}

void changeGear(int newValue) {

gear = newValue;

}

void speedUp(int increment) {

speed = speed + increment;

}

void applyBrakes(int decrement) {

speed = speed - decrement;

}

void printStates() {

System.out.println("cadence:"+cadence+" speed:"+speed+" gear:"+gear);

}

}

Page 15: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

15

Bicycle DEMO CLASS APPLICATION THAT INTERACTS WITH THE Bicycle CLASS

class BicycleDemo {

public static void main(String[] args) {

// Create two different Bicycle objects

Bicycle bike1 = new Bicycle();

Bicycle bike2 = new Bicycle();

// Invoke methods on those objects

bike1.changeCadence(50);

bike1.speedUp(10);

bike2.changeCadence(50);

}

}

Page 16: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

16

UML REPRESENTATION OF A CLASS

Bicycle

gear speed

changeGear speedUp

Name of class

List of Fields

List of Methods

Page 17: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

17

UML CLASSES AND ASSOCIATION

Bicycle

gear speed

changeGear speedUp

BicycleDemo

main()

BicycleDemo has a reference to Bicycle (indicated by the

arrow).

Bicycle does NOT have a reference to BicycleDemo

(indicated by the lack of an arrow).

Line of Association.

Page 18: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

18

UML SEQUENCE DIAGRAM EXAMPLE: OBJECTIVE: TO SHOW HOW BicyleDemo CREATES AND SENDS MESSAGES TO Bicycle OBJECTS.

BicycleDemo

mai

n()

java BicycleDemo

User

create

Bicycle: bike1

Bicycle: bike2

create

changeCadence(50)

speedUp(10)

changeCadence(50)

Page 19: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

19

UML NOTES: CLASS DIAGRAM

• Shows a static view of the program (system)

• It describes what classes are in the system and how they are related (association).

• Abstracted view of system

• Need to identify the objective for showing a class diagram

• Not all classes or components of classes need to be shown

• Show only the items which are needed to get your point (objective) across.

Page 20: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

20

UML NOTES: SEQUENCE DIAGRAM

• Shows a dynamic view of the program (system)

• It describes the interaction between items in the system

• Items can be users, classes, objects, etc.

• Abstracted view of system

• Need to identify the objective for showing a sequence diagram

• Not all items in the system need to be shown

• Show only the items which are needed to get your point (objective) across.

Page 21: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

21

WHAT IS INHERITANCE?

• Consider the blueprints (classes) of different types of bicycles. • Examples: Tandem, Road, and Mountain bikes

• The objects of these different types have a certain amount in common with each other.

• Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles (current speed, current pedal cadence, current gear).

• Yet each also defines additional features that make them different

– Tandem bicycles have two seats and two sets of handlebars

– Road bikes have drop handlebars

– Mountain bikes have an additional chain ring: for lower gear ratio.

• It would be redundant to duplicate the code to describe the common fields and common behavior of these different types of bicycles.

Page 22: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

22

WHAT IS INHERITANCE?

Object-oriented programming allows classes to inherit commonly used state and behavior from other classes.

For example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike.

Page 23: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

23

UML REPRESENTATION OF INHERITANCE

Bicycle

gear

changeGear

MountianBike

chainRing

changeChainRing

RoadBike

handleBarPos

changeHandleBarPos

TandemBike

seat

Closed, non-filled, solid line triangle points to the

superclass

Superclass

Subclass Subclass Subclass

Page 24: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

24

INHERITANCE SYNTAX

At the beginning of your class declaration, use the extends keyword, followed by the name of the class to inherit from:

class MountainBike extends Bicycle {

// New fields and new methods that make

// a mountain bike different than a

// Bicycle would go here.

}

This gives MountainBike all the same fields and methods as Bicycle, yet allows its code to focus exclusively on the features that make MountainBike unique.

Page 25: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

25

INHERITANCE EXAMPLE

class MountainBike extends Bicycle {

int seatHeight;

public void setHeight(int newValue) {

seatHeight = newValue;

}

public int getHeight() {

return seatHeight;

}

}

• MountainBike inherits all the fields and methods of Bicycle and adds the field seatHeight and methods to get and set it.

Page 26: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

26

INHERITANCE RULES Java programming language specific rules

– Each class is allowed to have a maximum of one direct superclass.

– To minimize complexity

– Each superclass has the potential for an unlimited number of subclasses.

Good Object Oriented practice rules

– Ensure each subclass obeys the 1. “ISA” rule

2. Distinctiveness rule

3. Rule that all inherited features make sense in each subclass.

Page 27: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

27

INHERITANCE RULES: ISA RULE

Ensure each subclass obeys the “ISA” rule

– A chequing account is an account.

– A village is a municipality.

– A mountain bike is a bike.

Should Province be a subclass of Country?

– No, a Province is not a Country.

– Modeling a Province as a Country violates the ISA rule.

– Modeling a Province as Country would artificially introduce complexity into the source code, making the source code more difficult to understand, maintain, and use.

Page 28: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

28

INHERITANCE RULES: DISTINCTIVENESS RULE

A subclass must retain its distinctiveness throughout its life.

– Consider a bike with training wheels, for teaching kids to ride.

– You might consider creating a class called TrainingBicycle, and making this a subclass of Bicycle.

– However, a training wheel bicycle will not be a training bike once the training wheels are removed.

– Therefore, TrainingBicycle is not a subclass of Bicycle.

– Modeling a TrainingBicycle as a Bicycle would artificially introduce complexity into the source code, making the source code more difficult to understand, maintain, and use.

– Actually, TrainingBicycle should not even be a class. – It’s better to have a field that indicates if a Bicycle has training wheels.

Page 29: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

29

INHERITANCE RULES: MSFMS

Make Sure that each Feature of a superclass Makes Sense in each subclass.

Each subclass inherits the features of a superclass.

Modeling a class as a subclass of some superclass with one or more features that do not make sense in the subclass would artificially introduce complexity into the source code, making the source code more difficult to understand, maintain, and use.

Page 30: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

30

ADVANTAGES OF INHERITANCE

• Only one copy of the common features of a set of subclasses is coded: they are coded only in the superclass

• No duplication of code

• Avoids errors of copying the features to each of the subclasses

• More flexible for change, for examples:

• Replacing a more efficient method in the superclass means each of the subclasses will also have the benefit of the more efficient method, since it is inherited

• Adding/removing fields to/from the superclass affects all classes underneath the superclass

Page 31: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

31

WHAT IS AN INTERFACE? • Objects define their interaction with the outside world through the

methods that they expose.

• Methods form the object’s interface with the outside world.

• For example,

– The buttons on the front of your television set are the interface between you and the electrical, electronic, and digital circuits on the other side of its plastic casing.

– You press the “power” button to turn the television on and off.

Page 32: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

32

MOTIVATION FOR HAVING AN INTERFACE

1. The main reason for having an interface is CONVENIENCE.

• Suppose you have written several re-usable classes that offer a main point of functionality, which is implemented by a set of primary methods, but that the classes had other methods which are either supporting methods or for another purpose

• You want to give other developers easier access to the primary methods of the main point of functionality.

• It would be good if: • Developers did not have to search through your set of classes and

determine what are the desired primary methods

• There was a mechanism to publish (make known) the primary methods in a single container (Interface)

• This would be more convenient.

Page 33: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

33

MOTIVATION FOR HAVING AN INTERFACE

2. Easier to replace objects in the system

• The required methods an object needs to provide to a system is formalized and published in the interface

• The interface or set of interfaces could be the only connection the object has with the other objects in the system

• Therefore, to replace an object with a new one, the new object need only implement the required interface.

Page 34: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

34

JAVA INTERFACE Java provides such a mechanism, called an interface.

An interface is a formal specification of a set of related methods that a class (or a group of classes) provides and implements.

In Java, an interface is a group of related methods with empty bodies.

A class (or group of classes) provides the implementation of the methods specified in the interface.

Page 35: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

35

INTERFACE: BICYCLE EXAMPLE

• To specify (or advertise) the methods that the Bicycle hierarchy provides to effect speed (i.e., a main point of functionality), consider creating an interface called BicycleSpeed.

interface BicycleSpeed {

void setCadence(int newValue);

void setGear(int newValue);

void speedUp(int increment);

}

• Now, a developer need not search the Bicycle hierarchy for the methods that effect speed: that information is contained in the BicycleSpeed interface.

• Note: there is no body for each method in the interface, as required. Methods with no bodies are called abstract methods.

Page 36: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

36

INTERFACE: BICYCLE EXAMPLE

To implement this interface, use the implements keyword in the class declaration.

class Bicycle implements BicycleSpeed {

void setCadence(int newValue){

//code for changing the cadence.

}

void setGear(int newValue){

// code for changing the gear.

}

//Other methods

}

interface BicycleSpeed {

void setCadence(int newValue);

void setGear(int newValue);

}

Page 37: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

37

UML REPRESENTATION OF INTERFACE

Bicycle

setCadence setGear

MountainBike

chainRing changeChainRing

RoadBike

handleBarPos changeHandleBarPos

TandemBike

seat

Closed, non-filled, dashed line triangle

points to the interface

«interface»

BicycleSpeed setCadence

setGear

This class hierarchy implements the

interface “BicycleSpeed”.

Dashed line connects the class with the interface.

Interface

Page 38: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

38

SIMPLE APPLICATION PURPOSE: TO CHANGE SPEED OF A MOUNTAIN BIKE

Bicycle

setCadence setGear

«interface»

BicycleSpeed

setCadence setGear

MountainBike TandemBike …

BicycleDemo

User of the Bicycle hierarchy for the sole purpose of changing

speed of a mountain bike. Assume Bicycle Hierarch is very

Complex

Page 39: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

39

Bicycle CLASS IN FILE Bicycle.java

public class Bicycle implements BicycleSpeed {

// the Bicycle class has three fields

public int cadence;

public int gear;

public int speed;

// the Bicycle class has one constructor

public Bicycle(int startCadence, int startSpeed,

int startGear)

{

gear = startGear;

cadence = startCadence;

speed = startSpeed;

}

Page 40: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

40

Bicycle CLASS (CONTINUED) // the Bicycle class has four methods

public void setCadence(int newValue) {

cadence = newValue;

}

public void setGear(int newValue) {

gear = newValue;

}

public void applyBrake(int decrement) {

speed -= decrement;

}

public void speedUp(int increment) {

speed += increment;

}

}

Page 41: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

41

MountainBike CLASS IN FILE BountainBike.java

public class MountainBike extends Bicycle {

// the MountainBike subclass adds one field

public int seatHeight;

// the MountainBike subclass has one constructor

public MountainBike(int startHeight, int startCadence,

int startSpeed, int startGear) {

super(startCadence, startSpeed, startGear);

seatHeight = startHeight;

}

// the MountainBike subclass adds one method

public void setHeight(int newValue) {

seatHeight = newValue;

}

}

Page 42: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

42

BicycleSpeed INTERFACE IN FILE BicycleSpeed.java

interface BicycleSpeed {

void setCadence(int newValue);

void setGear(int newValue);

void speedUp(int increment);

}

Page 43: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

43

BicycleDemo CLASS IN FILE BicycleDemo.java

public class BicycleDemo{

public static void main(String[] args) {

MountainBike myMountainBike = new MountainBike(1, 2, 3, 4);

BicycleSpeed speedOfmyMountainBike;

speedOfmyMountainBike = myMountainBike;

speedOfmyMountainBike.setCadence(5);

speedOfmyMountainBike.setGear(6);

System.out.printf("gear is %d%n", myMountainBike.gear);

System.out.printf("cadence is %d%n", myMountainBike.cadence);

}

}

«interface» BicycleSpeed

setCadence setGear

BicycleDemo

Bicycle

MountainBike

Page 44: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

44

JAVA INTERFACE RULES • Each class is allowed to implement any number of interfaces.

• Each method specified in an interface cannot have any implementation.

– Each method in an interface must have an empty body.

• Each method specified in an interface must be implemented by some class in the source code.

• If a class claims to implement an interface, all methods defined by that interface must appear in the class’s source code.

– Otherwise the class will not compile successfully.

Page 45: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

45

WHAT IS A PACKAGE? • A package is a container in which related classes and interfaces are

grouped together.

• Conceptually you can think of packages as being similar to different folders on your computer.

– You might keep HTML pages in one folder, images in another, and scripts or applications in yet another.

• For large programs which are composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.

Page 46: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

46

JAVA’S PACKAGES: API • The Java platform provides an enormous class library (a set of

packages) suitable for use in your own applications.

• This library is known as the “Application Programming Interface” (API).

• The packages within the API represent the tasks most commonly associated with general-purpose programming.

Page 47: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

47

EXAMPLES OF JAVA PACKAGES

java.lang

– The String, Integer, Double and other related classes are contained within the “java.lang” package.

– For example, the Integer class contains state and behavior for common integer tasks, such as

– Converting a string to an Integer object.

java.io

– a File object allows a programmer to easily create, delete, inspect, or modify a file on the file system. The File and other related classes are contained within the “java.io” package.

Page 48: Introduction and Review of Java: Part 2ece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides/L03... · Introduction and Review of Java: Part 2 ... –Mountain bikes have an additional

48

EXAMPLES OF JAVA PACKAGES

java.net

– a Socket object allows for the creation and use of network sockets. The Socket and other related classes are contained within the “jave.net” package.

java.awt

– various GUI objects control buttons and checkboxes and anything else related to graphical user interfaces. The GUI related classes are contained within the “java.awt” package.

There are many other packages and literally thousands of classes to choose from. This allows you, the programmer, to focus on the design of your particular application, rather than the infrastructure required to make it work.