Core Java: Srikanth M D Software Consultant Colossal Technologies
Core Java: Srikanth M D Software Consultant Colossal Technologies
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
<<Object>>
cust1: Customer
Purchase( ) Sell()
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
javac
Byte code
jvm
jvm
jvm
Unix
Windows
Linux
Keywords
abstract
boolean
default
do
if
implements
private
protected
throw
throws
import instanceof
int
public return
short
else
extends
try
void
final finally
float
interface long
native
static super
switch
while
const*
continue
for
goto*
new
package
synchronized
this
-2,147,483,648
-9,223,372,036,854,775,808
2,147,483,647
9,223,372,036,854,775,807
Approx Approx
Conditional Constructs
If ( true ){ //execute this code } If (age<15){
System.out.println(Not qualified); }
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
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
for (initialisation; condition; increment) statement; The increment part is executed at the end of each iteration
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
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 {