java oops and fundamentals

24
By javawithease

Upload: javaease

Post on 15-Jul-2015

167 views

Category:

Technology


6 download

TRANSCRIPT

Page 1: Java oops and fundamentals

Byjavawithease

Page 2: Java oops and fundamentals

Object Classes Inheritance Abstraction Encapsulation Polymorphism

Page 3: Java oops and fundamentals

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.

Page 4: Java oops and fundamentals

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).

Page 5: Java oops and fundamentals

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.

Page 6: Java oops and fundamentals

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

Page 7: Java oops and fundamentals

What Is Polymorphism?

Page 8: Java oops and fundamentals

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.

Page 9: Java oops and fundamentals
Page 10: Java oops and fundamentals

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?

Page 11: Java oops and fundamentals
Page 12: Java oops and fundamentals

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

Page 13: Java oops and fundamentals

 * not used

 ** added in 1.2

 *** added in 1.4

 **** added in 5.0

Page 14: Java oops and fundamentals

There are eight primitive types in Java Programming

Page 15: Java oops and fundamentals

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

Page 16: Java oops and fundamentals
Page 17: Java oops and fundamentals

ExpressionsAn 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.

int result = 1 + 2;Statements

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 (;).

Page 18: Java oops and fundamentals

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 }

}

Page 19: Java oops and fundamentals

The if-then and if-then-else Statements The switch Statement The while and do-while Statements The for Statement

Page 20: Java oops and fundamentals

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

} }

Page 21: Java oops and fundamentals

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

}

Page 22: Java oops and fundamentals

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

Page 23: Java oops and fundamentals

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.

Page 24: Java oops and fundamentals

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