Академический Документы
Профессиональный Документы
Культура Документы
LOGO
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.
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 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;
}
}
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
IU-VNU-HCM
Company Logo
Interface
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
Company Logo
Company Logo
Company Logo
Primitive types
Contain a
Reference
Value
How define?
Class definition
How create?
new
6, 4.3 true
How initialize?
Constructor
Usually zero
How use?
Methods
Company Logo
Flow control
while(Boolean_expression)
{
//Statements
}
do
{
//Statements
}while(Boolean_expression);