Вы находитесь на странице: 1из 35

Core Java

Srikanth M D Software Consultant Colossal technologies

Object Oriented Programming


Is a very popular approach of problem solving in software applications The solution simulates real life entities and there actions to solve problem Object are building blocks of solution

Objects
Runtime entities Class instances Interact with each other by passing messages Objects simulate real world entities Objects have state and behavior

Classes
A class specification that specifies the design of an object A collection of objects A user define data type A blue print of an object A classes has two types of members Fields Methods

Compile Time

Inside Run Time Environment

Compilers and Interpreters

<<Object>>

cust1: Customer

Obj1: Customer Cash: double

Purchase( ) Sell()

Obj2: CashClerk Cash: double

Object Oriented programming

Procedure Oriented Programming

fun1()

fun2()

fun3()

--------------

--------------

--------------

Global data

Member Fields
A filed is a data holder Fields can of both primitive and user defined class type

Methods
Contain executable code Service Methods Helper Methods Getters/setters

Oops / Pops
Oops application are more maintainable Oops application can reuse code but need a strong design Oops application implement better modularity

Hello World Example

Java Source code

javac

Byte code

jvm

jvm

jvm

Unix

Windows

Linux

Java Lexical Lessons


Keywords Primitive Types Operators

Keywords
abstract
boolean

default
do

if
implements

private
protected

throw
throws

break double transient byte


case

import instanceof
int

public return
short

else
extends

try
void

catch volatile char


class

final finally
float

interface long
native

static super
switch

while

const*
continue

for
goto*

new
package

synchronized
this

P rimitive D ata T ype R anges


Type boolean char byte short int long float double void * Not truly min and max. Size 1 16 8 16 32 64 32 64 -128 -32,768 127 32,767 Min false* Ma x true* Default false '\u0000' (null) (byte) 0 (short) 0 0 0L 0.0F

-2,147,483,648
-9,223,372,036,854,775,808

2,147,483,647
9,223,372,036,854,775,807

Approx Approx

3.4E+38 with 7 significant digits

1.7E+308 with 15 significant digits 0.0D

Conditional Constructs
If ( true ){ //execute this code } If (age<15){
System.out.println(Not qualified); }

If ( true){ //execute this code } else { //execute this code }

switch(var){ case 1: //execute this code break; case 2: //execute this code break; default: //execute this code }

Loop Constructs
While (<<Condition to control loop>>){ //execute this code //increment control }

do{
//execute this business code //increment control

}while(<<condition to control loop>>)

For (intialization ;condition;incrementation) { //execute code } Example for ( int i=0 ; i<10 ;i++ ) { System.out.println(i); }

T he for statement
The for statement has the following syntax:

Reserved word

The initialisation part is executed once before the loop begins

The statement is executed until the condition becomes false

for (initialisation; condition; increment) statement; The increment part is executed at the end of each iteration

Value type / reference types


Value type variables are primitive type variables Reference type variables are reference variable which hold a reference to the object on the heap

Java source Code

Java compiler

jvm

Stack

Manag ed Heap

Data Segme nt

Code segment

F1()
F2() F3()

Unmanaged heap

CA obj=new CA();
obj is a reference variable new is a operator used to acquire memory on the heap CA is a class name and CA() is a constructor call which intializes the object members

Instance members are member variables declared with class scope occupy memory in the object on the heap Local members variables declared inside a method Created and destroyed during the function call Occupy memory on stack

Global Variables Variables declared with static modifiers Occupy memory in data segment Declared in class scope Single copy in memory shared among all the objects of the class

Constructors
Constructors are methods of a class which are used to initialize instance variables Java compilers provide default constructor to initialize instance members to default values constructors can be overloaded for customized initialization of instance members.

Abstraction

Black box Module

White box module

Client Module

Abstraction
"A view of a problem that extracts the essential information relevant to a particular purpose and ignores the remainder of the information." "An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of object and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer."

Abstraction
Abstraction is most often used as a complexity mastering technique Abstraction dictates that some information is more important than other information, but (correctly) does not specify a specific mechanism for handling the unimportant information.

Encapsulation
Encapsulation means the act of enclosing one or more items within a (physical or logical)container "every thing that was encapsulated is not also hidden

Class Relations
A java class at runtime can relate to another class There are four types of class relationships
Dependency Association Containment Generalization

Inheritance
Inheritance is generalizes different classes with common behavior under one family of classes by creating a parent class with common behavior All the member classes of the family inherit (extends)from the parent class All the member classes inherit the members of the parent class Thus inheritance allows reusability of code

Golden rule of inheritance : Base class reference can point derived class Objects Example : class CA {
//members of CA } class CB extends CA {

//inherited members of CA //members of CB


}

Heterogeneous Arguments Rule


Method accepting a base class object as a argument can accept a derived class object as an argument

Вам также может понравиться