You are on page 1of 35

Java

Object Oriented Programming(OOP):


Background Of OOP
Object is a person,thing that have class
,function,method and attributes(zahiri shakal).e.g
person,motorcycle,animal,etc.
OOP me humary object ki approach function and attributes
he.
Advantages Of OOP:
(1) Code separation:
Is me aik kam ko different parts me divide kr dety hn or
har part alag alag bandy ko pura krny k liay de dety hn.
Is me ek complex program (task) ko different components
k xth mila kr ek shakal dena OOP kehlata hai.
(2) Code Reusability:
Ek component ko reuse krna.
Pehly code sy dabra aik naya object bnana.
Aik code ko dusry code me integrate krna.
Classes and Objects
A person is class, male and female are its objects.
Animal is a class, dog and cat are its objects.
Every object belongs to a class.
Classes consist of a hierarchy, which means parent child
relationship. Open Source:
Ye wo softwares hn jinky liay license ka hona zruri ni.for
example,linux and window etc.
These consist of the following:
(1) Function/method.
(2) Attributes.

Java:
1996 me java language bhi thi. James Croslings was the developer
of the java language.James worked for 1.5 years and after 1.5
years he was able to develop the first version of java language.
Embedded Software:
Embedded software are those softwares which are fix and
they don’t need any memory. Once a program installed in the
device it will on work on that program no other program can be
integrated with it. Instructions are given to it and the program
will only work according to those instructions. For example,
microwave oven, Ac etc.
Advantages of Java:
This language is an open source language i.e. it doesn’t need
any kind of license to be used.
It is a machine independent language or platform system
independent .
Comparison between C language and Java:
Programs in c language only work in windows operating system but
the programs in Java have no limitation they can be executed on
linux,windows,mac etc.for example if a program is made in linux
it can run on windows.Thats why Java is a machine independent
language.
Machine Independent:
Hardware purana ho ya naya Java har kisi py chlta hy and theres
no limitation. Platform Independent:
There is no limitation for the operating system fo Java it works
on every operating system.
Development of android can also be done by using Java.
Description for class and object:
Jesi class ho gi object bhi wesa hi hoga. Class aik dafa bnti hy
or object zada martba ban skta hy.for example,
Time t1=new Time();
t1.h=15; t2.m=23;
t3.s=44;
Time t2=new Time();
t1.h=7; t2.m=34;
t3.s=54;

jb bhi koi code likhna ho to usko kisi na kisi method k


andr likhyn q k agr usmy ni likhein gy to error aa jye ga
.
for example,
Time{ int
h,m,s; void
print(){
Sysytem.out.println(“h=”+h+”,m=”+m+”,s=”+s);
}
}
Function ki Pehchan:
Function ki pehchan brackets hn () jis k xth ye brakets lagi ho
gi wo function hy.isky xth return type bhi ho gi.
Format: return_type function_name()
Is me opening nd closing brackets {} hn gi.
OOP k liay functions ka hona bht zruri hy it is a block of code.
Void print:-
Ye aisi return type hy jo value bhi return ni krta.

• Jb bhi Built in class use krein gy to uska pehla letter


capital hoga.
• Java me capital or small letter ka khaas dehan rkhna hy
warna error aa jye ga.

Class ki Pehchan:
Java me har Class ka pehla letter capital or uppercase me ho ga
agr ni to wo class unacceptable ho gi.
Variable ki Pehchan:
Variables java k andr small letters or lowercase me hn gy or in
k xth parenthesis ni hn gi. Example of class variable function:
class Time{ int
h,m,s; void
print(){
System.out.println(“h=”+h+”,m=”+m+”,s=”+s”);
}
} class
Test{
public static void main(String args[]){
Time t1=new Time();
t1.h=23; t2.m=44;
t3.s=56;
}
}
Constructor(important):
Constructor is a special type of function which is
totally different from an ordinary function because it
has some special working in the program. It is used to
solve a particular problem.
Characteristics of a constructor:
• Constructor name and class name are always same.
• Constructor has no return type.
• Constructor is used to initialize class variables.
• Contructor is called when an object is created.
Example:
class Date{
int d,m,y;
Date(){
d=12; m=7;
y=2018;
}
Void print(){
System.out.println(“d=”+d+”,m=”+m+”,y=”+y);
} } class
Test{
public static void main(String args[]){
Date d1=new Date(); d1.print();
}
}
Sequence of main class and other class doesn’t matter
in java.
Default Constructor:
Date()
{
}
Koi bhi class beghair kisi constructor k compile ni hoti.
Agr user koi constructor ni bnata to compiler us class me
khud constructor bna deta hy.

Parameterized Constructor:
Example: class
Date{ int
d,m,y;
Date(int a,int b,int c){
d=a; m=b; y=c;
}
Void print(){
System.out.println(“d=”+d+”,m=”+m+”,y=”+y);
} } class
Test{
public static void main(String args[]){
Date d1=new Date(12,3,2018); d1.print();
}
}
Examples of constructors:
Example#1: Rectangle Area.
class Rectangle{ int
w,h;
Rectangle(int a,int b){
w=a; h=b; } int
getArea(){
Return w*h;
} } class
Test{
public static void main(String args[]){
Rectangle r1=new Rectangle(5,7); int
x=r1.getArea(); System.out.println(x);
}
}
getArea(),getVolume,etc are the built in functions to
calculate area and volume.

Constructor overloading:
Creating or making multiple constructors in a class is
known as constructor overloading.
The parameters in the constructors should be different for
every constructor.
Sequence doesn’t matter for constructors.
Example:Valid
Time(){
}
Time(int x,int y,int z){
}
Time(int a,int b,int c){
}
Data Type List:
• Boolean(1 byte)(True or false).
• Byte (127 to -128).
• Short (2 bytes)(-32768 to 32767).
• Char .
• Int.
• Long.
• Float.
• Double.
• Class.
“This” keyword:
Java language doesn’t use pointers instead java use “this”
keyword which refers to the current object. Examples:
Rational Numbers:
Add two rational numbers.
Program: class
Rational{ int
n,d;
Rational(int a,int b){
n=a; d=b; }
void print(){
System.out.println(n+"/"+d);
}
Rational add(Rational r1, Rational r2){
int p=r1.d*r2.d; int a=p/r1.d *r1.n; int
b=p/r2.d *r2.n;
Rational t=new Rational(a+b,p); return
t;
} } class
Test{
public static void main(String args[]){
Rational r1=new Rational(2,3);
Rational r2=new Rational(5,6);
Rational r3=r1.add(r1,r2); r3.print();
}
}

Subtract two rational numbers.


Program: class
Rational{ int
n,d;
Rational(int a,int b){
n=a; d=b; } void
print(){
System.out.println(n+"/"+d);
}
Rational subtract(Rational r1, Rational r2){
int p=r1.d*r2.d; int a=p/r1.d *r1.n; int
b=p/r2.d *r2.n; Rational t=new Rational(a-
b,p); return t;
} } class
Test2{
public static void main(String args[]){
Rational r1=new Rational(2,3);
Rational r2=new Rational(5,6); Rational
r3=r1.subtract(r1,r2); r3.print();
}
}

Divide two rational numbers.


Program: class
Rational{ int
n,d;
Rational(int a,int b){
n=a; d=b; } void
print(){
System.out.println(n+"/"+d);
}
Rational divide(Rational r1, Rational r2){
int a=r1.n*r2.d;
int b=r1.d*r2.n;
Rational t=new Rational(a,b); return
t;
} } class
Test4{
public static void main(String args[]){
Rational r1=new Rational(2,3);
Rational r2=new Rational(5,6); Rational
r3=r1.divide(r1,r2); r3.print();
}
}

Multiply two rational numbers.


Program: class
Rational{ int
n,d;
Rational(int a,int b){
n=a; d=b; }
void print(){
System.out.println(n+"/"+d);
}
Rational multiply(Rational r1, Rational r2){
int a=r1.n*r2.n;
int b=r1.d*r2.d;
Rational t=new Rational(a,b); return
t;
} } class
Test3{
public static void main(String args[]){
Rational r1=new Rational(2,3);
Rational r2=new Rational(5,6); Rational
r3=r1.multiply(r1,r2); r3.print();
}
}

Add two rational numbers using this


statement.
Program: class
Rational{ int
n,d;
Rational(int a,int b){
n=a; d=b; }
void print(){
System.out.println(n+"/"+d);
}
Rational add(Rational r){ int
p=r.d*this.d;
int a=p/this.d*this.d; int
b=p/r.d*r.n;
Rational t=new Rational(a+b,p); return
t;
} } class
Test5{
public static void main(String args[]){
Rational r1=new Rational(2,3);
Rational r2=new Rational(5,6);
Rational r3=r1.add(r2); r3.print();
}
}

Add two rational numbers using single


variable.
Program: class
Rational{ int
n,d;
Rational(int a,int b){
n=a; d=b; }
void print(){
System.out.println(n+"/"+d);
}
Rational add(Rational r){
int p=r.d*d; int
a=p/r.d*r.n; int b=p/d* n;
Rational t=new Rational(a+b,p); return
t;
} } class
Test6{
public static void main(String args[]){
Rational r1=new Rational(2,3);
Rational r2=new Rational(5,6);
Rational r3=r1.add(r2); r3.print();
}
}

Multiply two rational numbers using


single variable.
Program: class
Rational{ int
n,d;
Rational(int a,int b){
n=a; d=b;
}
void print(){
System.out.println(n+"/"+d);
}
Rational multiply(Rational r){
int
a=r.n*n; int
b=r.d*d;
Rational t=new Rational(a,b); return
t;
} } class
Test7{
public static void main(String args[]){
Rational r1=new Rational(2,3);
Rational r2=new Rational(5,6); Rational
r3=r1.multiply(r2); r3.print();
}
}

Divide two rational numbers using


single variable.
Program: class
Rational{ int
n,d;
Rational(int a,int b){
n=a; d=b; }
void print(){
System.out.println(n+"/"+d);
}
Rational multiply(Rational r){
int
a=r.n*n; int
b=r.d*d;
Rational t=new Rational(a,b); return
t;
}
Rational divide(Rational r){
int
a=r.n*d; int
b=r.d*n;
Rational t=new Rational(a,b); return
t;

} } class
Test8{
public static void main(String args[]){
Rational r1=new Rational(2,3);
Rational r2=new Rational(5,6);
Rational r3=r1.multiply(r2); r3.print();
Rational r4=r1.divide(r2); r4.print();
}
}

Print “Hello World” in a single


class.
class Test{
public static void main(String args[]){
System.out.println("HELLO WORLD");
}
}

Inheritance:
Definetion:
Inheritance is a technique in which a class is based on a
existing class.
Extends:
It is a keyword which is used to inherit a class with
another class.
Principles of inheritance:
• Pehly parent class ko values milti hn or usky bad
child class ko initialize kia jata hy.
• Parent class ka constructor pehly initialize hota hyor
child class ka bad me hota hy lekin call bad me ho ya
pehly is me koi sequence ni k pehly child class ho ya
parent class.
Super keyword:
It is a keyword which is used for the parent class. It is
used to call the parent class from another class.
Multi-level Inheritence:
It is supported in every languge.
A

B

C

D
Multiple Inheritence:
It is not supported by Java but it is supported by C++.
Examples:

Print student name , student roll no.,employee


name,employee salary, using inheritance.

Program: class
Person{ String
name; int age;
void print(){
System.out.println("Name:"+name+",Age="+age);
} }
class student extends Person{
int r; void
print(){
super.print ();
System.out.println("Roll NO."+r);
} }
class employee extends student{
int salary;
void print(){
super.print ();
System.out.println("Salary="+salary);
} } class
Test3{
public static void main(String args[]){ ,employee
e=new employee();
e.name= "Asad";
e.age=34;
e.r=3;
e.salary=20000;
e.print();
}
}
Print student name , student roll no.,employee
name,employee salary, without using inheritance.
Program: class
Person{ String
name; int age;
void print(){
System.out.println("Name:"+name+",Age="+age);
} } class
student{
int r; void
print(){
super.print ();
System.out.println("Roll NO."+r);
} }
class employee{
int salary;
void print(){
super.print ();
System.out.println("Salary="+salary);
} } class
Test4{
public static void main(String args[]){ Person
p=new Person();
p.name= "Asad";
p.age=34;
p.r=3;
p.salary=20000;
p.print();
}
}
Print values of I,j,k using inheritance.
Program:
class A{ int
i,j; void
print(){
System.out.println("i="+i+"j="+j);
} }
class B extends A{
int k; void
print(){
super.print();
System.out.println("k="+k);
}
}
class
Test2{
public static void main(String args[]){
B b=new B(); b.i=2;
b.j=3;
b.k=6;
b.print();
}
}

Interface:
Definetion:
Template of incomplete method is known as interface.
Voice of animals.
Program:
interface Animal{
public void voice();
} class Dog implements
Animal{ public void voice(){
System.out.println("Bark");
}
}

class Lion implements Animal{ public


void voice(){
System.out.println("Roar");
}
}
class
Test{
public static void main(String args[]){
Animal a; a=new Dog();
a.voice(); a=new
Lion();
a.voice();
}
}

Abstract:
Definetion:
An incomplete class is known as an abstract class. Methods
which are incomplete and have no body are called abstract.
• Jis method ki body ni hoti wahan abstract likhna hota.
• It is a keyword of java which is wriiten at the start
of the body.
• It is a class which has incomplete functionality.
• There are no objects in it.
• Complete class is also known as concrete class.
• Semicolon(;) is used at the end.
• Abstract class cant be instantiated.
Examples:
Voice of animals.
Program:
abstract class Animal{ abstract
void voice();
}
class Dog extends Animal{ void
voice(){
System.out.println("Bark");
} }
class Lion extends Animal{ void
voice(){
System.out.println("Roar");
} } class
Test{
public static void main(String args[]){
Animal a; a=new Dog();
a.voice(); a=new
Lion();
a.voice();
}
}
Communication(Parent class), English(Child
class),Urdu(Child class).
Program:
abstract class Communication{ abstract
void language();
} class English extends
Communication{ void language(){
System.out.println("Hello");
} }
class Urdu extends Communication{ void
language(){
System.out.println("Salam");
}
}
class Test{
public static void main(String args[]){
Communication c; c=new English();
c.language(); c=new
Urdu();
c.language();

}
}

Machine(Parent class),Laptop(Child class),Mobile(Child


class).
Program:
abstract class Machine{ abstract
void working();
}
class Laptop extends Machine{ void
working(){
System.out.println("By Keyboard");
} }
class Mobile extends Machine{ void
working(){
System.out.println("BY Touch");
} } class
Test1{
public static void main(String args[]){
Machine m; m=new Laptop(); m.working();
m=new Mobile(); m.working();

}
}
Devices(Parent class),Input(child class),Output(Child
class).
Program:
abstract class Devices{ abstract
void use();
}
class Input extends Devices{
void use(){
System.out.println("To Enter Data And Information");
} }
class Output extends Devices{
void use(){
System.out.println("To Display Result");
} } class
Test2{
public static void main(String args[]){
Devices d; d=new Input(); d.use(); d=new
Output(); d.use();

}
}
Agriculture(Parent class),Winter(Child class),Summer(Child
class).
Program:
abstract class Agriculture{ abstract
void crop();
}
class Summer extends Agriculture{ void
crop(){
System.out.println("Wheat,Rice");
} }
class Winter extends Agriculture{ void
crop(){
System.out.println("Orange,Sugarcane");
} } class
Test3{
public static void main(String args[]){
Agriculture a; a=new Summer();
a.crop(); a=new
Winter();
a.crop();

}
}

Superior(Parent class),Campus12(Child class),Campus34(Child


class).
Program:
abstract class Superior{ abstract
void department();
}
class Campus12 extends Superior{ void
department(){
System.out.println("Business Department");
}
}
class Campus34 extends Superior{ void
department(){
System.out.println("IT Department");
} } class
Test4{
public static void main(String args[]){
Superior s; s=new Campus12();
s.department(); s=new
Campus34();
s.department();
}
}
Difference between implements and extends:
Jb do cheezein same hn gi extend lafz use ho ga jesy k
class class ko extend ni krta.
Jb do different cheezein hn gi tb implement aye ga
jesy k class interface ko implement krta hai.

Packages:
Packages are just like files/directories used to group
classes of same topic.
Java me hazaroo built-in classes mojood hn.e.g.
• Text box
• Check box
• Buttons
Java me network ki bhi classes mojood hn.
Ek computer sy dusry computer contact kr skty hn.
Directory/file retrieve,review,delete etc kr skty hn.
Java me jitni bhi classes (built-in) hn unko unky
topic k mutabiq ikhata kia gya hy. Examples:
• java.io(input/output)(data read or write kr skty
hn).
• java.awt(directory).
• java.net(it is used for networking).

The above examples are the packages.


Advantage:
• It wont be conflicted.
Characteristics:
• Packages me coding ni ho gi.
• Is me class rkhi jati hn.
• Do packages k name same ni ho skty.
Import:
Import is a keyword which is used to call a particular
package to the class.
Import p2.* is used to combine a package with all the
classes.
(*) is used to for the combination of all classes with a
package.
Example: import
java.awt.*; class
Test{
public static void main(String args[]){ Frame
f=new Frame();
f.setSize(500,600);
f.setVisible(true);

}
Method Overriding:
When a child class pre-defines a parent class with same
signature.i.e,name etc then this is known as method
overriding.
Ya phr…
Jb bhi child class , parent class jesy name ki ho gi ya
same ho gi to isy method overriding kehty hn.
Method Overloading:
Making multiple methods with same name but different
parameters in the same class, it is known as method
overloading.
Ya phr…
Aik class me agr aik sy zada method bany hn gy wo bhi aik
jesy name k xth to isy method overloading kehty hn.
Computer har trh k data ko binary me convert kr leta hy
storage k liay bhi or transmission k liay bhi.
• Text.
• Image.
• Video etc.
ASCII Code:
ASCII is a standard that is used in the whole world.
Is me wo characters shamil hn jo keyboard py mojood hn.
• Numeric.
• Alphabet.
• Special characters etc.
Is me puri dunya ki languages shamil ni hoti.
Unicode:
Unicode stands for universal code.
Is me sari languages shamil hn.
Every language is supported in Unicode.

Explicit me data zaya hony k chances kam hoty hn.


Arithmatic In Java:
byte b=1;
b=b+1;
System.out.println(b);
It wont compile.
Arithmetic in Java is performed at integer level. Java
me arithmetic srf integer level pr hi perform kiay jaty
hn.
Range of byte= 127 to -128
Java type Promotion:
java compiler integer sy choti value ko integer level pr
lata hy… ta k variable range sy hr na nikly.
Java compiler choti value sy integer me promote kr deta hy.
“Arithmetic is performed at integer level in Java.”
String:
String is a collection of characters or an array of
characters.
String characters sy mil kr bnta hy.
String aik sy zada characters ko mila kr bnta hy.
String is written within “ “ . Character
is written in ‘ ’.
String is a class in Java.
Example,
String s=”Pakistan”;
int x=s.length();
char ch=s.charAt(3);

Function of string class
Most important question:
Explain five working function of string class in Java?
• int x= s.length(); it returns an
integer.
• char ch=s.charAt(3); it returns
characters at that index.
x=s.indexOf(‘t’); it returns an integer at which
the letter is present.
• x=s.indexOf(‘e’); jb character string
me mojood na ho to return value -1 ho
gi q k wo match ni krta.

indexOf() is sy hum searching kr skty hn k kon sa


character string me kis jaga pra hy yah y bhi k ni.
indexOf() is an overloading function.
x=s.indexOf(‘e’); → return -1
x=s.indexOf(“ist”); ↓
It gives the starting index
Jis ka mtlb ye hy k jaha sy string strt
Hoti hy

Agr koi bhi character repeat ho rha ho ga to wo pehly


return kry ga.
x=s.lastindexOf(‘a’);
ye end sy searching kry ga yani k string k end sy
wo character search kry ga jo ‘ ’ k andr hy.

String s1=s.toUpperCase();

Return type string ho gi or result me Pakistan capital


letters me likha aye ga “PAKISTAN”.
String s2=s.toLowerCase();
It will convert all the characters to small letters.

Camel Case Notation:


Agr function me aik sy zada word aa rhy hn to camel case
notation istemal krty hn.
e.g.
s.toUpperCase();
s.indexOf(); etc.
methods me start small letter sy krty hn or jb dusra word
ata hyto pehla letter capital rkhty hn.
Camel case notation me pehly word ka pehla character small
case me ho ga or dusra word ka pehla character uppercase me
ho ga.
Byte.
Char.
Short.
Jb bhi arithmetic upr use krn gy in ka jwb error ho ga or
ye promote kr jye gy.
Is ko typecasting krn gy.
Example, class Test{
public static void main(String args[]){
char ch=’A’; ch=(char)(ch+1);
System.out.println(ch);
for(i=65;i<90;i++){
System.out.println((char));
}
String s=”pakistan”; int
x=s.length();
System.out.println(x);
System.out.println(s.charAt(3)); x=s.indexOf(‘t’);
System.out.println(x); x=s.indexOf(“ist”);
System.out.println(x);

}
}

Static keyword:
It is not possible to count the objects of class so we use
static keyword to count the objects of the class.
Static int count=0;
It indicates that how many objects are present in the
class.
Static variable pehly bnty hn or non-static variable bad me
bnty hn.
Object bnany sy pehly static variable memory me create ho
jaty hn.
Static variable ko uski class k name k xth bhi access kia
ja skta hy.
Jb hum static keyword variable k xth istemal krty hn to wo
variable objectsy belong ni krta balky object sy bhr ho ga.
Wo object variable ni rehta so wo har object me ni hota .is
liay us ki single copy hoti hy jo object sy bhr bnti hy.
Static variable :
• Only one copy in memory.
• Static variable does not belong to the objects it
belongs to the classes.
• Belong to class.
• They are created when a class is refered.
• Static variables can be accessed with class name as
well as object variables/name. Non-Static variable:
• Multiple copies in memory.
• Non-static variables belong to objects.
• They are created upon object creation.
• They can only be accessed by the object variables.

“Static variable ki trh static methods b hoty


hn.Function k xth bhi static use kia ja ska hy.Static
humesha left side pr likhaa jata hy.”
Example, class box{ int
w,h,l; static int count;
Box(int w,int h,int l){
this.w=w; this.h=h;
this.l=l; cout++; }
static void initialize-count(){ count=0;
} } class
Test{
public static void main(String args[]){
}
}

“Non-static variable cant be accessed by static


variable. Non-static variable/methods can not be
accessed from within static context.”
Java Compiler:
Class file(byte code).
Source code compiler ko dia jata hy compiler ki output
class hoti hy.
“ Java Runtime Environment(JRE) contains JVM, class
libraries and other supporting components.”
JVM is a built-in program jo har computer me pehly sy hi
mojood hota hy chahy user install kry ya na.
Java Development Kit(JDK):
ye programming code likhny k liay or software banny k liay
program run krta hy or us sy byte code bnta hy jisy JVM
prhta hy or phr usy translate krta hy.

“Compiled Java byte code is interpreted by two different


JVM’s.”
Window Operating

Source JVM System

Code

Java JVM Linux Operating


System
Compiler

Byte Code

C Compiler:

Ye srf windows operating system pr hi run hota hy. C ka prgrm linux pr bhi ni chly ga .

Source C Compiler Execution Window OS


Code (.exe)

Java ka Byte code har operating System pr chalta hy beshak wo window ho ya linux.

JVM:

JVM stands for Java Virtual Machine. Is ka kam ye hy k ye instructions ko


byte code sy prhta hy or real time machile k mutabiq translate krta hy. “Java is
also called as Java Run-time.”

Java me agr arrays bnany hn to uska size kbhi bhi left side pr ni likhy gy e.g.
int arr[5],x; it should be
written as , int arr[
]=new int[5];

‘String args[]’ ye khud bhi aik array hy. Array ki pehchan “[ ]”hy.

Exception:

“Error in coding rule is called as Syntax Error.”

• Exception is an runtime error in java.


• Syntax error is also known as compile time error.
• Program run krny k bad jo error ayn gy jin ki waja sy program apni
execution complete ni kr skty is ko Exception kehty hn yani run time error
kehty hn.
• Exception is an abnormal condition.

Question: jb exception ata hy to kia hota hy?

• Program terminates(band ho jata hy).


• Jis line py exception ati hy us line sy program terminate ho jata hy.
 Runtime System us exception ko analyze krta hy k kis kism ka error
ayah y.

Exception ki information as an object milti hy ,object class sy bnta hy or Java


me be-shumar exception classes shamil hn.e.g.

Divisible by zero is an “Arithmetic Exception”.

“File Not Found Exception” wo hy k jb aap koi file open krna chah rhy ho or wo
delete ho chuki hy pehly sy hi.

Important:

java.lang aik default package hy ,string ,system etc jesi classes or jo classes hum
bnaty hn wo isi package me ati hn.
Null Pointer Exception:

Time t;

t.print(); it is a null pointer exception because there is no object


created.

Exception Handling :

Try-catch block is used to handle or catch exceptions.

Syntax:

Try{

Body;

}catch(exception name e){

Body;

Example:

class Test{

public static void main(String args[]){


int a,b,c; a=40; b=0

try{ c=a/b;

}catch(Arithmetic Exception e){

System.out.println(e);

}catch(Null Pointer Exception e1){


System.out.println(e);
}}

Finally Keyword:

It is a keyword which will work everytime whether there is an exception or not.

Ye humesha execute hota hy is me job hi code likha jata hy wo har haal me


execute hota hy chahy koi or code run ho ya na ye lazmi run hota hy.

Example:

class Test{

public static void main(String args[]){


int a,b,c; a=40; b=0

try{ c=a/b;

}catch(Arithmetic Exception e){

System.out.println(e);

}catch(Null Pointer Exception e1){

System.out.println(e);

}finally{

System.out.println(“Inside Finally”);

}
Checked Exceptions:
ye wo exceptions hn jinko compilation time pr hi dekha jata hy k kia ye exception
hy ya nii matlb ye k ye aye gi ya ni aye gi iska andaza ni hota or is ko catch krna
lazmi hota hy wrna program run ni ho ga.

Un-Checked Exceptions:
Ye wo exception hy jo compilation time pr ni pehchana jata k ye exception aye gi
ya ni aye gii.

Args[] is a command line argument which is used to run the program.

Blinking cursor is the promptent jiska mtlb ye hy k screen input k liye ruki hy k
user usy input dy k btaye k kia krna hy.

Built-in class ki pehchan:


Iski pehchan ye hy k jiska pehla letter capital ho ga wo built-in class ho gi.

For example,
Integer.parseInt([]):
Ye wo code hy jo aik string value ko integer value me convert krta hy.

“Try-catch block is compulsory, agr ye ni ho ga to program run ni ho ga q k us me


koi exception ho gi jo program ko run ni hony de gi or user ko usky bary me ni
pta ho ga. ”

Static me non-static ko use ni kia ja skta q k static value object bany beghair hi
call kia ja skta hy.

Custom/ User defined Exception:


User defined exception is the exception which is defined by the user .
User defined exception bnany k liye built-in class exception ky code ko inherit
krna prta hy.
For example,
class MyException extends Exception{
String msg;
MyException (String msg){
This.msg=msg;
}
public String toString();
}

Java me jitni bhi classes hn un sb ki aik na aik parent class hy. Object ye aik super
parent class hy or ye aik built-in class hy. Or java ki jitni bhii classes hn wo
directly ya indirectly Object sy hi ati hn . Object class me job hi function ho ga wo
java ki tmam classes me chala jye ga .
toString():
ye method method override ho rha hy , hum to-String ni likhty magr jb hum
Object create krty h to wo khud hi is ko call kry ga. toString() ka function khud hi
call ho jaye ga agr class na bhi kry. Ye tb hoga jb hum main class k Object ko print
krty hn.
Hum directly integer ko string me change ni kr skty is k liye different datatypes
use kiu jati hn.e.g.
int a=Integer.parseInt(args[0]);

Parent class k Object ko child class me re-create kia ja skta hy


Example,
Exception e=new MyException(“HELLO”);

Throw keyword:
User defined or anyother exception Object can be thrown through this keyword.
Exception can be thrown, jb hum khud sy exception ko throw krna chahty hn tb
hum throw keyword use krty hn, yaha hum sirf exception class ko throw kr skty
hn.

Throws Keyword:
The throws keyword is used in the function handling to create a gateway
through which exception can go to the call in function.
Ye exception ko throw krta hy. Function k end py likha jata hy or ye declare krta
hy k kis type ki exception hy.
Abstract Window ToolKit(AWT):-
Ye java ka aik package hy jis sy hum graphical user interface bana skty hn.
AWT Package contains all the classes used to create a Graphical User
Interface(GUI).
Package is just like a directory file jo k grouped hoti hy aik tarah ki classes k
sath mil kr.
Name Of Classes:
 Frame.
 Button.
 TextField.
 TextArea.
 Label.

Enter the File

Run

Hum is tarah ki window AWT Package k istmal sy bna skty hn.


Frame = aik frame banny k liye istmal hota hy.
Button= frame k upr mukhtlif jaga pr buttons bnany k liye istmal hota hy.
TextField= frame me aik field bnata hy jis me user data enter kr skta hy.
TextArea= is me aik sy zada lines user sy text ki ja skti hn.
Label= ye textfield ko label krti hy k is me kia type kr skty hn.
In tmam components k istmal sy aik screen bnaii jatiii hy.
Layout:-
Layout ka mtlb hy arrangement k kis tarah sy tmam components ko screen pr
display krn k screen achii nazar aye .
Controls ko shakal ya tarteeb deny ko layout kehty hn.
Types Of Layout:-

Border Layout:

NORTH

W E
E A
S S
T Centre T

SOUTH
Sign-in Screen Layout:-

UserName:

Password:

login

Grid Layout:-
Ye wo alignment hy jis k thorough components ko screen py layout k xth add kia
jata hy.

Anonymous Object:
Aisa Object jis ka koi name ni hy or na hi variable declare kia hy . ye time or
typing kam krny k liye banaya jata hy.
It is used by the professionals.
It can not be changed.
Can every class be anonymous?
The answer is NO.

Sirf akela border layout ya flowlayout istmal kr k hum achy programmers ni ban
skty
Ek hi screen pre k layout me dusra layout estmal kr skty hn.
What is the default layout of Frame?
Frame ka default layout border layout hy.

Panel:-
Panel invisible hota hy is ka koi Interface ni hota.
Ek layout me dusra layout istmal krny k liye panel use krty hn.
Aik panel me dusra panel or dusry me teesra panel ban skta hy.
Panel ka default layout Flowlayout hai…..

Modern programming me sequential programming ni hoti.


“now we use event-driven programming.”
Every elements can generate something with the help of certain events.
User design elements(button,textfield etc) allow to generate event.
Anonymous Listener:
Is ko koi name ni dya jata is me class name ni ho ga
Object bany ga,
p.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println(“clicked”);
}
});
Events:
GUI me user action hn , windows ko close,minimize or restore krta hy.
Textfield me kuch type kr k key press krna ,key release krna,mouse click krna
resize krna, focus change krna ,event kehlaty hn.
Wo tmam actions jo GUI me user perform krta hy usy event kehty hn.
Event Listeners:
Aisy functions jo kisi events k against call hoty hn unhn event listeners kehty
hn.e.g.click krna ,etc.
Interface ko implements krty hn jb bhi event listeners bnaty hn.
Total 8 listeners are there in java.
 ActionListener.
+actionPerformed.
 MouseListener.
+mousePressed.
+mouseReleased.
+mouseClicked.
+mouseEntered.
+mouseExited.
 FocusListener.
+focusGained.
+focusLost.
 WindowListener.
+windowActivated.
+windowDeactivated.
+windowIconified.
+windowDeiconified.
+windowOpened.
+windowClosed.
+windowClosing.
 ItemListener.
+itemStateChanged.
 KeyListener.
+keyPressed.
+keyReleased.
+keyTyped.
 MouseMotionListener.
+mouseMoved.
+mouseDragged.
Events handle krny k liye jo classes bnayii jatoi hn unhn event listeners kehty hn.
ActionListener ek abstract function hy is me action performed kia jata hy.
Adapter Classes:
Java adapter classes provide the default implementation of listener interfaces. If you inherit the
adapter class, you will not be forced to provide the implementation of all the methods of listener
interfaces. So it saves code.