Java basics
Java basics
Programming language-
Set of lines
Run that line of code
Output-
Target:
-1. Operation of system
0. Set up – Software installation and its usage
1. Programming language
Basic terminologies about language - Java
Rules to write code
- Data type
Types of data type
Sub types with example
- Variables
o Types of variables
o Examples of variable
o Understanding of usage of variable in
real time.
- Methods
Types of methods
Concept and its usage
Conditional Statements
Loops
Object oriented programming concept -
OOPS
Inheritance
Polymorphism
Encapsulation
Abstract
Interface
Exception handling
String class
Practice problems
Casting
Collection
Map
Selenium
- Setup and configuration
- Basic concept
- Java and Selenium combination
o Basic controlling of the browser
o Webelements
o Handling of multiple elements
Text field
Dropdown
Calendar
Hover
Page switching
Iframe
Window handling
o Actions class
o Screenshot
o Scroll
o Git
o Jenkins
System.out.println("This is my first
line of program");
System.out.println("This is my second
line of program");
System.out.println("This is my third line
of program");
Output:
To check the output press the green button to execute it:
Java is a strongly typed language which means every line which has
data must have some type otherwise we will get compilation error.
To define a specific type to the data is called as Datatype.
1. Primitive
2. Non Primitive
There are 2 categories of datatype:
1. Numeric
2. Non numeric
1. Numeric:
a. Integral data type:
i. byte: Size of the byte is 1 byte
range of byte is -128 to 127 (-27 to 27-1)
System.out.println(var1);
Example:
short var = 2;
System.out.println(var);
int i = 2;
int j = 128;
int k = 32767;
int l = 2147483647;
int m = -2147483648;
System.out.println(l);
example:
long u = 45l;
System.out.println(u);
Example:
float f = 58.33f;
float g = 8989898.544566f;
System.out.println(f);
Output:
58.33
double d = 89.96;
double w =
899898989898989.6565656565656566;
System.out.println(d);
Output:
89.96
System.out.println(var3);
boolean c = false;
System.out.println(c);
Addition program:
public static void main(String[] args) {
int i = 10;
int j = 20;
int k = i+j;
System.out.println(k);
int i = 10;
int j = 20;
int k = i+j;
System.out.println(k);//30
// substraction
int l = 60;
int m = 10;
int n = l-m;
System.out.println(n);//50
// Multiplication
int z = 20;
int y = 80;
int x = z*y;
System.out.println(x);//1600
// Division
double e = 100000000;
double f = 565656;
double g = e/f;
System.out.println(g);//
176.78589107160536
Output:
30
50
1600
176.78589107160536
Example:
public static void main(String[] args) {
// datatype variablename = value;
System.out.println(s);//this is string
value
Output:
this is string value
Note:
System.out.println(s);//this is string
value
// 1. Concatenation +
String s1 = "Pune";
String s2 = "Mahanagar";
String s3 = s1+s2;
System.out.println(s3);//PuneMahanagar
System.out.println(s4);
Output:
this is string value
PuneMahanagar
Pune Mahanagar
Example:
String s5 = "Mumbai";
String s7 = s6+s5;
Example:
String s5 = "Mumbai";
String s6 = "India's financial capital is ";
System.out.println(s6+s5);//India's financial
capital is Mumbai
int a = 10;
int b = 20;
System.out.println(a+b);//30
Example:
int c = 50;
int d = 20;
int e = c+d;
System.out.println(e);//70
System.out.println("The addition of c
and d is "+e);// The addition of c and d is 70
Example 2:
int z = 100;
int y = 20;
int e = z+y;
System.out.println(e);//70
System.out.println("The addition of
"+z+" and "+y+" is "+e);// The addition of 100
and 20 is 120
Output:
this is string value
PuneMahanagar
Pune Mahanagar
India's financial capital is Mumbai
India's financial capital is Mumbai
30
120
The addition of 100 and 20 is 120
Solution:;
int a = 10;
int b = 20;
int c = b-a;
System.out.println("The difference
between "+a+" and "+b+" is "+c);
// multiplication
int d = a*b;
System.out.println("The multipliation
of "+a+" and "+b+" is "+d);
// division
int e = b/a;
System.out.println("The division of
"+b+" by "+a+" is "+e);
Output:
The difference between 10 and 20 is 10
The multipliation of 10 and 20 is 200
The division of 20 by 10 is 2
Rules for String with number:
1. Whenever we perform any thing with string
then the output value will always be String
only
2. Evaluation in the () bracket will happen
only in left to right direction
Examples:
// String with numbers:
int z = 100;
int y = 20;
int e = z+y;
System.out.println(e);//70
System.out.println("The addition of
"+z+" and "+y+" is "+e);// The addition of c
and d is 70
System.out.println("**************************
***************");
System.out.println(20+2);//22
System.out.println("20"+"2");//202
System.out.println("20"+2);//202
System.out.println("20"+2+2);//2022
System.out.println("50"+true);//50true
System.out.println(5+3+"53");//853
}
Methods: The entity which defines the
behaviour of the object is called as Method.
Inside the method we can define the logic
which can be executed as per our requirement.
To execute the method we have to call it.
Example:
public class StaticMethod {
// Syntax:
// public static void method_name()
// {
// logic which we want to run
// }
methodOne();
Output:
hello
hello
hello
hello
hello
Example 2:
public class StaticMethod {
// Syntax:
// public static void method_name()
// {
// logic which we want to run
// }
int b = 50;
int c = a+b;
System.out.println(c);
}
addition();
methodOne();
methodOne();
methodOne();
addition();
}
Output:
60
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
60
}
Assignment:
WAP to create a calculator class which can
perform addition, multiplication, subtraction
and division operation :
Solution:
public class Calculator {
int b = 50;
int c = a+b;
System.out.println(c);
}
int b = 50;
int c = a-b;
System.out.println(c);
}
int b = 50;
int c = a*b;
System.out.println(c);
}
double b = 50;
double c = a/b;
System.out.println(c);
}
addition();//60
substraction();//-40
multiplication();//500
division();//0.2
Output:
60
-40
500
0.2
Example:
public class StaticMethod {
// Syntax:
// public static void method_name()
// {
// logic which we want to run
// }
int b = 50;
int c = a+b;
System.out.println(c);
}
StaticMethod.methodOne();
StaticMethod.addition();
}
}
Output:
hello
hello
hello
hello
hello
60
Syntax:
// public void name_of_method()
// {
// logic which is to be executed
// }
Example:
public class NonStaticMethod {
public void m1()
{
System.out.println("m1 non static
method from NonStaticMethod class");
}
nsm.m1();
Output:
m1 non static method from NonStaticMethod
class
Example 2:
// Syntax:
// public void name_of_method()
// {
// logic which is to be executed
// }
// create object:
// Syntax:
// classname variable_name = new
classname();
nsm.m1();
nsm.m1();
nsm.m2();
Output:
m1 non static method from NonStaticMethod
class
m1 non static method from NonStaticMethod
class
Non static method m2
Example 3:
// Syntax:
// public void name_of_method()
// {
// logic which is to be executed
// }
// create object:
// Syntax:
// classname variable_name = new
classname();
Output:
m1 non static method from NonStaticMethod
class
m1 non static method from NonStaticMethod
class
Non static method m2
m1 non static method from NonStaticMethod
class
m1 non static method from NonStaticMethod
class
1. static variable
2. non static variable
3. local variable
System.out.println(a);//50
System.out.println(StaticVariable.a);//50
System.out.println(StaticVariable.a);
Example 3:
public class StaticVariable {
System.out.println(a);//50
System.out.println(StaticVariable.a);//50
a = 90;
System.out.println(a);//90
Output:
50
50
90
Example 4:
public class StaticVariable {
System.out.println(StaticVariable.a);//50
StaticVariable.a = 60;
System.out.println(StaticVariable.a);//60
Output:
50
60
String s = "abc";
int i = 10;
System.out.println(nsv.s);//abc
NonStaticVariable nsv1 = new
NonStaticVariable();
System.out.println(nsv1.i);//10
System.out.println(nsv.i);//10
nsv.s = "def";
System.out.println(nsv.s);
}
}
Output:
abc
10
10
def
Example 2: Calling inside other class:
System.out.println(StaticVariable.a);//50
StaticVariable.a = 60;
System.out.println(StaticVariable.a);//60
System.out.println("**************************
***");
System.out.println(var.i);
System.out.println(var.s);
Output:
50
60
*****************************
10
Abc
Example:
public class G {
static int i = 50; // static variable
System.out.println(i);//50
G g1 = new G();
System.out.println(g1.j);//80
g1.j = 70;
System.out.println(g1.j);//70
g1.j = 100;
System.out.println(g1.j);//100
G g2 = new G();
System.out.println(g2.j);//80
System.out.println(g1.j);//100
System.out.println("**************************
****************");
System.out.println(i);//50
i = 90;
System.out.println(i);//90
System.out.println(g1.i);//90
System.out.println(g2.i);//90
g1.i = 120;
System.out.println(g1.i);//120
System.out.println(g2.i);//120
System.out.println(i);//120
Output:
50
80
70
100
80
100
******************************************
50
90
90
90
120
120
120
System.out.println(i);
}
System.out.println(l);//80
System.out.println(s);//abc
Output:
80
abc
Calling of one method into another method:
a. Calling of static method into another
static method.
b. Calling of static method into non-static
method
c. Calling of non-static method into static
method
d. Calling of non-static method into another
non-static method
public class A {
}
Output:
m1 method
m2 method
A a = new A();
a.m3();
}
Output:
m3 method
m1 method
Output:
m5 method
m4 method
d. Calling of non static into another non
static:
public class B {
B b = new B();
b.m7();
}
Output:
non static m6 method
non static m7 method
Example:
public class B {
public class C {
b.m6();
System.out.println("Method 8");
}
C c = new C();
c.m8();
Example:
public class Test3 {
int i = 50;
System.out.println(i);//80
System.out.println(this.i);//50
int i = 90;
System.out.println(i);
}
t3.m1();
}
Output:
80
50
String k = "def";
System.out.println(i);//60
System.out.println(this.i);
System.out.println(k);//def
System.out.println(this.k);//def
System.out.println(this.j);//90
System.out.println(j);//90
H h = new H();
System.out.println(h.i);//50
// System.out.println(this.j);// this
keyword is not applicable inside the static
area
}
// System.out.println(this.i);// this
keyword is not applicable inside the static
area
System.out.println(j);
Output:
90
College program:
public class College {
String name;
static String collegename;
int age;
int mathsmarks;
int physicsmarks;
student1.name = "Albert";
student1.age = 12;
student1.mathsmarks = 50;
student1.physicsmarks = 40;
collegename = "Coep";
student2.name = "Daniel";
student2.age = 21;
student2.mathsmarks = 60;
student2.physicsmarks = 70;
System.out.println(student2.name);//Daniel
System.out.println(student1.physicsmarks);//40
}
}
Output:
Daniel
40
Assignment:
WAP to store the values of following variables
for 3 customers:
Customername
Mobilenumber
Serviceprovider
Cityname
Solution:
public class MobileSubscriber {
String customername;
long mobile;
static String companyname;
static String cityname;
}
Default value: If any of the non-static /
static variable don’t have the values assigned
then they will by default have default values
stored init.
A a = new A();
a.m2(60);
a.m2(80);
Output:
60
80
Example:
public void m3(int i)
{
int j = i+2;
System.out.println(j);
}
Output:
10
Example:
public void m4(int h, int k)
{
int l = h+k;
System.out.println(l);
}
A a = new A();
a.m4(9, 2);
}
Output:
11
Assignment :
WAP to create the calculator which should be
able to perform addition, multiplication,
substraction and division
return 70;
String id = name+roll;
return id;
}
B b = new B();
System.out.println(var);//70
int y = var + 2;
System.out.println(y);//72
String idvalue = b.uniqueId();
System.out.println(idvalue);//Ron50
System.out.println(finalcode);//coepRon50
}
}
Output:
This is method with return and no arg
70
72
Ron50
coepRon50
System.out.println(word);
return word;
}
return 50;
}
public static void main(String[] args) {
String s1= m9("India",7);//India 7
System.out.println(area);// India 7
largest country
System.out.println(m9("Russia", 1));
C c = new C();
System.out.println(c.m10("abc",
false));//50
Output:
India 7
India 7 largest country
Russia 1
Russia 1
50
Solution:
public class AreaCalc {
return landArea;
System.out.println("Total area is
"+totalArea);
Output:
4300
Operators:
1. Arithmetic operator:
// addition '+'
// substraction '-'
// multiplication '*'
// division '/'
// modulus '%'
public static void main(String[] args) {
int i = 2;
int j = 10;
int k = j/i;
System.out.println(k);//5
System.out.println(l);//0
int o = 10 % 3;
System.out.println(o);//1
}
}
Output:
5
0
1
2. Conditional operators:
// greater than '>'
System.out.println(isGreater);//true
int u = 90;
int v = 78;
boolean g = u>v;
System.out.println(g);//true
int t = 40;
int e = 60;
boolean r = e<t;
System.out.println(r);
int y = 90;
int j = 90;
boolean h = y >= j;
System.out.println(h);// true
Output
True
int p = 87;
int q = 96;
boolean w = p <= q;
System.out.println(w);// true
// equal to '=='
int s = 23;
int f = 62;
boolean c = s==f;
System.out.println(c);//false
Assignment:
// WAP to verify all the given sides of a
structure is forming a square or not
// a = 10
// b= 10
// c = 10
// d = 10
If and else:
// syntax for if - else
// if(boolean_condition)
// {
// actions to be executed if
boolean_condition is true
// }
// else
// {
// actions to be executed if
boolean_condition is false
// }
Example:
int n = 56;
int x = 96;
if(n>=x)
{
System.out.println("n is greater than
x");
}
else
{
System.out.println("n is smaller
than x");
}
Output:
n is smaller than x
OR
False False False
False True True
True False True
True True True
Examples:
// AND -> &&
// OR -> ||
int i = 50;
int j = 70;
int k = 80;
// false true
boolean y = i > 60 && k < 100;
System.out.println(y);// false
System.out.println(z);// true
System.out.println(u);// true
// NOT -> !
boolean r = false;
boolean o = !r;
System.out.println(o);// true
int p = 90;
int q = 101;
System.out.println(e);// true
boolean w = q != 90;
System.out.println(w);// true
}
// while(boolean_condition)
// {
// Action to be executed if condition
is true
// }
int i = 5;//6//7//8//9//10
i = i +1;//7//8//9//10
}
Output:
Hello
Hello
Hello
Hello
Hello
Solution:
public static void main(String[] args) {
int number = 1;
while(number<=10)
{
number = number + 1;
}
Output:
2
4
6
8
10
// syntax:
// do
// {
// //Actions to be executed
// }
// while(boolean_condition);
int i = 5;
do
{
System.out.println("Hello");
i = i +1;
}
while(i>10);
}
Output:
Hello
For loop:
Example:
int i = 50;
int j = 60;
for(System.out.println("first statement");
i<j; System.out.println("third statement"))
{
System.out.println("Actions are
executing");
i = i+1;
}
Output
first statement
Actions are executing
third statement
Actions are executing
third statement
Actions are executing
third statement
Actions are executing
third statement
Actions are executing
third statement
Actions are executing
third statement
Actions are executing
third statement
Actions are executing
third statement
Actions are executing
third statement
Actions are executing
third statement
Example 2:
public static void main(String[] args) {
int number = 2;
for(int i = 1; i<=10; i= i+1 )
{
int j = number*i;
System.out.println(j);//0, 2, 4,
}
}
Output:
2
4
6
8
10
12
14
16
18
20
Assignment:
WAP to print the table of 2 like:
2 x 1 = 2
2 x 2 = 4
System.out.println(number+" x "+i+"
= "+multiResult);
}
}
Assignment:
WAP using for loop to print keeping the use of
* inside the program only once:
*****
Solution:
public static void main(String[] args) {
System.out.println("123");// first
print the automatically change the line
System.out.println("456");
System.out.print("789");// just print
the value but not change the line
System.out.println("012");
}
Output:
123
456
789012
Note: If we write println then after
printing it has to change the line but if we
don’t write ln then it will just print but
not change the line after printing the
statement.
int x = 10;
System.out.println(y);//10
System.out.println(x);//11
System.out.println("**************************
******************");
int u = 10;
int v = ++u;// first increment and then
transfer the value to v
System.out.println(u);//11
System.out.println(v);//11
System.out.println("**************************
******************");
int p = 10;
int q = p--;// first transfer the value
to q and then decrement the value of p
System.out.println(p);//9
System.out.println(q);//10
System.out.println("**************************
******************");
int c = 10;
int d = --c;// first decrement and then
transfer the value to d
System.out.println(c);//9
System.out.println(d);//9
Output:
10
11
********************************************
11
11
********************************************
9
10
********************************************
9
9
Assignment:
WAP to print the pattern like:
*****
*****
*****
*****
*****
Solution:
public static void main(String[] args) {
System.out.println();
}
}
Output:
*****
*****
*****
*****
*****
Break keyword: It is a keyword which can only
be used inside loops and once it gets execute
then it will terminate that loop right away
and comes out from the program.
Example:
public static void main(String[] args) {
int number = 2;
for(int i=1; i<=10; i= i+1)
{
int multiResult= number*i;
System.out.println(number+" x
"+i+" = "+multiResult);
if(i==5)
{
break;
}
Output:
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
if(remainder ==0)
{
continue;
}
System.out.println(i);
}
}
Output:
1
3
5
7
9
Constructor:
Definition: It is a special block which gets
execute once object gets created. Constructor
get called automatically once we execute the
object statement. The area of constructor is
also non static area.
// Syntax:
// public name_of_class()
// {
// actions to be executed when constructor
gets execute
// }
Example:
public Test()
{
System.out.println("Constructor is
executing");
}
Output:
Constructor is executing
There are 2 types of constructor:
1. Default constructor
2. User defined constructor
Example:
// public name_of_class()
// {
// actions to be executed when constructor
gets execute
// }
public Test()
{
System.out.println("Constructor is
executing");
}
Output:
Constructor is executing
Rules of constructor:
Example:
public class A {
// Multiple constructors inside the class
public A()
{
System.out.println("zero argument
constructor");
}
public A(int i)
{
System.out.println("One argument
constructor");
System.out.println("Value of argument
is "+i);
}
A a1 = new A();
A a2 = new A(10);
Output:
zero argument constructor
One argument constructor
Value of argument is 10
public A()
{
System.out.println("zero argument
constructor");
}
public A(int i)
{
this();
System.out.println("One argument
constructor");
System.out.println("Value of argument
is "+i);
}
A a1 = new A();
A a2 = new A(10);
}
Output:
zero argument constructor
zero argument constructor
One argument constructor
Value of argument is 10
Example 2:
public A()
{
System.out.println("zero argument
constructor");
System.out.println("**************Zero
argument*****************");
}
public A(int i)
{
this();
System.out.println("One argument
constructor");
System.out.println("Value of argument
is "+i);
System.out.println("**************one
argument*****************");
}
System.out.println("2 argument
constructor");
System.out.println(s+b);
System.out.println("**************two
argument*****************");
}
public static void main(String[] args) {
A a1 = new A();
A a2 = new A(10);
Output:
zero argument constructor
**************Zero argument*****************
zero argument constructor
**************Zero argument*****************
One argument constructor
Value of argument is 10
**************one argument*****************
zero argument constructor
**************Zero argument*****************
One argument constructor
Value of argument is 60
**************one argument*****************
2 argument constructor
abcfalse
**************two argument*****************
public Pcons()
{
System.out.println("Zero argument
constructor from Pcons");
}
}
public Ccons(int i)
{
System.out.println("one argument child
class constructor");
}
Usage of constructor:
String name;
static String collegename;
int age;
int mathsmarks;
int physicsmarks;
System.out.println(s1.name);
Output:
Daniel
System.out.println(s1.name);
}
Output:
Daniel
Code:
public static void main(String[] args) {
for(int i=0; i<=5; i++)
{
System.out.println();
}
}
Output:
*
**
***
****
*****
Assignment:
WAP to draw the star patter for these :
// *
// **
// ***
// ****
// *****
System.out.println();
}
}
Output:
*
**
***
****
*****
OOPs concept:
Inheritance: It is a process in which we
acquire the property of one class into another
class by using extends keyword to connect
those classes.
Example:
package inheritance;
public class A {
package inheritance;
B b = new B();
b.m4();
b.m1();
b.m2();
m3();
}
}
Example 2:
public class Parent {
}
public class Child extends Parent
{
c.home();
c.car();
c.bike();
}
}
int i = 20;
System.out.println(c.i);
System.out.println(s);
}
}
Output:
20
Abc
System.out.println(super.ps1);//Pune
System.out.println("m4 method");
}
B b = new B();
b.m4();
}
Exaample:
public class C {
} public class D {
}
public class E extends C, D{-- here we get
error
E e = new E();
e.profit();
public Pcons()
{
System.out.println("Zero argument
constructor from Pcons");
}
}
public Ccons(int i)
{
System.out.println("one argument child
class constructor");
}
Example:
public class Pcons {
public Pcons(String s)
{
System.out.println(s);
System.out.println("one argument
constructor from Pcons");
}
}
public Ccons(int i)
{
super("abc");
System.out.println("one argument child
class constructor");
}
Output:
abc
one argument constructor from Pcons
one argument child class constructor
It applicable for:
1. Class
2. Methods
3. Variables etc.
Example:
}
b. <default>: If a class is declared as
<default> then it is accessible only
within the package but not outside the
package.
To define the default class we just don’t
write any access modifier to declare a
class as default.
Example:
class Test2 {
Example 2:
final class Test3 { --- using <default>
Example:
public static int i = 50;
public static void m1()
{
System.out.println("public m1 method
from MT class");
}
Example:
class MT {
Example:
private boolean b = false;
private void m3()
{
System.out.println("Private m3 method");
MT mt = new MT();
mt.m3();
}
Example:
package accessmodifiers;
public class A {
}
package accesstest;
import accessmodifiers.A;
A a = new A();
B b = new B();
package accesstest;
import accessmodifiers.A;
A a = new A();
// b.m1();
C c = new C();
c.m1();
Example:
public class Parent {
System.out.println(p.i);
System.out.println(j);
p.i = 80;
j = 40;
}
4. Any class can be declared as abstract if it
doesn’t have any abstract method or no methods
inside it.
Example:
Example:
}
public void f4() {
System.out.println("f4 code completion
inside B class");
B b = new B();
b.f1();
b.f2();
b.f5();
}
}
Output:
feature 1 code
feature 2 code
f5 code completion inside B class
Example 2:
public abstract class A {
}
public void f5()
{
System.out.println("f5 code completion
inside B class");
}
public class C extends B
{
public void f6() {
System.out.println("f6 from c class");
}
public static void main(String[] args) {
C c = new C();
c.f1();
c.f2();
c.f4();
c.f6();
}
}
Output:
feature 1 code
feature 2 code
f4 code completion inside B class
f6 from c class
public A()
{
System.out.println("zero argument
constructor");
}
}
public abstract class B extends A{
}
public void f4() {
System.out.println("f4 code completion
inside B class");
}
public class C extends B
{
public void f6() {
System.out.println("f6 from c class");
}
public static void main(String[] args) {
C c = new C();
c.f1();
c.f2();
c.f4();
c.f6();
}
Example:
public class A {
Example:
System.out.println(s);
}
public class A {
public A()
{
System.out.println("0 argument
constructor");
}
public A(int i)
{
System.out.println("1 argument
constructor");
}
Example:
public class Parent {
c.home();
c.furniture();
}
Output:
Home from parent
Furniture method from Child class
Example:
public class Parent {
}
}
public class Child extends Parent {
}
}
Example:
public class Parent {
}
}
public class Child extends Parent {
int home()
{
System.out.println("Child class home
method");
int rent = 15000;
return rent;
}
}
Example:
package polymorphism;
public class B {
}
package polymorphism;
C c = new C();
c.m1();//C class m1 method
B b = new B();
B bb = new C();
}
}
Output:
C class m1 method
B class m1 method
C class m1 method
C class m2 method
B class m3 method
Example:
public class B {
public static void m5()
{
System.out.println("m5 static method
from B class");
}
C c = new C();
B b = new B();
B bb = new C();
c.m5();
b.m5();
bb.m5();
Output:
m5 static method from C class
m5 static method from B class
m5 static method from B class
int i = 50;
int i = 60;
C c = new C();
B b = new B();
B bb = new C();
System.out.println(c.i);//60
System.out.println(b.i);//50
System.out.println(bb.i);//50
}
8. Constructor doesn’t follow overriding as
the name of constructor must be same as the
name of class.
Difference between overloading and overriding:
Sr. no Overloading Overriding
1 Method name is Method name is same
same but arguments but arguments
are different should also be same
atleast order must is called as
be different is Overriding.
called as
Overloading.
2 Method execution / Method execution /
resolution based resolution based on
on reference runtime object.
variable
3 Static and non Only non static
static method method follows
follow overriding.
overloading.
4 Method return type Method return type
doesn’t make must be same for
difference for overriding.
overloading.
5 Access modifier Access modifier
can be any for should have greater
method scope inside the
overloading. child class or
should have the
same scope but it
cannot have lesser
scope to call the
method as
overridden.
6 Constructor follow Constructor doesn’t
overloading follow overriding.
7 It is also called It is also called
as Static as Dynamic
polymorphism and polymorphism and
compile time run time
polymorphism. polymorphism.
8 Final method can Final method cannot
be overloaded be overridden.
Through polymorphism we have achieved
Portability.
Encapsulation:
It is an oops concept through which we can achieve
the security.
System.out.println(customerBalance);
}
else
{
System.out.println("Incorrect pin
please try again");
}
}
else
{
System.out.println("Incorrect pin
please try again");
}
}
}
server.getBalance(1234);//Abstraction
server.setBalance(1234, 20000);
Output:
50000
Your account balance is :30000
System.out.println(customerBalance);
}
else
{
System.out.println("Incorrect pin
please try again");
}
}
Setter method: A method whose name starts with
‘set’ word and it is used to update the
information then it is called as Setter
method.
public void setBalance(int pin, int amount)
{
if (pin == 1234)
{
if(amount<customerBalance)
{
customerBalance=
customerBalance - amount;
System.out.println("Your
account balance is :"+customerBalance);
}
}
else
{
System.out.println("Incorrect pin
please try again");
}
}
Solution:
package assignmentofmulticlassmethodcalling;
public class A {
}
package assignmentofmulticlassmethodcalling;
public class B {
public class C {
}
package assignmentofmulticlassmethodcalling;
a.m3();
b.m4();
c.m9();
System.out.println("*******M10 method
end******************");
}
c.m7();
a.m2();
System.out.println("*******M11 method
end******************");
}
D d = new D();
d.m10();
d.m11();
}
package
assignmentofmulticlassmethodcalling;
public class E {
A a;
B b;
C c;
public E()
{
a = new A();
b = new B();
c = new C();
}
void m3();
}
package interfacediscussion;
System.out.println("This m1 from A
class");
}
System.out.println("This m2 from A
class");
}
A a = new A();
a.m1();
a.m2();
a.m3();
Output:
This m1 from A class
This m2 from A class
This m2 from A class
Example 2:
void m3();
System.out.println("This m1 from A
class");
}
System.out.println("This m2 from A
class");
}
}
B b = new B();
b.m2();
b.m1();
b.m3();
Output:
this is method 2 from B class
This m1 from A class
This m2 from A class
@Override
public void m5() {
// TODO Auto-generated method stub
}
@Override
public void m6() {
// TODO Auto-generated method stub
@Override
public void m4() {
// TODO Auto-generated method stub
Example:
public interface Interface3 {
}
public interface Interface1 {
}
public interface Interface2 {
@Override
public void m4() {
// TODO Auto-generated method stub
}
@Override
public void m5() {
// TODO Auto-generated method stub
@Override
public void m6() {
// TODO Auto-generated method stub
@Override
public void m1() {
// TODO Auto-generated method stub
@Override
public void m2() {
// TODO Auto-generated method stub
@Override
public void m3() {
// TODO Auto-generated method stub
Example:
public interface Interface4 {
}
public interface Interface5 extends
Interface4{
@Override
public void m1() {
// TODO Auto-generated method stub
@Override
public void m2() {
// TODO Auto-generated method stub
}
@Override
public void m3() {
// TODO Auto-generated method stub
@Override
public void m4() {
// TODO Auto-generated method stub
@Override
public void m5() {
// TODO Auto-generated method stub
@Override
public void m6() {
// TODO Auto-generated method stub
}
package interfacediscussion;
}
package interfacediscussion;
@Override
public void m18() {
// TODO Auto-generated method stub
@Override
public void m15() {
// TODO Auto-generated method stub
}
@Override
public void m16() {
// TODO Auto-generated method stub
Example:
public interface StaticMethodInterface {
m1();
StaticMethodInterface.m1();
}
}
Output:
static method from interface
static method from interface
Example:
public interface StaticMethodInterface {
int i = 20;
m1();
StaticMethodInterface.m1();
System.out.println(StaticMethodInterface.i);
}
}
Output:
static method from interface
static method from interface
20
Sr Interface Abstract
no
1 All the methods We can define
inside an interface complete as well as
are by default incomplete methods
public and abstract inside the class.
whether we define it
or not but static
method is an
exception.
2. All methods and Methods and
variable are by variable inside the
default public. abstract class need
not be public.
3 Constructor is not Constructor is
allowed inside the allowed.
interface
4 If we don’t know If we have partial
about the information then we
implementation then can use abstract.
of anything then we
can use interface.
5 Interface must be Abstract class
implements by a should be extends
class another class.
6 Every variable Every variable need
inside an interface not be public
are by default static and final.
public static and
final.
Exception handling:
Definition: Any unwanted event occurs during
the execution of code which cause the
termination of program abnormally then it is
called as Exception.
And when we handle this situation after
exception arrived and the code gets terminated
normally then it is called as Exception
handling.
Example:
package exceptionhandling;
System.out.println("first line");
int k;
try {// all risky code
int i = 10;
int j = 0;
k = i/j;
}
catch(ArithmeticException e)
{
System.out.println("Exception
arrived hence executing the catch block");
k= 2;
System.out.println(k);
System.out.println("20th line");
System.out.println("last line");
}
}
Output:
first line
Exception arrived hence executing the catch
block
2
20th line
last line
Example:
public class Test {
System.out.println("first line");
int k;
try {// all risky code
int i = 10;
int j = 0;
k = i / j;
System.out.println("inside try
after the exception");
} catch (ArithmeticException e) {
System.out.println("Exception
arrived hence executing the catch block");
k = 2;
System.out.println(k);
System.out.println("20th line");
System.out.println("last line");
}
Example:
public static void main(String[] args) {
System.out.println("first line");
int k;
try {// all risky code
int i = 10;
int j = 0;
k = i / j;
System.out.println("inside try
after the exception");
catch (NullPointerException e) {
System.out.println("Exception
arrived hence executing the catch block");
k = 2;
}
System.out.println(k);
System.out.println("20th line");
System.out.println("last line");
}
Output:
first line
Exception in thread "main"
java.lang.ArithmeticException: / by zero
at
exceptionhandling.Test2.main(Test2.java:16)
public class A {
}
package exceptionhandling;
public class B {
A a;
a.m1();
a.m2();
b.m3();
}
Output:
m3 method from B class
Exception in thread "main"
java.lang.NullPointerException: Cannot invoke
"exceptionhandling.A.m1()" because "this.a" is
null
at exceptionhandling.B.m3(B.java:10)
at exceptionhandling.B.main(B.java:19)
Example:
try
{
int i = 10/0;
}
catch (NullPointerException e) {
System.out.println("Null pointer
exception arrived");
}
catch (ArrayIndexOutOfBoundsException
a) {
}
catch(StringIndexOutOfBoundsException
r)
{
System.out.println("String index
out of bound");
}
}
Output:
Exception in thread "main"
java.lang.ArithmeticException: / by zero
at
exceptionhandling.TryCatchCombination.main(Try
CatchCombination.java:10)
Example:
public static void main(String[] args) {
try
{
int i = 10/0;
}
catch (NullPointerException e) {
System.out.println("Null pointer
exception arrived");
}
catch (ArrayIndexOutOfBoundsException
a) {
}
catch(StringIndexOutOfBoundsException
r)
{
System.out.println("String index
out of bound");
}
catch (RuntimeException e) {
System.out.println("Run time
Exception catch block executing");
Output
Run time Exception catch block executing
Note: Here ArithmeticException arrived hence
it can be handled by its parent i.e
RuntimeException and it will be able to handle
the exception. So we should always write the
parent class exception at the last.
IF we write parent first and then child then
child class catch block code will become
unreachable code and we get compile time
error.
Try – catch – finally:
try
{
int i = 10;
int j = 0;
int k = i/j;
catch (ArithmeticException e) {
System.out.println("NPE");
}
finally
{
System.out.println("Finally is
executing");
Output:
NPE
Finally is executing
Example 2:
public static void main(String[] args) {
try
{
int i = 10;
int j = 0;
int k = i/j;
catch (NullPointerException e) {
System.out.println("NPE");
}
finally
{
System.out.println("Finally is
executing");
}
}
Output:
Finally is executing
Exception in thread "main"
java.lang.ArithmeticException: / by zero
at
exceptionhandling.TryCatchFinally.main(TryCatc
hFinally.java:11)
Categories of exception:
There are 2 categories of exception:
1. Checked : whenever we write the code and
compiler suggests to handle the exception then
this category is called as Checked category of
exception.
Example:
System.out.println("lastline of try
block");
catch (FileNotFoundException e)
{
System.out.println("File not found
exception arrived");
}
}
}
Example 2:
System.out.println("This is first
line");
try {
Thread.sleep(5000);
}
catch (InterruptedException e)
{
System.out.println("Catch block is
executing");
}
System.out.println("This is last
line");
}
Output:
This is first line
This is last line
System.out.println("first line");
int k;
try {// all risky code
int i = 10;
int j = 0;
k = i / j;
System.out.println("inside try
after the exception");
catch (ArithmeticException e) {
System.out.println("Exception
arrived hence executing the catch block");
k = 2;
Example:
public class ThrowKeyword {
int i = 50;
int j = 20;
if(i>j)
{
throw new
NullPointerException("something went wrong
please try again later !!!");
}
}
Output:
Exception in thread "main"
java.lang.NullPointerException: something went
wrong please try again later !!!
at
exceptionhandling.ThrowKeyword.main(ThrowKeywo
rd.java:14)
Output:
M2 method
M1 method
M2 method
M1 method
M2 methException in thread "main"
java.lang.StackOverflowError
at
java.base/java.io.PrintStream.writeln(PrintStr
eam.java:722)
at
java.base/java.io.PrintStream.println(PrintStr
eam.java:1028)
Arrays in Java:
Definition: Group of homogenous elements when
represented by a single entity is called as
Array.
i[0] = 50;
i[1] = 90;
i[2] = 2;
i[3] = 96;
i[4] = 48;
System.out.println(i[0]);
}
Output:
50
Example
public static void main(String[] args) {
// syntax to define array:
// data_type [] variable_Name = new
data_type [size-of-array];
i[0] = 50;
i[1] = 90;
i[2] = 2;
i[3] = 96;
i[4] = 48;
System.out.println(sizeOFArray);
}
Output:
5
Assignment:
// Assignment: WAP to print all the elements
present inside the array using for loop
i[0] = 50;
i[1] = 90;
i[2] = 2;
i[3] = 96;
i[4] = 48;
i[5] = 96;// this line will throw the
exception as array size is 5 which is defined
hence we cannot provide more than 5
System.out.println(i[0]);//50
}
Output:
Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException:
Index 5 out of bounds for length 5
at arraydisc.Test.main(Test.java:17)
Sorting of array :
public static void main(String[] args) {
Arrays.sort(a);
Output:
2
5
76
85
98
102
Example
:
for(int t:a)
{
System.out.println(t);
}
s[0] = "Pune";
s[1] = "Bangalore";
s[2] = "Delhi";
for(String ss:s)
{
System.out.println(ss);
}
}
Output:
85
5
2
76
102
98
Pune
Bangalore
Delhi
int a [] = {0,1,0,1,1,1,1,0,0,0};
Arrays.sort(a);
for(int aa :a)
{
System.out.print(aa);
}
}
Output:
0000011111
WAP to swap 2 numbers
A= 5;
B= 6;
Solution:
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = a;
a = b;
b = c;
System.out.println(a);
System.out.println(b);
Output:
20
10
System.out.println(sum);
Output:
14
Solution:
public static void main(String[] args) {
if(!isCorrect)
{
System.out.println(a[i]+1);
}
}
}
Output:
5
int var;
a[j] = a[i];
a[i] = var;
}
}
}
for(int aa :a)
{
System.out.print(aa+" ");
}
}
Output:
96 82 56 10 0
for(int bb:b) {
System.out.println(bb);
}
Output:
10
56
0
82
96
Solution:
public static void main(String[] args) {
System.out.print(bb+" ,");
}
System.out.println();
System.out.println("**************************
*******");
Arrays.sort(b);
System.out.println("Smallest element from b
array is "+b[0]);
}
if(a[i]==b[lastIndex])
{
System.out.println("Largest element
is "+i);
largestindex = i;
}
int c = 0;
c = a[smallestindex];
a[smallestindex] = a[largestindex];
a[largestindex] = c;
for(int aa:a)
{
System.out.print(aa+" ");
}
// Steps
// sort b array
// find out the largest and smallest number of
b
// Find out the index position of smallest and
largest number in a
// Swap the index position in array a
Output:
b array is
10 ,56 ,0 ,82 ,96 ,
*********************************
Smallest element from b array is 0
Largest element from b array is 96
Smallest element index is 2
Largest element is 4
10 56 96 82 0
String class:
String is a class present inside java.lang
package. It is an immutable class as it is not
changing the original string value if we try
to perform any of the operation to the string.
Example:
public class StringClassConcept {
String b = a + "software";
System.out.println(b);//abcsoftware
a.concat("software");
System.out.println("**************************
******************");
// string buffer
sb.append("software");
System.out.println(sb);// abcsoftware -
mutable
Output:
abcsoftware
abc
********************************************
Abcsoftware
Example:
public static void main(String[] args) {
// There are 2 ways to define the string
String s4 = "ghi";
String s5 = "abc";
String s6 = "ghi";
// equals method- It compares the content
of 2 string values
System.out.println(isequal);
System.out.println(isEqual2);
System.out.println(isequal4);
}
Output:
true
true
false
true
System.out.println(countOfCharacter);//4
String s2 = "Mumbai";
char ch = s2.charAt(3);
System.out.println(ch);//b
String s3 = "Mumbai";
String s4 = "mumbai";
System.out.println(isEqual);// false
Example:
String s3 = "Mumbai";
String s4 = "mumbai";
System.out.println(isEquals);
String s5 = "BangaloRe";
String s6 = s5.toLowerCase();
System.out.println(s6);//bangalore
// 6. toUpperCase()
String s7 = "Chennai";
String s8 = s7.toUpperCase();
System.out.println(s8);//CHENNAI
7. subsString(int begin): This method returns
a String which starts with the provided int
index position.
Example:
// 7. subsString(int begin)
String s9 = "Ahmednagar";
Example:
// subsString(int begin, int end)
System.out.println(s12);//Sri
System.out.println(s14);//VishAkhApAttnAm
10. replace(String old, String new): This
method returns a String which is the
replacement of first argument string with the
second argument string.
System.out.println(s16);//Hyderabad
11. trim(): This method removes the spaces
present in starting and trailing part of the
string but it CANNOT remove the intermediate
space from the string.
Example:
// 11. trim()
System.out.println(s18);//Hello World
System.out.println("**************************
***");
String reverse =
"";//o//ol//oll//olle//olleH
String s0 = "Hello";
for(int i=s0.length()-1; i>=0; i-- )
{
reverse = reverse + s0.charAt(i);
}
System.out.println("Reverse of String
"+s0+" is "+reverse);//olleH
char c = linebyline.charAt(i);
System.out.println(c);
}
System.out.println(index);//2
for(String ss:s21)
{
System.out.println(ss);
Output:
This
is
String
System.out.println(isStarts);//true
System.out.println(s24);//true
System.out.println(s25+2);//202
System.out.println(s27);//false
System.out.println(s29+2);//62
System.out.println(s31);//86.55
for(char cc:s33)
{
System.out.println(cc);
}
Output:
p
e
n
n
s
y
l
v
a
n
i
a
System.out.println(isnumber);//true
18. isAlphabetic(char c): This method returns
true if the provided character is alphabet.
Example:
//18. isAlphabetic(char c)
System.out.println(isalpha);//true
Example:
//19 replaceAll(String regex)
System.out.println(s36);//apring
System.out.println(s39);//Hqqqqqq
System.out.println(s41);//!pr!n!
System.out.println(s43);//tampa
System.out.println(s45);//A2c4centure
1. Implicit casting
2. Explicit casting
// Implicit casting
byte b = 20;
int ii = (int)b;
System.out.println(ii);//20
short s = 90;
long y = (long)s;
System.out.println(y);//90
Example:
// Explicit casting
int i = 10;
byte bb = (byte)i;
System.out.println(bb);//10
int k = 129;
byte cc = (byte)k;
System.out.println(cc);//-127
Output:
10
-127
}
public class Child extends Parent
{
//data_type variablename =(data_type_to
which we want to cast)variable_to_casted;
Example:
package casting;
cc.marry();
cc.home();
cc.car();
Output:
Exception in thread "main"
java.lang.ClassCastException: class
casting.Parent cannot be cast to class
casting.Child (casting.Parent and
casting.Child are in unnamed module of loader
'app')
at casting.Child.main(Child.java:33)
Example: