Вы находитесь на странице: 1из 28

IU-VNU-HCM

LOGO

Data Structures and Algorithms


Robert Lafore, Data structures and Algorithms in Java, Waite Group Press, 2002

Lecture 1. Review of OOP and Java

Trong Hai Duong, PhD


Email: haiduongtrong@gmail.com
Mobile phone: 0945090908
Homepage: http://co-intelligence.vn

IU-VNU-HCM

Contents
Oriented-object programming
Object
Class
Inheritance
Interface
Package
Types and Conditionals

Company Logo

Object
The real-world objects: your dog, your
desk, your television set, your bicycle.

IU-VNU-HCM

Company Logo

Object

Bicycles
- state (current gear, current pedal
cadence, current speed)
- behavior (changing gear,
changing pedal cadence, applying
brakes)

IU-VNU-HCM

Company Logo

Object
Software objects are conceptually similar
to real-world objects.
Bicycles
- fields (current gear, current pedal
cadence, current speed)
- methods (changing gear,
changing pedal cadence, applying
brakes)

IU-VNU-HCM

Company Logo

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.

A class is the blueprint from which individual objects


are created.

IU-VNU-HCM

Company Logo

Class
class Bicycle {
int cadence = 0;
int speed = 0;
int gear = 1;
void speedUp(int increment) {
speed = speed + increment;
}

void changeCadence(int newValue) {


cadence = newValue;
}

void applyBrakes(int decrement) {


speed = speed - decrement;
}

void changeGear(int newValue) {


gear = newValue;
}

void printStates() {
System.out.println("cadence:" +
cadence + " speed:" +
speed + " gear:" + gear);
}
}

IU-VNU-HCM

Company Logo

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);
bike1.changeGear(2);
bike1.printStates();

bike2.changeCadence(50);
bike2.speedUp(10);
bike2.changeGear(2);
bike2.changeCadence(40);
bike2.speedUp(10);
bike2.changeGear(3);
bike2.printStates();
}
}

The output of this test prints the ending pedal cadence, speed, and gear for the two bicycles:
cadence:50 speed:10 gear:2
cadence:40 speed:20 gear:3

IU-VNU-HCM

Company Logo

Inheritance
Different kinds of objects often 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;
Some mountain bikes have an additional chain ring,
giving them a lower gear ratio.

IU-VNU-HCM

Company Logo

Inheritance
class MountainBike extends Bicycle {
// new fields and methods
defining
// a mountain bike would go here
}

IU-VNU-HCM

Company Logo

Inheritance
class MountainBike extends Bicycle {
// new fields and methods defining
int chainRing =0;
// a mountain bike would go here
void changeChainRing(int newValue) {
chainRing = newValue;
}
}

The students should complete the program as


lab assignment.

IU-VNU-HCM

Company Logo

Overriding
In object-oriented terms, overriding means to
override the functionality of an existing method.
public class MountainBike extends Bicycle {
// new fields and methods defining
int chainRing =0;
// a mountain bike would go here
public void changeChainRing(int newValue)
{
chainRing = newValue;
}
public void printStates() {
System.out.println("cadence:" +
cadence + " speed:" +
speed + " gear:" + gear + " chang
ring: "+chainRing);
}
}

Company Logo

Abstraction
An abstract class is one that cannot be instantiated.
All other functionality of the class still exists, and its
fields, methods, and constructors are all accessed in
the same manner.
You just cannot create an instance of the abstract
class.

IU-VNU-HCM

Company Logo

Interface
An interface is a collection of abstract methods. A
class implements an interface, thereby inheriting the
abstract methods of the interface.
Unless the class that implements the interface is
abstract, all the methods of the interface need to be
defined in the class.

IU-VNU-HCM

Company Logo

Interface
An interface is similar to a class
However, an interface is different from a class in
several ways, including:
You cannot instantiate an interface.
An interface does not contain any constructors.
All of the methods in an interface are abstract.
An interface cannot contain instance fields. The
only fields that can appear in an interface must be
declared both static and final.
An interface is not extended by a class; it is
implemented by a class.
An interface can extend multiple interfaces.

IU-VNU-HCM

Company Logo

Interface

public class Bicycle implements InterfaceBicycle{


int cadence = 0;
int speed = 0;
int gear = 1;
public void changeCadence(int newValue) {
cadence = newValue;
}
// all abstract methods from the interface should be
implemented here
}

IU-VNU-HCM

Company Logo

Interface

public abstract class Bicycle implements InterfaceBicycle{


int cadence = 0;
int speed = 0;
int gear = 1;
public void changeCadence(int newValue) {
cadence = newValue;
}
// dont require all abstract methods from the interface
define here
// assume that the method applyBrakes was not define
here, the class MountainBike inherits from Bicycle should
implement the applyBrakes
}

IU-VNU-HCM

Company Logo

Interface
public class MountainBike extends Bicycle {
// new fields and methods defining
int chainRing =0;
// a mountain bike would go here
public void changeChainRing(int newValue) {
chainRing = newValue;
}
public void applyBrakes(int decrement) {
speed = speed - decrement;
}
public void printStates() {
System.out.println("cadence:" +
cadence + " speed:" +
speed + " gear:" + gear + " chang ring:
"+chainRing);
}
}

IU-VNU-HCM

Company Logo

Polymorphism
Polymorphism is the ability of an object to take on
many forms. The most common use of polymorphism
in OOP occurs when a parent class reference is used
to refer to a child class object.

IU-VNU-HCM

Company Logo

Polymorphism
public interface InterfaceBicycle{}
public class Bicycle{} implements InterfaceBicycle{}
public class MountainBike extends Bicycle {}

Now, the MountainBike class is considered to be polymorphic since this has multiple
inheritance. Following are true for the above example:
A MountainBike IS-A Bicycle
A MountainBike IS-A InterfaceBicycle
A MountainBike IS-A MountainBike
A MountainBike IS-A Object
When we apply the reference variable facts to a MountainBike object reference,
the following declarations are legal:
MountainBike d = new MountainBike ();
Bicycle a = d;
InterfaceBicycle v = d;
Object o = d;

Company Logo

Encapsulation
Encapsulation is the technique of making the fields in
a class private and providing access to the fields via
public methods.
If a field is declared private, it cannot be accessed by
anyone outside the class, thereby hiding the fields
within the class. For this reason, encapsulation is also
referred to as data hiding.

Company Logo

Package
Packages are used in Java in order to prevent naming
conflicts, to control access, to make searching/
locating and usage of classes, interfaces,
enumerations and annotations easier, etc.
A Package can be defined as a grouping of related
types(classes, interfaces, enumerations and
annotations ) providing access protection and name
space management.

Company Logo

Package
Some of the existing packages in Java are::
java.lang - bundles the fundamental classes
java.io - classes for input , output functions are
bundled in this package
The import Keyword:
If a class wants to use another class in the same
package, the package name does not need to be
used. Classes in the same package find each other
without any special syntax.

Company Logo

Basic Data Types


There are two data types available in Java:
Primitive data types
Reference/Object data types

Company Logo

Primitive Data Types


byte:
Byte data type is an 8-bit signed two's complement integer.
Minimum value is -128 (-2^7)
Maximum value is 127 (inclusive)(2^7 -1)
Default value is 0
Byte data type is used to save space in large arrays, mainly in
place of integers, since a byte is four times smaller than an int.
Example: byte a = 100 , byte b = -50
Short, int, long, float, double, boolean, char,
You can refer to
http://www.tutorialspoint.com/java/java_basic_datatypes.htm
to remind the primitive data types

Company Logo

Primitive Data Types


Byte

Generally, we'll use a byte to represent:


unsigned numeric values in the range 0 => 255
signed numbers in the range -128 => +127
ASCII character codes
other special data types requiring no more than 256 different
values.

Company Logo

Primitive vs. Reference


Object types

Primitive types

Contain a

Reference

Value

How define?

Class definition

Built into java

How create?

new

6, 4.3 true

How initialize?

Constructor

Usually zero

How use?

Methods

Operators (+, -,)

Company Logo

Flow control

while(Boolean_expression)
{
//Statements
}
do
{
//Statements
}while(Boolean_expression);

Вам также может понравиться