part 4. advanced java topics

24
00 Algorithms and Data Structures Colorado State Universit Part 4. Advanced Java Topics Instructor: Sangmi Pallickara ([email protected] ) Department of Computer Science Colorado State University 1

Upload: giacomo-franklin

Post on 31-Dec-2015

35 views

Category:

Documents


0 download

DESCRIPTION

Part 4. Advanced Java Topics. Instructor: Sangmi Pallickara ( [email protected] ) Department of Computer Science Colorado State University. Outline. Object Oriented Programming Data Encapsulation Inheritance Polymorphism Using Abstract and Interface. Object Oriented Programming. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

1

Part 4.Advanced Java Topics

Instructor: Sangmi Pallickara ([email protected])

Department of Computer Science

Colorado State University

Page 2: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

2

Outline• Object Oriented Programming• Data Encapsulation• Inheritance• Polymorphism• Using Abstract and Interface

Page 3: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

3

Object Oriented Programming• Programming paradigm using “Objects” : data

structures consisting of data fields and methods together with their interaction.

• Object?• Class?• Interface?• Package?

Page 4: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

4

Basic Components• a software bundle of related states

(properties, or variables) and behavior (method)– State is stored in fields

(variables in some

programming languages)– Method exposes

object’s behavior. TimeAlarm time

Set the time

Set the alarmEnable the alarm

Sound the alarmSilence the alarm

Show the time

Properties

Methods

Page 5: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

5

Basic Components• Class: Blueprint from which objects are

created– Multiple Instances created from a class

• Interface: A Contract between classes and the outside the world.– When a class implements an interface, it promises

to provide the behavior published by that interface.

Page 6: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

6

Basic Components• Package: a namespace for organizing classes

and interfaces

Page 7: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

7

Outline• Object Oriented Programming• Data Encapsulation• Inheritance• Polymorphism• Using Abstract and Interface

Page 8: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

8

Data Encapsulation• An ability of an object to be a container (or

capsule) for related properties and methods.– Preventing unexpected change or reuse of the

content

• Data hiding– Object can shield variables from external access.

• Private variables• Public accessor and mutator methods

Page 9: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

9

Data Encapsulationpublic class Clock{

private long time, alarm_time;private String serialNo;

public void setTime(long _time){time = _time;}public void setAlarmTime(long_time){

alarm_time = _time;}public long getTime(){return time}public long getAlarmTime(){return alarm_time}

public void noticeAlarm(){ ring alarm } protected void set serialNo(String _serialNo){…}

}

Page 10: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

10

Outline• Object Oriented Programming• Data Encapsulation• Inheritance• Polymorphism• Using Abstract and Interface

Page 11: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

11

Inheritance• The ability of a class to derive properties from

a previously defined class.• Relationship among classes.• Enables to reuse software components

– E.g. java.lang.Object() – toString(), notifyAll(), equals(), etc.

Page 12: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

12

Example: Inheritance

Page 13: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

13

Example: Inheritance – cont.Public class SportsWatch extends Clock{ private long start_time; private long end_time;

public long getDuration() { return end_time - start_time; }

}

Page 14: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

14

Overriding Methods

public class RadioClock{@override

public void noticeAlarm(){ ring alarm turn_on_the_Radio }}

Page 15: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

15

Java Access Modifiers• Keywords: public, private,and protected

• Control the visibility of the members of a class– Public members can be used by anyone– Members declared without an access modifier are

available to methods of the class and methods of other classes in the same package

– Private members can be used only by methods of the class

– Protected members can be used only by methods of the class, methods of other classes in the same package, and methods of the subclasses.

Page 16: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

16

Outline• Object Oriented Programming• Data Encapsulation• Inheritance• Polymorphism• Using Abstract and Interface

Page 17: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

17

Polymorphism• “Having multiple forms”• Ability to create a variable, or an object that has

more than one form.

Page 18: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

18

Polymorphic method

RadioClock myRadioClock = new RadioClock();Clock myClock = myRadioClock;myClock.notifyAlarm();

Code of this room: DD

A: ClockB. RadioClock

Page 19: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

19

Dynamic Binding• myClock actually references an instance of RadioClock– It will turn on the radio.

• The version of a method “notifyAlarm()” is decided at execution time. (not at compilation time)

Page 20: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

20

Outline• Object Oriented Programming• Data Encapsulation• Inheritance• Polymorphism• Using Abstract and Interface

Page 21: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

21

Abstract• A special kind of class that cannot be

instantiated.• It allows only other classes to inherit from it.• It enforces certain hierarchies for all the

subclasses

Page 22: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

22

Interface• An Interface is NOT a class. • An Interface has NO implementation inside.

– Definitions of methods without body.

Page 23: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

23

Comparison-1Feature Interface Abstract Class

Multiple inheritance

A class may inherit several interfaces

Only one

Default implementation

Cannot provide any code

Can provide complete, default code and/or just the details that have to be overridden.

Access Modifier Cannot have access modifiers. (everything is assumed as public)

Can have it.

Page 24: Part  4. Advanced Java Topics

CS200 Algorithms and Data Structures Colorado State University

24

Comparison-2Feature Interface Abstract Class

Adding functionality ( Versioning)

For a new method, we have to track down all the implementations of the interface and define implementation for the new method

For a new method, we can provide default implementation and all the existing code might work properly.

Fields and Constants

No fields can be defined in interfaces

Fields and constants can be defined