• One class can inherit from another, in which case the new class called the subclass or child class carries all the variables and methods of the higher-level class called the superclas
Trang 1CHAPTER 0
Java Programming Basic
Trang 2• CLASSES AND OBJECTS
• OBJECT STATE AND BEHAVIOR
• INSTANCE AND STATIC VARIABLES AND METHODS
• Inheritance
• POLYMORPHISM
Trang 3programming languages in use today
Microsystems, its design takes advantage of modern ideas about what makes a good
programming language
popularity from the very day of its release
Trang 4JAVA TYPES
operates on software objects Object
orientation means that the programmer can define a new type of data element by defining
a new class
can create an example object of the class and manipulate it as a unique object
Trang 5JAVA TYPES
types to get started These Java primitive
types are not objects, but are simply the
definitions of the varieties of data with which all Java programs can work The primitive
types fall into three categories:
1 integral types integers and characters
2 floating-point types fractional numbers
3 boolean type true or false values
Trang 6JAVA TYPES
1 integral types
byte 8 bits wide −128 to 127
short 16 bits wide −32768 to 32767
int 32 bits wide −2 billion to 2 billion
long 64 bits wide −2 63 to (2 63 −1)
char 16 bits wide “Unicode” character codes
Trang 7JAVA TYPES
2 floating-point types
float 32 bits wide +/− 3.4 × 10 38 with 6–7
significant decimal digits double 64 bits wide +/− 1.8 × 10 308 with 14–15
significant decimal digits
3 boolean type
– boolean logical true or false
Trang 8JAVA TYPES - EXAMPLE
public class Primitive {
public static void main( String[] args ) {
Trang 9JAVA TYPES
• Sometimes you will find it necessary to operate
on objects instead of primitive types This is
because objects are reference types, and are
handled differently internally than primitive
types
• For the purpose of converting variables of
primitive types to reference types, Java provides
a set of “wrapper classes” so that the
programmer can always create an object having the same value as a corresponding variable of a primitive type.
Trang 10JAVA TYPES - Primitive Wrapper Class
Trang 11Primitive Wrapper Class - Examples
Integer ix;// Obj of type Integer I
Trang 12values of the same type One speaks of an
array of Strings, or an array of ints
either after the type declaration or after the name:
int[] x;
int y[];
Trang 13create the elements of an array, one must also use the new key word
x = new int[15]; // 15 int
elements, each set to 0
y = new int[10]; // 10 int
elements, each set to 0
changed
Trang 14array of Strings, by convention called args, for accepting any arguments that the user might supply from the command line Ex:
java myProg firstParam secondParam
the program myProg can retrieve the String
“firstParam” from args[0], and the String
“secondParam” from args[1]
Trang 17JAVA IDENTIFIERS
Letters in Java include all lowercase and
uppercase letters, as well as the underscore
character “_” and the currency symbol “$”
and/or digits may follow A Java identifier may
be of any length
Trang 18BASIC CONTROL STRUCTURES
Trang 19BASIC CONTROL STRUCTURES
BufferedReader in = new BufferedReader(
new FileReader( "Students" ) );
for ( int i = 1; i <= numberOfStudents; i++ ) {
String score = in.readLine();
total = total + Double.parseDouble( score ); }
Trang 20BASIC CONTROL STRUCTURES
while & do while
while( <loop condition> ) <statement>
do <loop body>
while( <loop condition> );
Ex
BufferedReader in = new BufferedReader(
new FileReader( "Students" ) );
Trang 21BASIC CONTROL STRUCTURES
switch
switch( <integral expression> ) {
case value_one: <statement_one>
case value_two: <statement_two>
case value_three: <statement_three>
case value_n: <statement_n>
default: <statement_default>
}
Trang 22BASIC CONTROL STRUCTURES
Trang 23OBJECT-ORIENTED PROGRAMMING
means that Java facilitates the writing of
object-oriented programs Object orientation
programming than it is a particular
programming technique When one writes
object-oriented code, one thinks of software
“things” that are analogous to things in the
outside world
Trang 24OBJECT-ORIENTED PROGRAMMING
• The data one operates on and the logic one
encodes may be the same for the procedural
approach and the object-oriented approach, but the object-oriented approach organizes the work differently
• When an object oriented program is complete, a set of classes is the result These classes, if well designed, can be reused and extended more
easily so that future programming projects have a head start.
Trang 25OBJECT-ORIENTED PROGRAMMING
• Putting the code inside classes also allows functionality
to be encapsulated , which leads to more effective
testing and more reliable use later on The software
classes exist as their own entities, with known
properties (attributes) , and well-defined methods
• Using inheritance , the OO approach also offers a
standard way to add functionality without changing at all the code that has already been written There is less temptation to modify the classes that have already
been defined, implemented and tested The result is usually more stable and reliable code
Trang 26OBJECT-ORIENTED PROGRAMMING
programmers think about their work The OO approach leads to software classes which
more closely model the real world, and
to think about In addition, building classes
usually leads to code which is more easily
reused Both effects lead to better
Trang 27CLASSES AND OBJECTS
• A class is a specification, a blueprint, or maybe even a concept, like vehicle
• An instance is a specific example of a class
My Ford with vehicle ID 1FABP64T1JH100161 is said to be
an instance of the class Automobile.
for the word instance One also says that my Ford is an object of the class Automobile
Trang 28• One class can inherit from another, in which case the new class (called the subclass or child class) carries all the variables and methods of the higher-level class
(called the superclass or superior class or parent class).
• The child class can add state variables unique to the
child class, and add behavior methods unique to the child class
• The child class can override methods of the parent
class in order to give the child class different behavior , even though the name of the method implementing the behavior has the same name as the method of the parent class.
Trang 29• These sorts of considerations lead software
developers to create a class hierarchy The
programmer defines the more general state
variables and behavior methods in higher-level classes Then, when writing the subordinant
classes, the programmer uses the inherited state and behavior when it fits, and adds state
variables, methods, and overriding methods to subordinant classes in order to implement
differences between the superior and
subordinant classes.
Trang 30which are written and tested do not change
When the programmer requires new features
simply to meet the new requirements
Trang 31INSTANCE AND STATIC VARIABLES AND
METHODS
information and some behaviors belong to the
class, while others belong to the instances of a class
each Automobile as part of the state of each individual car (part of the state of each
instance)
Trang 32INSTANCE AND STATIC VARIABLES AND
METHODS
count of the total number of Automobiles as part of the state of the class Automobile It
seems natural that speed should be
associated with a particular car, but we need a central place to keep track of the total number
of Automobiles
Trang 33INSTANCE AND STATIC VARIABLES AND
METHODS
• Variables like speed, which represent the state of
an instance of a class, are called instance
variables Variables like count, which are
maintained by the class itself, are called static
variables.
• Similarly, methods can be instance methods or
static methods If two different instances of a
class both call the instance method accelerate(),
it will be as if there were two different copies of that method, and each instance had its own (In reality, there will not be two copies of the code).
Trang 34INSTANCE AND STATIC VARIABLES AND
only one copy, and that is shared.
• Variables and methods will always be instance
variables and methods, unless you specifically
label them static If you label a variable or
method static, the variable or method will exist only at the level of the class, and will be shared among the instances.
Trang 35private String make;
private String model;
private int year;
hp = power;
count++; //add to count of Automobiles created
Trang 36class AutomobileFactory {
public static void main( String[] args ) {
Automobile economy, family, sports;
economy = new Automobile(
System.out.println( family );
System.out.println( sports );
}
}
Trang 37• OO programming makes it easy to add functionality to software without rewriting the code one has already written and tested Suppose we want to add
distinctions between types of Automobiles A Ferrari can go much faster than a Kia, so the accelerate()
method should be different, and perhaps other
behaviors should also be different for such different
Automobiles.
• We can add a new class called SportsCar that will
inherit from Automobile, and we can give the
SportsCar class a different accelerate() method.
Trang 38class SportsCar extends Automobile {
private double maxSpeed = 150.;
//override of inherited accelerate() method
public void accelerate( double newSpeed )
{
if( newSpeed > maxSpeed ) speed = maxSpeed;
else speed = newSpeed;
}
Trang 39Automobile; that means SportsCar inherits
from Automobile Any instance of a SportsCar will also be an Automobile, and except where there are differences between the code for
SportsCar and the code for Automobile, an
instance of a SportsCar will have exactly the same state variables and behavior as any
instance of the class Automobile
Trang 40SportsCar and instances of Automobile will be the behavior provided by the accelerate()
method
Trang 41superior class, the behavior of the method will depend upon which type of object is being
used in the program, an instance of the
superior class or an instance of the subclass This characteristic of OO programming is
called polymorphism, and it is a powerful
feature of the OO approach
Trang 44superior class as we write programs, and if the object being used by the program happens to belong to a subclass of the superior class, the methods in the subclass which override the
corresponding methods in the superior class will insure that methods appropriate to the
Trang 45reduces the amount of code that must be
of existing code in new applications
interface in more details in the following
chapters
Trang 46Examples