Académique Documents
Professionnel Documents
Culture Documents
Classes
Inheritance
Abstraction
Encapsulation
Polymorphism
What Is a Class?
A class is the blueprint from which individual objects
are created. In the real world, you'll often find many
individual objects all of the same kind. 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 bicycles.
What Is Inheritance?
Passing the basic property of a parent class into its child
class is known as Inheritance.
Example:
What Is Abstraction?
Abstraction is the force that hide private implementation
details behind public interfaces. Or You can say that
showing only relevant information is known as
Abstraction.
Example:
Withdrawing money from ATM machine is a good
example of abstraction.
What Is Encapsulation?
Hiding the complexity or wrapping of data into a single
capsule or module is known as Encapsulation.
To achieve encapsulation in your Java program you
should declare all the instance variable as private, and
the method that use these variable as public
What Is Polymorphism?
What Is a Package?
A package is a namespace that organizes a set of related
classes and interfaces. Conceptually you can think of
packages as being similar to different folders on your
computer.
not used
**
added in 1.2
***
added in 1.4
Expressions
An expression is a construct made up of variables,
operators, and method invocations, which are
constructed according to the syntax of the language,
that evaluates to a single value.
Statements
int result = 1 + 2;
Blocks
A block is a group of zero or more statements between
balanced braces and can be used anywhere a single
statement is allowed.
class BlockDemo
{
public static void main(String[] args)
{
boolean condition = true;
if (condition) { // begin block 1
System.out.println("Condition is true.");
} // end block one
else { // begin block 2
System.out.println("Condition is false.");
} // end block 2
}
}
The if-then statement is the most basic of all the control flow
statements. It tells your program to execute a certain section of code
only if a particular test evaluates to true.
void applyBrakes()
{
if (isMoving)
{ // the "if" clause: bicycle must be moving
currentSpeed--; // the "then" clause: decrease current speed
}
}
class IfElseDemo
{
public static void main(String[] args) {
int testscore = 76;
char grade;
if (testscore >= 90) {
grade = 'A';
}
else if (testscore >= 80) {
grade = 'B';
}
else if (testscore >= 70) {
grade = 'C';
}
else if (testscore >= 60) {
grade = 'D';
}
else { grade = 'F'; }
System.out.println("Grade = " + grade);
}}