Vous êtes sur la page 1sur 24

By

javawithease

Object
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:

Mountain bikes, road bikes, and tandem bikes, for example,


all share the characteristics of bicycles (current speed,
current pedal cadence, current gear).

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.

What are the four OOPs principles and define them?


What is inheritance?
What is Abstraction?
What is multiple inheritance?
What can be the state and behavior of a computer?

In Java Programming ,an identifier is a name of a


variable , a class, or a method.
Identifier starts with a letter, underscore, or dollar
sign($), subsequent character can be digits
Followings are valid identifier:
Identifier
userName
User_name
_sys_var1
$change

not used

**

added in 1.2

***

added in 1.4

There are eight primitive types in Java Programming

Operators are special symbols that perform


specific operations on one, two, or three
operands, and then return a result. There are:
Arithmetic Operators
Unary Operators
Assignment Operator
Bitwise and Bit shift Operator
Relational and Conditional operator

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;

Statements are roughly equivalent to sentences in


natural languages. A statement forms a complete unit of
execution. The following types of expressions can be
made into a statement by terminating the expression
with a semicolon (;).

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 and if-then-else Statements


The switch Statement
The while and do-while Statements
The for Statement

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
}
}

The if-then-else statement provides a secondary path of


execution when an "if" clause evaluates to false.
void applyBrakes()
{
if (isMoving)
{
currentSpeed--;
}
else {
System.err.println("The bicycle has already stopped!");
}
}

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);
}}

Unlike if-then and if-then-else statements, the switch


statement can have a number of possible execution
paths. A switch works with the byte, short, char, and
int primitive data types.

public class SwitchDemo {


public static void main(String[] args) {
int day = 4;
String dayString;
switch (month) {
case 1: dayString = "Sunday";
break;
case 2: dayString = "Monday";
break;
case 3: dayString = "Tuesday";
break;
case 4: dayString = "Wednesday";
break;
case 5: dayString = "Thursday";
break;
case 6: dayString = "Friday";
break;
case 7: dayString = "Saturday";
break;
default:dayString = "Invalid day"; break;
}
System.out.println(dayString);
}
}

Vous aimerez peut-être aussi