Vous êtes sur la page 1sur 73

JAVA

When we compile a java program after successful compilcation it will generate a


.class file (this file contains byte code).
Because of this byte code only java has become platform independent.
Why? Bcz byte code is executed by JVM.
That means, for Linux there is seperate JVM. For windows separate JVM.
Or for other OS. Provided different JVMs for different OS
Examples:1.Can we run a program without main? In JAVA
YES. By using static block.
2. can we complie a program with out main
YES.
3. when we must make our class public
Example:
public class Sam0{

public static void main(String[] args) {


// TODO Auto-generated method stub
System.out.println("in main");
}
class Sam {
/**
* @param args
*/

4. can we save the file with another name and can we compile and run
YES
Oops concepts supported by java:1. Encapsulation :Grouping up of data members and methods with in the same class
Class A
{
Int a;
Void m()

//data members
/methods
{
}

}
Hidding the implementation of the class/method in a sub class
o 1. Class level
Class A
{
Class B
{
}
}
Hiding the implementation of class B in class A
2. Method level
Class A
{
Void m()

{
Void m1()
{
}
}
Hidding the implementation of m1() within m() method

3. Interface level
Interface A
{
Interface B{
}
}
Class A{

Interface B{

2.Abstraction:-

Showing the necessary data and hiding the unnecessary data.


Hiding the visibility of data.
Class
Interfaces
Colletions
Exception handling

3.Inheritance
a) extending the features of super class with in sub class.

Class A
{
//state-- member variables ------- for objects
//behavior -- methods()
}

Class B extends A
{
State member variables------ for objects
//behavior -- methods()
}
Generally we write behaviours for states (for datas)

Example:Account1------- balance=10000

Account2------ balance=500

Is Should write a program to transfer 1000 rupees from Account1 to Account2


Method/behavior( transfer(from,to))
transfer(long fromaccount,long toaccount) // behaviour
{
//business logic will transfer 1000 from to to account
}

In core java,if we write a simple class without extending any other class, then also
there will be a relationship formed?

How?
Class A extends Object
{

The Top level class in JAVA is object class


IN entire JAVA the super class is Object.

The inheritance can be provided by providing the relationship between two classes
or by providing the relationship between the class and the interface
Example:
How?
interface A1{
}
Class A
{
}
class B exnteds A{ / /formed with two classes
}
public class A implements A1{ //relationship formed wth
class and interface
}
Can we form a relationship with one class and many interfaces?
YES (since java doesnt support multiple inheritance,therefore it can be achieved
by above concept)

Example1:
interface A1{
}
interface A2{
}
interface A3{

}
public class A implements A1,A2,A3{
}

Example2:
interface A1{
}
interface A2 extends A1{
}
interface A3 extends A2{
}
public class A implements A3{
}

But, in core java we can form the relationship in two ways


1.Has a relationship
The has- a relationship can be obtained through a oops concept called
compostion/Delegation.

Class A{
Void m(){}
Void m1(){}
}
Class B{
A a1=new A()
a1.m()
}
Only m() method will be loaded, m1() method will not get Loaded as class B is not
using it.
Class B HAS-a object of class A

2. Is a relationship
The is-a relationship can be obtained through the oops concept called
inheritance
Class A{
Void m(){}
Void m1(){}
}

Class B extends A
{

In the above, both m() and m1() methods are internally available to class B.
Class B is-a subclass of Class A
OR
Class A is-a Super class of Class B

Note:As per core we cannot extend more than one class at a time [ bcz multiple
inheritance is not supported]

Classs A
{

Class B

Class C extends A,B . Not possible

{
}

Now, How to get Features of more than one class in derived class?

Class C{
A a1=new A();
B b1=new B();
}
(OR)
Class A
{
Void m()
{
}
}

Class B{
Void m1(A a1)
{
a1.m();
}

}
We call the above scenario as Delegation. And this is also an example for HASA relationship.
Example:
package com.cs;
class A
{
void m()
{
System.out.println("Class A m1() method");
}

void m2()
{
System.out.println("class A m2() method");
}

class C{
A a1=new A();
void m1()
{
a1.m2();
System.out.println("Class c m1() method");
}
}
public class B {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
C c1=new C();

c1.m1();

}
class A m2() method
Class c m1() method

What happens if we provide a constructor in inheritance relationship?


package com.cs;
class A1{
A1()
{
System.out.println("in class A ()
constructor");
}
}
class B1 extends A1{
B1()
{
System.out.println("in class B ()
constructor");
}
}
public class One {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
B1 b=new B1();
}
}

in class A () constructor
in class B () constructor

As per the above code, whenever we provide a default constructor in inheritance,


The constructor will internally give a super( ) call. Which is added by compiler.

super keyword// using super we can call a super class constructor and also a super
class overridden (At Dynamic Polymorphism) methods.
package com.cs;
class C1
{
C1()
{
System.out.println("in class C1 ()
constructor");
}
}
class A1 extends C1{
A1()
{
System.out.println("in class A1 ()
constructor");
}
}
class B1 extends A1{
B1()
{
System.out.println("in class B1 ()
constructor");
}
}
public class One {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
B1 b=new B1();
}
}

in class C1 () constructor
in class A1 () constructor
in class B1 () constructor

Constructor will get executed from TOP-TO-BOTTOM approach

What happens if we provide parameterized constructor?


Here no super() call provided.We need to do manually.
package com.cs1;
class A
{

i);
}

A(int i)
{
System.out.println("Class A Constructor"+"\t"+
}

class C extends A{
C(Float f)
{
super(120);
System.out.println("Class C
constructor"+"\t"+f);
}

}
public class B {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
C c1=new C(32.34F);
}
}
Class A Constructor
Class C constructor

120
32.34

package com.cs1;
class Z{
Z()
{
System.out.println("Class Z Constructor default
constructor");
}

k);

Z(int k)
{
new Z();
System.out.println("Class Z Constructor"+"\t"+
}

}
class A extends Z
{

A(int i)
{
super(100);
System.out.println("Class A Constructor"+"\t"+
i);

}
class C extends A{
C(Float f)
{
super(120);
System.out.println("Class C
constructor"+"\t"+f);
}

}
public class B {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
C c1=new C(32.34F);
}
}

Class
Class
Class
Class

Z
Z
A
C

Constructor default constructor


Constructor
100
Constructor
120
constructor
32.34

4. Polymorphism
a) Having the same method name with different parameters
Class A{

Int a;//state are member vairables


Void m1(int i)//methods are behaviours
{
}

Void m1(float f)
{

}
}

b) Deciding the behavior of the object.


i. We can decide the behavior of the object in two ways:1. At compilation time/ static polymorphism
Class A{
Void m(int i){}
Void m(float d){}
}
Class B extends A{
Void m(double d){}
}

package com.cs2;
class A{

void m(int i)
{
System.out.println("m(int i)");
}
void m(float f)
{
System.out.println("m(float f)");
}
}
class B extends A{
void m(double d)
{
System.out.println("m(double d)");
}

}
public class One {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
B b1=new B();
b1.m(100);
b1.m(30.9f);
b1.m(100.01);
}
}
m(int i)
m(float f)
m(double d)

2. At Run time/ Dynamic Polymorphism.

Class A{
Void m(int i){}
Void m(float d){}
}
Class B extends A{
Void m(int i ){}
}
package com.cs2;
class A{
void m(int i)
{
System.out.println("in class A m(int i)");
}
void m(float f)
{
System.out.println("m(float f)");
}
}
class B extends A{
void m(int i)
{
System.out.println("in Class B m(int i)");
}
}
public class One {
/**
* @param args
*/
public static void main(String[] args) {

// TODO Auto-generated method stub


B b1=new B();
b1.m(100);// here it will call sub class
}
}
in Class B m(int i)

How to resolve the above issue?


package com.cs2;
class A{
void m(int i)
{
System.out.println("in class A m(int i)");
}

void m(float f)
{
System.out.println("m(float f)");
}

class B extends A{
void m(int i)
{
super.m(i);// add a super call to the
method
System.out.println("in Class B m(int i)");
}

}
public class One {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
B b1=new B();
b1.m(100);// here it will call sub class
}
}

in class A m(int i)
in Class B m(int i)

//super()-----> must be used in Default constructor


only
//super.somemethod() --->> used in method

6.Generalisation :1.Generalization uses is-a relationship


2.is-a relationship we form with the help of
inheritance

Class A{

Class B extends A{

7.Realisation:-

1. Its a relationship between class and the object


which contains the respective implementation
level details.
Interface A{
Void add();
Void delete();
}
Class B implements A
{
Void add(){}
Void delete(){}
}

8.Association/aggregation:1. IS nothing but relationship between two objects


<one-to-one>,<one-to-many>,<many-to-one>,
<many-to-many>, HAS-A relationship

2. Aggregation is a special form of association


Class A{
B obj;
A(B obj)
{
}

this.obj=obj;

}
Class B{
Void m()

{
}

new A(new B())

}
3. Composition is a special form of aggregation

***************************************************

JAVA consists of :-

a) Classes
Class is a template/blue print.
Class is used to specify the state and behavior for an object.
State:Are nothing but the property of the class
State are used to represent data

How declare state? And how to define class?

Class classname
{
Dataype variablename;
}

Example:

Class Employee
{
Int EID;
String ENAME;

Void setEID()
{
}

Int getEID()
{
Return EID;
}
}

//in the above EID,ENAME are variable names. A variable should hava some
return type.

The return types can be either a primitive type/ wrapper types


Whenever we return any state, that state must have some return types

JAVA supports 9 primitive and 9 wrapper types


The primitive means it is used to hold only singl data.It will not hold any
objects/references.
The Wrapper means it is used to hold data as well as objects and also references

The 9 primitve Types are:-

1.int
2.float
3.char
4.double
5.short
6.long
7.byte.

8.boolean
9.void

The wrapper classes are:-

1.Integer
2.Float
3.Character
4.Double
5.Boolean
6.Byte
7.Short
8.Long
9.Void

Class Employee

{
Int eid;
String ename;

String design;
}
AS shown in the above code, String is not a primitive type. It is a predefined
class in java which is used to store group of characters or special characters.

Behaviour:Behaviour is used to provide the implementation for the state.

Behaviour is nothing but functions/methods

HOW?

How to define/ declare the behaviours?

Declare:-

Returntype functionname();
Define:Returntype functionname(datatype name,dataype name..)
{
//logic here
}

Example:-

int setProperty(int eid,String ename,String design)


{
Eid=eid;
Ename=ename;
Design-=design;
Return 1/or some interger value
}

Note:
If the method is not going to return any values then we are going to represent the
method as void.
Void- it is used to represent that the method does not return any value

Example:

Void setProperty(int eid,String ename,String design)


{
Eid=eid;
Ename=ename;
Design=design;
}// here return statement is not needed

Void is not a class, its a primitive which can be applied only for methods
Without a state and behavior class is dummy
Without object we cannot use class

b)Objects:Object are nothing but entity for a class.


By using objects we are going to access the properties of the class.

How to create an object for a class?

We can create Object in two different ways:1.


Example:-

Employee e;

Employee
E

classname
userdefined reference name

But, we cannot call this e as object, we call e as reference variable.


Why?
Until we cannot pass new keyword for the e referenced, we cannot get an
object for the class.

2.
Employee e=new Employee();
Employee

- classname

- user defined Object name

New
DMA (Dynamic memory allocator)---- allocates the memory
for e object inside the heap memory.
Employee() - in java we call this a constructor.

What is the difference between a reference variable and an object?


1.Reference variable wil be stores inside

1.Objects are stores in heap area

Stack are.
2.Reference variable can only point to the
Class properties.But using reference variable
class

we Cannot access the properties of class.

2. Objects can point to the class and a


also can access properties of

Eid,ename,desig

In order to create the object for employee class , we require a method preferably
Main() method

Main() is nothing but which is used to show the execution point for the object
execution.
How to specify the main() method in java?

Public static void main(String [] a)


{
}

Public is used to represent the scope:-

In java there are 4 scopes supported:1.public


2.private
3.protected
4.default

Public means ,the method can be accessed from any where.(inside the class,outside
the class,outside from some other clas etc;)
Static is a non-access specfier in java.
Once a method is made static, then we need not create object for calling that
(main )method

Static helps us to access the method globally.

Void it represents that main() method is not going to return any value.
Main()- shows the execution point for JVM to process our request.
String it is predefined class in java.

[] args declaration of array variable of type String


Main(String [] v)

String [] v
Stirng v[]
v- variable name. it can be anything

In order to display output at console we make use of the statement


System.out.println();

System- is a predefined class.


IS used to dislay the value onto the console(command prompt) or to accept the
values through keyboard.

Class System
{
Static PrintStream out;
Static InputStream in;
Stati PrintStream err;
}
class Sys{
}

static Print out;

class Print{
void println()
{

}
}
public class myMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Sys.out.println();
}
}
OutIs a predefined object ( reference) of PrintStream class.

PrintStream is a predefined class


1.out outputs the value
2. in accepts the value

Constructor:Constructor is used to allocate the memory for the object.


Constructor will initialize the default values for the properties of the object
Whenver we define a class,there will be a default constructor available.
The signature of default constructor
Classname()
{
Super();
}
Note:Constructor should be same as classname bcz it is used to create and allocate
memory for the object of the same class.
Constructor always belongs to same class
Constructor cannot be inherited
Constructor does not have any return type coz its return type of the constructor is
class itself(Object). Even void.
Constructor can be overloaded
Constructor cannot be overridden.

Example:
package com.cs;
class A{
int id;

A()
{
super();
System.out.println("in A() constructor");
}
void m()
{
System.out.println("class A method");
}

}
public class B {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
A a1=new A();
a1.m();
}
}
in A() constructor
class A method

Example:Constructor overloading means, same constructor is used to pass different


parameters.
Once we provide a parameterized constructor then there will be no default
constructor provided by JVM.
package com.cs;

class A{
int id;
String name;
A(int id,String name)
{

name)");
}

this.id=id;
this.name=name;
System.out.println("called A(int id,String

A(String name,int id)


{
this.id=id;
this.name=name;
System.out.println("called A(String name,int
id)");
}
void m()
{
System.out.println("ID:"+id+"\t"+"Name:"+name);
}
}
public class B {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
A a1=new A(1001,"rakesh");
a1.m();
A a2=new A("rakesh",2001);
a2.m();
}

called A(int id,String name)


ID:1001
Name:rakesh
called A(String name,int id)
ID:2001
Name:rakesh

The above program is a drawback. IF we have 5 constructors in a class, then we


need to create 5 objects ,5 times.Then the memory will be wasted.

How to overcome This?

package com.cs;
class A{
int id;
String name;
A(int id,String name)
{

this("rakesh",2001);

name)");
}

this.id=id;
this.name=name;
System.out.println("called A(int id,String
m();

A(String name,int id)


{
this.id=id;
this.name=name;
System.out.println("called A(String name,int
id)");
m();
}

void m()
{
System.out.println("ID:"+id+"\t"+"Name:"+name);
}
}
public class B {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
A a1=new A(1001,"rakesh");
}
}

this call is used to call the overloaded constructors with in the same class
this call is used to call only constructors. We cannot use this call for calling the
overloaded methods.

In java, we have a concept called Garbage Collector.


Whenever JVM once identifies that an Object memory is not needed.
Automatically Garbage Collector will remove the objects memory.

Manually making reference as null


A a1=new A(1001,"rakesh");
a1=null;

Variables and methods:There can be 2 types of variables we can specify with in a class
1. static variable
static int a;
a) static variable can be represented by the class or object.
b) Static variables are available for the class directly
Why?
Static variables get loaded into the memory during the loading of
class itself.
c) Static variables will be stored inside the method are (part of
heap)
d) Static variables can be used only with in static methods.

package com.cs;
class MyShare
{
static int todayPRICE=2;
void increment()
{
todayPRICE++;
}
void decrement()
{
todayPRICE--;
}
void displayTodaysPrice()
{
System.out.println("SHARE:"+todayPRICE);
}

}
public class StaticEx {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyShare m1=new MyShare();
m1.increment();
MyShare m2=new MyShare();
m2.decrement();
MyShare m3=new MyShare();
m3.increment();
MyShare m4=new MyShare();
m4.increment();
m1.displayTodaysPrice();
m2.displayTodaysPrice();
m3.displayTodaysPrice();
m4.displayTodaysPrice();
}
}
SHARE:4
SHARE:4
SHARE:4
SHARE:4

2. Non- static variables


Int a;
a) The instance can be represented only with the help objects.
b) Until and unless we wont call the instance variables ,the memory
for that variable will be allocated.
c) The instance variables will be stored in the heap memory.
d) Instance variables can be used in instance method/static method

e) Instanve variables are available only for the object not for class.

There can be 2 types of methods we can specify with in a class:1. Static method
Static Void m()
{
}
a) Static method can be called directly by the classname or we can also call
with Object.
b) Static method are stored in method area.
c) During the loading of the class the static method will be loaded
package com.cs;
class A{
static int id;//instance variables in runtime
static String name;
double sal;
A(int id,String name)
{
this.id=id;
this.name=name;
System.out.println("called A(int id,String
name)");
}
static void m()
{
System.out.println("ID:"+id+"\t"+"Name:"+name);
}
}
public class B{
/**
* @param args

*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new A(1001,"rakesh");
A.m();
}
}
2. Non-static method
Void m()
{
}
a) Instance method can be called only using object.

A a1=new A(1001,"rakesh");
a1.m();
b) Instance methods are stored inside heap memory.
c) Untill and unless the instance method is not called, the method will be
dummy.

Difference between static variable and non-static variable


package com.cs;
class A{
static int id;//instance variables in runtime
String name;
double sal;
A(int id,String name)
{

this.id=id;
this.name=name;
System.out.println("called A(int id,String

name)");
}
static void m()
{
System.out.println("ID:"+id+"\t"+"Name:"+name);
}
}
public class B{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(A.id+"\t"+A.name);
new A(1001,"rakesh");
A.m();
}
}

A.name:- Error: bcz is not-static


variable not loaded at class loading
time.

Static block/ Instance Block:The class can also consist of two blocks:1Static block 2.Instance block
package com.cs;
public class A1 {
/**
* @param args
*/
static{// static block
System.out.println("in static block");
}
{// instance block
System.out.println("in instance block");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("in Main()");
A1 a=new A1();
}
}
in static block
in Main()
in instance block

a) Whenever we want to share the same information for all objects during the
loading of class itself.
b) Static will be called automatically during the loading of class.
loading of class is done by classloader.
c) Static block will be called only once.

1) Instance block will be available only when we create object for the class
2) How many number of objects we create for the class that many times
instance block will be called.

package com.cs;
public class A1 {
/**
* @param args
*/
static{
System.out.println("in static block");
}
{

System.out.println("in instance block");

}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("in Main()");
A1 a=new A1();
A1 a1=new A1();
A1 a2=new A1();
}
}
in
in
in
in
in

static block
Main()
instance block
instance block
instance block

Access specifiers: Public


1. Can be applied to a class.
Whenever we provide the public specifier for a class, then we need
to save the file with the same classname, which is holding public
specifier.
2. Can be applied to a method
3. We cannot make constructor as public
We can globalize the properties by making public
Private
1.can be applied for a constructor
2. cannot be applied at class level
3. private methods/properties are visible only to their own class.

Protected
If we want to provide the accessing of the class properties with in the
same package sub class and within the other package sub class.
1.can be applied for the variables
2.can be applied for the methods
3.can be applied for constructor
4.cannot be applied at class level
Default
We can access the default properties with in the same package sub
class but we cannot access the properties with in the different package
sub class.

1.can be applied for the variables


2.can be applied for the methods
3.can be applied for constructor
4.can be applied at class level

Non-access Specifiers
Static
Final
Three ways we can apply final
1. Final at class level
Overriding cannot be possible when we make a class as final.
2. Final at variable level
The data is fixed. Nobody can change the state.
3. Final at method level
We can overcome overriding. That is Overriding is not possible
Interface
public interface Shop {
//public static final will be added by compiler
public static final int shopID=84957;
public abstract void setCart();// all these methods
pure abstract methods in interface why? bcz no
implementation
public abstract void getCart();
public abstract void saveCart();
public abstract void selectCart();
public abstract void removeCart();
}
//no need to write like above bcz compliler does for us
public interface Shop {

int shopID=84957;
public void setCart();// all these methods pure
abstract methods in interface why? bcz no
implementation
public void getCart();
public void saveCart();
public void selectCart();
public void removeCart();
}

package com.cs;
public class Retail implements Shop {
@Override
public void setCart() {
// TODO Auto-generated method stub
System.out.println("in setCart() method");
}
@Override
public void getCart() {
// TODO Auto-generated method stub
System.out.println("in getCart() method");
}
@Override
public void saveCart() {
// TODO Auto-generated method stub
System.out.println("in saveCart() method");
}
@Override
public void selectCart() {
// TODO Auto-generated method stub
System.out.println("in selectCart() method");
}
@Override
public void removeCart() {

// TODO Auto-generated method stub


System.out.println("in removeCart() method");
}
}

package newy.me;
import com.cs.Retail;
import com.cs.Shop;
public class TestCase {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Shop shop =new Retail(); // Dynamic Dispatch
shop.selectCart();
}
}

abstract

It is a non-access specifier which is used to provide the restriction upon the


class.
Whenver we dont want the user to provide accessing for the class properties
directly then we go for abstract keyword.
An abstract class cannot be instantiated.

Difference between interface and abstract class


Once we implements an interface into our class, then we have to provide the
implementation for all methods.
If we dont want to provide the implementation for some methods, then that
class has to be made as abstract.
That is we can declare the method again in our class and apply abstract for
those methods.

package com.cs.one;
public interface Librarian {

public void issueBook();


public void returnBook();
public void renualBook();

package com.cs.one;
public abstract class LibrarianImpl implements
Librarian {
@Override
public void issueBook() {
// TODO Auto-generated method stub

}
@Override
public void returnBook() {
// TODO Auto-generated method stub
}
public abstract void renualBook();
}
package com.cs.one;
public abstract class Librarian1 {
public void issueBook() {
// TODO Auto-generated method stub
}
public void returnBook() {
// TODO Auto-generated method stub
}
public abstract void renualBook();
}
package com.cs.one;
public abstract class A extends Librarian1{
@Override
public abstract void renualBook();
}

package com.cs.one;
public class B extends A{
@Override
public void renualBook() {
// TODO Auto-generated method stub
}
}
Points to remember abstract class:
we can write main() method inside the abstract class

abstract class A{
public static void main()
{
}
}
Why?
Abstract class can consists of declared and defined methods
we can write constructor inside the abstract class
abstract class A
{
A()
{
}
}
When abstract class constructor is usefull?

Bcz default constructor is called automatically even for abstract class constructor

package one.two;
public abstract class A
{
A()
{
System.out.println("in abstract class A
A() constructor");
}
public abstract void add1();
}
package one.two;
public class B extends A{
B()
{

super();

}
public void add1()
{
}
public static void main(String [] ar)
{
B b=new B();
}
}

in abstract class A A() constructor

We can make a class as abstract even if our class doesnt consist of single
abstract method
public abstract class A
{

In the above we are restricting the user to create object for our abstract class.
If we are restricting it is called Encapsulation.
Another Example for Encapsulating.
Not provide implementation/definition details to the user but rather
provide service details to the user.
Lets take an example:
package com.cs.one;
public interface Librarian { // service name is
Librarian

public void issueBook();


public void returnBook();
public void renualBook();

package com.cs.one;

public class LibrarianImpl implements Librarian {


@Override
public void issueBook() {
// TODO Auto-generated method stub
System.out.println("Issued Book");
}
@Override
public void returnBook() {
// TODO Auto-generated method stub
System.out.println("Return Book");
}
@Override
public void renualBook()
{
System.out.println("Renual Book");
}
}

package com.cs.one;
public class MyMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Librarian l=new LibrarianImpl();
//here Librarian is service name ie; interface name
l.issueBook();
l.renualBook();
l.returnBook();

}
}

Volatile
Transient
Synchronized

Object class : In entire java the super class for all classes is Object
There is a predefined class in java called Object
class Object
{
// it contains seven methods
}
equals
notify()
notifyAll()
getclass()
hashCode()
wait()
toString()
.
package com.cs.one;
public class MyMain {
/**
* @param args

*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Librarian l=new LibrarianImpl();
l.issueBook();
l.renualBook();
l.returnBook();
Librarian l1=new LibrarianImpl();
System.out.println(l.hashCode());
System.out.println(l1.hashCode());
MyMain m=new MyMain();
System.out.println(m.hashCode());
}
}
11394033
4384790

Note:
By default java.lang.* will be imported
The above package is the default package In java

package com.cs.one;
public class MyMain{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Object o=new Object();
System.out.println(o.getClass());

}
}

class java.lang.Object

package com.cs.one;
public class MyMain{
/**
* @param args
*/
public static void main(String[] args) {

codes

Object o=new Object();


System.out.println(o.getClass());
//equals() in object class compares two hash
if(l.equals(l1)){
System.out.println("Objects ID's are

same");

}
else

System.out.println("ID's not same");

}
}
System.out.println(o.getClass().toString());

JDK 1.5 features [The current version of JDK is JDK 1.7]


The jdk1.5 features are:o Generics
are used for type safety conversions
we can decide dynamically what our datatypes needs to be
It can e of Wrapper types or user defined types
package com.cs;

class A<T>
{
T a;
T b;

public T getA() {
return a;
}

public void setA(T a) {


this.a = a;
}

public T getB() {
return b;
}

public void setB(T b) {


this.b = b;
}

A(T a,T b)
{
this.a=a;
this.b=b;
System.out.println("Value in a="+getA());
System.out.println("Value in b="+getB());
}

public class Sam {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

A<Integer> a=new A<Integer>(100,500);


A<String> a1=new A<String>("Naga","Manoj");

}
o varargs
When we define any method with parameters
Example:
Void m(int x)
{

}
Now to the above method we can pass only 1 interfer value
That is m(10).
If we want to pass more than one value then declare variable sa an array
Void m(int[3] x)
{

}
But if you give more than 3 elements u get
ArrayIndexOutOfBounds Exception

Therefore go for varrags

Void m(int b)
{
}

package com.cs;
public class VargEx {
/**
* @param args
*/
public static void AA(int ...myvar)

{
//System.out.println(myvar);

}
public static void main(String[] args) {
// TODO Auto-generated method stub
AA(10);
AA(10,20,30);
AA(10,20,30,40,50);

}
}

o Enumeration
Enumeration is a type which will help us to define the
properties/variables/values in a global way.

Java does not support global variables


So in order to declare any global variables we go for
Enumaration.

The enumeration is defined by using a keyword called enum.

Example:
Enum enumname

{
Constant values;

That is
Enum Weekdays
{

SUNDAY,MONDAY,TUESDAY, //enum values


}

We can get Enum values using enum name


How?

Weekdays.SUNDAY

package com.cs;
enum WeekDays
{
SUNDAY,MONDAY,Tuesday,Wednesday,Thursday,Friday,Saturda
y;
}

public class EnumEX {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WeekDays[] w=WeekDays.values();
System.out.println(w.length);
}
}

o for each loop (enhanced for loop)


syntax:
for(Object referece:variable name)
{
}
Example:
package com.cs;
public class VargEx {
/**
* @param args
*/
public static void AA(int ...myvar)
{
/*for(int i=0;i<myvar.length;i++)

System.out.print(myvar[i]+"\t");

}*/

for(int x:myvar) // for each loop


{
System.out.println(x);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
AA(10);
System.out.println("");
AA(10,20,30);
System.out.println("");
AA(10,20,30,40,50);
}
}
package com.cs;
public class MyExForEach {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer[] num={1,100,20000,30000};
for(Integer i:num)
{

System.out.println("Number

is:"+i);
}

}
}
Number
Number
Number
Number

is:1
is:100
is:20000
is:30000

o annotations
annotations are provided by sun and also by other vendors
Whenever we do documentation we go for comments // or /* */
Whenever we use the above comments then it is available only
within source code.
At Compilation of code we go for XDOCLET
If we want to do documentation in the run time. We go for
annotation
Core java supports 7 types of annotation:1.
2.
3.
4.
5.
6.
7.

@overriden
@documented
@inherited
@Supresswarning
@target
@RetentionPolicy
@deprecated

package com.cs;
public interface A {

public void A1();


public void B1();
}
package com.cs;
public class AImpl implements A {
@Override
public void A1() {
// TODO Auto-generated method stub
}
@Override
public void B1() {
// TODO Auto-generated method stub
}
}
o Autoboxing and unboxing

Autoboxing
Converting a primitive type into wrapper type
package com.cs;
public class Sample {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

//done prior to JDK 1.5


int i=10;
Integer k=Integer.valueOf(i);
//since JDK 1.5 no need to manual
conversion

int i1=10;
Integer k1=i; // autoboxing

}
}
Unboxing
Converting a wrapper into primitive
package com.cs;
public class Sample {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//done prior to JDK 1.5
Integer i=Integer.valueOf(3);
int k=i.intValue();
conversion

//since JDK 1.5 no need to manual


Integer i1=3;
int k1=i1;// unboxings

}
Package
1.Wherever we want to encapsulate the information
within a single name we go for Package.
2.Package is group of
interfaces,classes,Enums,Exceptions
3.There is a default package supports by java that is
java.lang.*
Can maintain a directoruy structure
Can divide our project into modules

String
String is predefined class / wrapper class
It can take any number of characters,special
symbols,numbers
Note: always write strings in

package com.one;
public class StringEx {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="manoj";
s=s.toUpperCase();
System.out.println(s);

}
}

package com.one;
public class StringEx {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="Gokul";
String s1="xyz";
System.out.println("Before"+s);
s=s+s1;
System.out.println("After"+s);

}
}

BeforeGokul
AfterGokulxyz

Exception HANLDING