0% found this document useful (0 votes)
2 views

AutomationTesting_Java_14 (1)

The document outlines the installation procedures for Java and Eclipse IDE, including steps for checking Java version, downloading, and setting environment variables. It also covers Java programming concepts such as variables, data types, methods, control statements, and constructors, providing code examples for each topic. Additionally, it explains the differences between primitive and non-primitive data types, as well as the use of static and non-static methods.

Uploaded by

vikas bisen
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

AutomationTesting_Java_14 (1)

The document outlines the installation procedures for Java and Eclipse IDE, including steps for checking Java version, downloading, and setting environment variables. It also covers Java programming concepts such as variables, data types, methods, control statements, and constructors, providing code examples for each topic. Additionally, it explains the differences between primitive and non-primitive data types, as well as the use of static and non-static methods.

Uploaded by

vikas bisen
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 41

Java installation procedure:

Step1: Command to check java version: java -version

step2: check operating system:

Step2: how to download java


search download java version-->click on oracle.com-->Java SE Development Kit -->
click on 32/64 related file to download java

Step3: install downloaded java file

Step4: Copy java path


open C drive-->program files-->java-->jdk/jre(jdk prefered)-->bin folder-->copy bin
folder path

Step5: Set java path


right click on This Pc/my computer-->properties-->advanced system setting--
>envirnment variable-->user variable--> check for "path" variable

--Case1: already path variable exist -->click on path variable-->edit-->new-->ctrl+


V(Paste) --> ok--ok
--Case2: no path variable--> click on new --> enter variable name i.e. -"path"-->
variable value -ctrl+ V(Paste)--> ok-->ok

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

Diff versions eclipse IDE:


versions:
oxygen
neon
marsh
photon

eclipse installation procedure:

--search download eclipse oxygen-->click on https://www.eclipse.org/-->MORE


DOWNLOADS-->Eclipse version-->Eclipse IDE for Java and DSL Developers-->Windows
x86_64--> eclipse.zip file

--open downloads folder --> unzip eclipse file ---> open eclipse folder--> double
click on eclipse application file (blue colour icon)--> it should ask for workspace
path--> keep as it is(default path)-->select checkbox-->launch--> welcome page

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

package Sample1;

public class demo2


{
//class body

public static void main(String [] args)


{
//main method body

System.out.println("Good morning"); //printing statement


System.out.println("Hi.....");
}
}

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
1.Variables:
Variables are nothing but piece of memory use to store information.
one variable can store 1 information at a time.

Variables also used in information reusability.

To utilise variables in java programing language we need to follow below


steps:

1. Variable declaration (Allocating/Reserving memory)


2. Variable Initialization(Assigning or Inserting value)
3. Usage

Note:- According to all programming language dealing with information directly is


not a good practice
to overcome this variables are introduced.

-------------------------------------------------------------------------
-
package Variables; -
-
public class sample2 -
{ -
public static void main(String[] args) {

//1. Variable declaration


String sname; //dataType variableName
int rollnum;
char grade;
float per;

//2. Variable Initialization(Assigning or Inserting value)


sname = "Kanchan"; //variableName = "variable value"
rollnum = 100; //variableName = variable value
grade = 'A'; //variableName = 'variable value'
per = 65.5f; //variableName = variablevalue f

//3. Usage
System.out.println(sname);
System.out.println(rollnum);
System.out.println(grade);
System.out.println(per);
-
} -
} -
-
-------------------------------------------------------------------------

package Variables;
public class sample2
{
public static void main(String[] args) {

//1. Variable declaration


String sname; //dataType variableName
int rollnum;
char grade;
float per;

//2. Variable Initialization(Assigning or Inserting value)


sname = "Kanchan"; //variableName = "variable value"
rollnum = 100; //variableName = variable value
grade = 'A'; //variableName = 'variable value'
per = 65.5f; //variableName = variablevalue f

//3. Usage
System.out.println("Student name: "+sname);
System.out.println("student rollNum: "+rollnum);
System.out.println("student grade: "+grade);
System.out.println("student percentage: "+ per +" %");

}
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-

Data Types:
Data type are used to represent type of data or information which we are
going to use in our java program.

In java programming it is mandatory to declare datatype before declaration


of variable.

In java datatypes are classified into two types :


1. Primitive datatype.
2. Non-primitive datatype.

1.Primitive datatype:
There are 8 type of primitive datatypes.
all the primitive datatypes are keywords.
* Memory size of primitive datatype are fix.
The types of primitive datatype are:

Note:- keyword starts with lower case

syntax: datatype variablename;

1.(Numeric + Non-decimal):-
Ex: 80,85,10,..etc

Data Type Size


1. byte 1 byte
2. short 2 bytes
3. int 4 bytes
4. long 8 bytes

2.(Numeric + decimal):-
Ex: 22.5,22.8,6.4....

5. float 4 byte
6. double 8 byte

3.Character :-
Ex: A,B,X,Z.
7. char 2 byte

---------------------------------------
4.Conditional:-
Ex: true,false.

8. boolean 1 bit
---------------------------------------------------------
2. Non-primitive datatype:
There are 2 types of non primitive datatypes .
all the Non primitive datatypes are identifiers.
* Memory size of non primitive datatype is not defined or not fix.

Note: Identifier starts with capital letter.

e.g. String, className


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

Methods:
A method is a block of code which only runs when it is called.
You can pass data, known as parameters, into a method.
Methods are used to perform certain actions, and they are also known as
functions.

Why use methods? To reuse code: define the code once, and use it many
times.

1. main method
In any Java program, the main() method is the starting point from where
compiler starts program execution.
So, the compiler needs to call the main() method.

without main method we can't run any java program.

2. Regular method

1. static regular method


1. static method call from same class --> methodname();
2. static method call from diffrent/another class --
>className.methodname();
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-

package Methods;

public class demo1 {

//1. static regular method call from same class --> methodname();

public static void main(String[] args) //main method-->manager


{
System.out.println("main method started");

m1(); // methodname(); //static regular method call from same


class
m2();

System.out.println("main method ended");


}

// static ->regular method


public static void m1() //regular method-->emp
{
System.out.println("running static regular method from same class:
m1");
}

public static void m2() {


System.out.println("running static regular method from same class:
m2");
}

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

package Methods;

public class demo2 {

//2. static method call from diffrent/another class --


>className.methodname()
public static void main(String[] args) {

System.out.println("main method started");

demo3.m3(); // className.methodname(); //static method call from


diff class

demo3.m4();

System.out.println("main method ended");


}
}
-----------------------------------------------------------------------------------
-----------

package Methods;

public class demo3 {

// static ->regular method


public static void m3()
{
System.out.println("running static regular method from diff class:
m3");
}

// static ->regular method


public static void m4()
{
System.out.println("running static regular method from diff class:
m4");
}

}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--

2. non- static regular method


3. non-static method call from same class --> create object of same class
4. non-static method call from diffrent/another class --> create object of
diff class

Note: At the time of program execution main method is going to get executed
automatically,
where as regular methods are not going to get executed automatically.

At the time of program execution priority is sceduled for main method only.

To call a regular method we need to make call method call from main method,
until unless if the method call is not made regular method will not get
executed.

regular methods can be called multiple times.


-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
package Methods;

public class demo4 {


//3. non-static method call from same class --> create object of same
class

public static void main(String[] args)


{
System.out.println("main method started");

//classname objectName = new classname();


demo4 d = new demo4(); //object creation
d.m5(); //objectName.methodname();
d.m6();

System.out.println("main method ended");


}

//non-static regular method


public void m5()
{
System.out.println("running non-static regular method from same class:
m5");
}

//non-static regular method


public void m6()
{
System.out.println("running non-static regular method from same class:
m6");
}
}

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

package Methods;

public class demo5


{
//4. non-static method call from diffrent/another class --> create object of
diff class

public static void main(String[] args)


{
System.out.println("main method started");

//classname objectName = new classname();


demo6 d6=new demo6(); //object creation
d6.m7();
d6.m8();

System.out.println("main method ended");


}

}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-

package Methods;

public class demo6 {

//non-static regular method


public void m7()
{
System.out.println("running non-static regular method from diff class:
m7");
}

//non-static regular method


public void m8()
{
System.out.println("running non-static regular method from diff class:
m8");
}

}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
package Methods;

public class demo7


{

//5. method without/zero parameter

public static void main(String[] args)


{
System.out.println("main method started");

addition();

System.out.println("main method ended");


}

//static regular method


//method without/zero parameter
public static void addition()
{
int a=10;
int b=20;
int c=a+b;
System.out.println(c);
}
}
-----------------------------------------------------------------------------------
-------------------------------------------------------

package Methods;

public class demo9 {

// 6. method with parameter

public static void main(String[] args) {


System.out.println("main method started");

studentName();
studentName("Anil");
studentName("Sunil");
System.out.println("main method ended");
}

//static regular method


//method without parameter --
public static void studentName()
{
String sname="Ajay";
System.out.println(sname);
}

//static regular method


//method with parameter --String parameter
public static void studentName(String sname)
{
System.out.println(sname);
}
}
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------------

package Methods;

public class demo10


{
public static void main(String[] args) {

System.out.println("main method started");


//studentInfo();
studentInfo("abc",200, 'C', 85.2f);
System.out.println("------------------");
studentInfo("xyz",300, 'A', 60.2f);

System.out.println("main method ended");


}

public static void studentInfo(String sname, int sRollNum, char sGrade, float sper)
{
System.out.println(sname);
System.out.println(sRollNum);
System.out.println(sGrade);
System.out.println(sper);
}

//public static void studentInfo()


//{
// String StudentName;
// int StudentRollNum;
// char StudentGrade;
// float StudentPer;
//
// StudentName ="abc";
// StudentRollNum = 100;
// StudentGrade ='B';
// StudentPer = 76.6f;
//
// System.out.println(StudentName);
// System.out.println(StudentRollNum);
// System.out.println(StudentGrade);
// System.out.println(StudentPer);
//}

}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
package Control_Statements;

public class example1_IF_Statement {


public static void main(String[] args) {

int marks= 20;

//20>=35
if(marks>=35)
{
System.out.println("Pass");
}

// if(condition)
// {
//
// }

}
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-

package Control_Statements;

public class example2_IF_Else_Statement {


public static void main(String[] args) {

int marks = 35;

// 35>=35
if(marks>=35)
{
System.out.println("Pass");
}
else
{
System.out.println("Fail");
}

}
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
package Control_Statements;

public class example3_else_If_Stement {


public static void main(String[] args) {
int marks = 40;

if(marks>=65)
{
System.out.println("distinction");
}
else if (marks>=60)
{
System.out.println("1st class");
}
else if (marks>=55)
{
System.out.println("higher 2nd class");
}
else if (marks>=50)
{
System.out.println("2nd class");
}
else if (marks>=35)
{
System.out.println("Pass");
}
else
{
System.out.println("Fail");
}

}
}
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------
package Control_Statements;

public class example4_nested_IF


{
public static void main(String[] args)
{
String UN="abc";
String PWD="xyz";

if("ab"==UN)
{
System.out.println("Correct UN");
System.out.println("Enter your password");

if("xyz"==PWD)
{
System.out.println("Correct PWD--> Login Scuccessful");
}
else
{
System.out.println("Wrong PWD--> Login Failed");
}
}
else
{
System.out.println("Wrong UN --> Login Failed");
}

}
}
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------

package Control_Statements;

public class example5_Switch {


public static void main(String[] args) {
int day=15;

switch (day)
{
case 1: System.out.println("Today is mon");
break;

case 2: System.out.println("Today is Tue");


break;

case 3: System.out.println("Today is Wed");


break;

case 4: System.out.println("Today is Thr");


break;

case 5: System.out.println("Today is Fri");


break;

case 6: System.out.println("Today is Sat");


break;

case 7: System.out.println("Today is Sun");


break;

default: System.out.println("invalid input");


break;

}
}
}

-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
Constructor:
A constructor in Java is a special method that is used to initialize
objects/variables.
The constructor is called when an object of a class is created.

At the time of constructor declaration below points need to folow:


1. Constructor name should be same as class name
2. you should not declare any return type for the constrictor(like
void).
3. Any no of constructor can be declared in a java class but
constructor name should be same as class name,
but arguments/parameter should be diffrent.

Use of Constructor
1. To copy/load all members of class into object --> when we create
object of class
2. To initialize data member/variable

Types of Constructor
1. Default Constructor
2. User defined Constructor

1. Default Constructor
If Constructor is not declared in java class then at the time of
compilation compiler will provide Constructor for the class
If programer has declared the constructor in the class then compiler
will not provided default Constructor.
The Constructor provided by compiler at the time of compilation is
known as Default Constructor

2. User defined Constructor


If programer is declaring constructor in java class then it is
considered to be as User defined constructor.
User defined Constructor are classified into 2 types
1. Without/zero parameter constructor
// example-without parameter constructor --eg. addition
2. With parameter constructor
// example-with parameter constructor- only 1 constructor
-- eg. addidtion
// example-with parameter constructor- multiple constructor
-- eg. addition
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
package Contructor;

public class sample1


{

//example of default constructor

//constructor --> default constructor privided by compiler


// use1: use to copy all the members of class into object
// sample1()
// {
//
// }

public static void main(String[] args)


{
sample1 s1=new sample1();
s1.addition();

//sample1 --> classname-->dataType


//s1 --> objectName/reference variable
// new --> keyword --> blank object
//sample1() --> classname(); --> constructor

sample2 s2=new sample2();


s2.mul();

//non-static regular from same class


public void addition()
{
int a=10;
int b=20;
int sum=a+b;
System.out.println(sum);
}

}
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------------
package Contructor;

public class sample2


{

//example of default constructor

//default condtictor--> provided by compiler


// use1: use to copy all the members of class into object
// sample2()
// {
//
// }

//non-static regular from diff class


public void mul()
{
int a=10;
int b=20;
int mul=a*b;
System.out.println(mul);
}

}
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------------

package Contructor;

public class sample3


{
//example of user defined contructor
//Step1: declaration
int a; //50
int b; //60

//contructor--> user defined


// use1: use to copy all the members of class into object
// use2. To initialize data member/variable

//step2: initialization
sample3()
{
a=50;
b=60;
}

public static void main(String[] args)


{

sample3 s3=new sample3();


s3.addition();
s3.mul();

System.out.println("------------------");

sample4 s4 =new sample4();


s4.div();
}

//step3: usage
public void addition()
{
int sum=a+b;
System.out.println(sum);
}

//step3: usage
public void mul()
{
int mul=a*b;
System.out.println(mul);
}

}
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------------

package Contructor;

public class sample4


{

//example of user defined contructor

//step1: declaration
int a;
int b;
//constructor--> user defined
// use1: use to copy all the members of class into object
// use2. To initialize data member/variable/objects

//step2: initialization
sample4()
{
a=10;
b=5;
}

//step3: usage
public void div()
{
int divValue= a/b;
System.out.println(divValue);
}

}
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
package Contructor;

public class sample5


{

//example with parameter -->single constructor

//step1: declaration
int num1; //10, 15
int num2;

//step2: initialization

//constructor --> user defined --> only 1 constructor

//use1: copy all members of class into object


//use2: to perform initialization

sample5(int a, int b) // int , int parameter constructor a=10, b=15


{
num1 = a;
num2 = b;
}

public void addition()


{
int c= num1+num2;
System.out.println(c);
}

public static void main(String[] args)


{

sample5 s5=new sample5(30,40);


s5.addition();

sample5 s55=new sample5(5,6);


s55.addition();

}
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------

package Contructor;

public class sample8


{

//example with parametr -->multiple constructor

int num1;
int num2;
String sname;

//user defined -->without parameter


sample8()
{
num1=20;
num2=30;
}

//user defined -->with parameter(2 int parameter)


sample8(int a,int b)
{
num1=a; //10
num2=b; //15
}

//user defined -->with parameter(String parameter)


sample8(String str)
{
sname=str;
}

public void addition()


{
int sum = num1 + num2;
System.out.println(sum);
}

public void studentName()


{
System.out.println(sname);
}

}
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------

package Contructor;

public class sample7 {


public static void main(String[] args) {

//example with parameter -->multiple constructor

sample8 s8 =new sample8(); //create object of without parameter constructor


s8.addition(); //50

System.out.println("----------------");

sample8 s9=new sample8(10, 15);


s9.addition();

System.out.println("----------------");
sample8 s10=new sample8("abc");
s10.studentName();

}
}
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
------------------------------------------------------
Object-Oriented Programming System(OOPS)

Java programming language is very popular in software industry because of OOPS


concept.

OOps concept provides 5 important pillers/principles for the language they are
1. Inheritance
2. Polymorphism
3. Encapsulation
4. Abstraction
-----------------------------------------------------------------------------------
---------------------

Inheritance:
It is one of the Oops principle where one class acquires properties of
another class with the help of 'extends' keywords is called Inheritance.

The class from where properties are acquiring/inheriting is called


super/base/parent class.

The class too where properties are inherited/delivered is called sub/child


class.

Inheritance takes place between 2 or more than 2 classes.

Inheritance is classified into 4 types:


1. Singlelevel Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. hirarchicle
1. Singlelevel Inheritance:
It is a operation where inheritance takes place between 2 classes.
To perform singlelevel inheritance only 2 classes are mandatory.

2. Multilevel Inheritance:
Multilevel Inheritance takes place between 3 or more than 3 classes.

In Multilevel Inheritance 1 sub class acquires properties of another super


class &
that class acquires properties of its another super class & phenomenon
continuous.

3. Multiple Inheritance:
1 subclass acquiring properties of 2 super classes at the same time is known
as Multiple Inheritance.
Java doesn't support Multiple Inheritance using class because of "dimond
ambiguity" problem.

By using interface we can achieve Multiple Inheritance.

4. Hirarchicle Inheritance:
multiple sub classes can acquire properties of 1 super class is known as
hirarchicle Inheritance.
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------------
package Inheritance;

// super/base/parent class

public class father


{
public void car()
{
System.out.println("Car: kia seltos");
}

public void money()


{
System.out.println("money: 2L");
}

public void home()


{
System.out.println("home: 2BHK");
}
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------

package Inheritance;

//sub / child class


public class son extends father
{
public void mobile()
{
System.out.println("mobile: samsung A50");
}

// public void car()


// {
// System.out.println("Car: kia seltos");
// }
//
// public void money()
// {
// System.out.println("money: 2L");
// }
//
// public void home()
// {
// System.out.println("home: 2BHK");
// }

}
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------

package Inheritance;

public class SingleLevel_Inheritance {


public static void main(String[] args) {

son s=new son();


s.mobile();
s.car();
s.money();
s.home();
}
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
package Inheritance;

//Multilevel Example

public class whatsAppV1


{
public void textMsg()
{
System.out.println("text Msg");
}
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------

package Inheritance;

public class whatsAppV2 extends whatsAppV1


{

public void audioCalling()


{
System.out.println("audio Calling");
}

// public void textMsg()


// {
// System.out.println("text Msg");
// }
}

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

package Inheritance;

public class whatsAppV3 extends whatsAppV2


{
public void videoCalling()
{
System.out.println("video Calling");
}

// public void audioCalling()


// {
// System.out.println("audio Calling");
// }

// public void textMsg()


// {
// System.out.println("text Msg");
// }

}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-

package Inheritance;

public class MultiLevel_Inheritance


{
public static void main(String[] args) {

whatsAppV3 v3=new whatsAppV3();


v3.textMsg();
v3.audioCalling();
v3.videoCalling();

}
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
package Inheritance;

// super/base/parent class
public class father
{
public void car()
{
System.out.println("Car: kia seltos");
}

public void money()


{
System.out.println("money: 2L");
}

public void home()


{
System.out.println("home: 2BHK");
}
}
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------

package Inheritance;

public class son1 extends father


{

public void mobile()


{
System.out.println("mobile");
}

}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------

package Inheritance;

public class son2 extends father


{

public void laptop()


{
System.out.println("laptop");
}

}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------

package Inheritance;

public class son3 extends father


{

public void PC()


{
System.out.println("PC");
}
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------

package Inheritance;

public class Hirarchicle_Inheritance {


public static void main(String[] args) {

System.out.println("---properties of son1---");
son1 s1 =new son1();
s1.mobile();
s1.car();
s1.home();
s1.money();

System.out.println("---properties of son2---");
son2 s2=new son2();
s2.laptop();
s2.car();
s2.home();
s2.money();

System.out.println("---properties of son3---");
son3 s3=new son3();
s3.PC();
s3.car();
s3.home();
s3.money();
}
}
-----------------------------------------------------------------------------------
-----------------------------
Abstract Class:
A class declared with "abstract" keyword is called abstract class.

an Abstract class is nothing but an incomplete class where programer


can declare complete as well as incomplete methods in it.

programer can declare incomplete methods as abstract method, by declaring


keyword
called "abstract" infront of method.

we can't create object of abstract class, to create object of abstract class


we need
to make use of concrete class.

Concrete class:
A class which provides definations for all the incomplete methods which are
present in abstarct class with the help of extends keywords is called
concrete class.
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------------

package Abstarct_Concrete_Class;

//incomplete class---> abstarct class

abstract public class sample1


{
//complete method
public void m1() //method declaration
{
System.out.println("method m1: completed in abstarct class"); //method
defination
}

//incomplete method
abstract public void m2(); //method declaration

//incomplete method
abstract public void m3(); //method declaration

}
-----------------------------------------------------------------------------------
------------------------------

package Abstarct_Concrete_Class;

//concrete class
public class sample2 extends sample1
{

// public void m1()


// {
// System.out.println("running m1 method"); //method defination
// }

public void m2()


{
System.out.println("method m2: completed in concrete class");
}

public void m3()


{
System.out.println("method m3: completed in concrete class");
}
}
-----------------------------------------------------------------------------------
-------------------------------

package Abstarct_Concrete_Class;

public class TestSample {


public static void main(String[] args) {

sample2 s2=new sample2();


s2.m1();
s2.m2();
s2.m3();
}
}
-----------------------------------------------------------------------------------
-------------------------------
Interface:
It is one of the oops principle.
It is pure 100% abstract in nature.
Interface is use to declare only incomplete methods in it.

Features of Interface:
1. Data Members/variable declared inside Interface are by default static and
final.
2. methods declared inside Interface are by default public & abstract.
3. constructor concept in not present inside Interface.
4. object of Interface can't be created.
5. Interface support multiple inheritance.
6. To create object of Interface programmer need to make use of
Implementation class using implements keyword.

Implementation class:
A class which provides definations for all the incomplete methods which are
present in interface with the help of "implements" keyword is called
Implementation class.
-----------------------------------------------------------------------------------
--------------------------------------------
package Interface_ImplementationClass;

//demo1 -->interface name


public interface demo1
{

int num=10; //variables are by default static & final

void m1(); //methods are by default public & abstract

void m2();

}
-----------------------------------------------------------------------------------
-------------

package Interface_ImplementationClass;

//implementation class
public class demo2 implements demo1
{
public void m1()
{
System.out.println("method m1 completed in implementation class");
}

public void m2()


{
System.out.println("method m2 completed in implementation class");
}

}
-----------------------------------------------------------------------------------
---------------------------
package Interface_ImplementationClass;

public class TestDemo2 {


public static void main(String[] args) {
//example of interface & implementation class
demo2 d2 =new demo2();
d2.m1();
d2.m2();
}
}
-----------------------------------------------------------------------------------
-----------------------------

package Interface_ImplementationClass;

//super interface 1
public interface I1
{

void m3();

void m4();

}
-----------------------------------------------------------------------------------
-----------------------------

package Interface_ImplementationClass;

//super interface 2
public interface I2
{

void m5();

}
-----------------------------------------------------------------------------------
------------------------------------
package Interface_ImplementationClass;

//sub class or implementation class


public class Sample2 implements I1, I2
{
//example of multiple inheritance using interface

public void m3() {


System.out.println("running method m3 from Interface I1");
}

public void m4() {


System.out.println("running method m4 from Interface I1");
}

public void m5() {


System.out.println("running method m5 from Interface I2");
}

}
-----------------------------------------------------------------------------------
----------------------------------
package Interface_ImplementationClass;
public class MultipleInheritance
{
//example of multiple inheritance using interface

public static void main(String[] args) {

Sample2 s2=new Sample2();


s2.m3();
s2.m4();
s2.m5();
}

}
-----------------------------------------------------------------------------------
------------------------------------------

Types of variables:

1. global variable:
the variable which is declared outside the method/block/constructor is called
global variable.
scope of global variable remains throught the class.
global variable is also called permanant variable.

2. local variable:
The variable which is declared inside the method/block/constructor is called
local variable.
scope of local variable remains only within the method/block/constructor.
local variable is also called temporary variable.
-----------------------------------------------------------------------------------
---------------------------------

This & super Keyword

1.this keyword
this keyword is use to access global variable from same class.

2. super keyword
super keyword is use to access global variable from super/diffrent class.
-----------------------------------------------------------------------------------
---------------------------------
package This_and_Super_Keywords;

public class sample1


{
//example of this keyword

int a=10; //global variable

public void m1()


{
int a=20; //local variable
System.out.println(a); //20
System.out.println(this.a); //10

}
}
-----------------------------------------------------------------------------------
-------------------------------
package This_and_Super_Keywords;

public class TestSample1


{

//example of this keyword


public static void main(String[] args) {

sample1 s1=new sample1();


s1.m1();

}
}
-----------------------------------------------------------------------------------
--------------------------------
package This_and_Super_Keywords;

//demo1 --> super class


public class demo1
{

//example of super keyword


int b =50; //global variable from super class

}
-----------------------------------------------------------------------------------
-------------------------------
package This_and_Super_Keywords;

//demo2--> sub class


public class demo2 extends demo1
{
//example of super keyword

//int b =50; //global variable from super class

int b=60; // global variable from sub class

public void m2()


{
int b = 70; //local variable
System.out.println(b); // 70 --> local variable call
System.out.println(this.b); // 60 --> global variable call from
same/sub class
System.out.println(super.b); // 50 --> global variable call from
super/diff class

}
}
-----------------------------------------------------------------------------------
--------------------------------
package This_and_Super_Keywords;
public class TestDemo {

//example of super keyword


public static void main(String[] args) {

demo2 d2=new demo2();


d2.m2();

}
}
-----------------------------------------------------------------------------------
--------------------------------
In Java, access specifiers (also known as access modifiers) are used to define the
visibility
and accessibility of classes, methods, and variables. There are four types of
access specifiers:

1. private
The private access specifier restricts access to the members (variables, methods)
of the class
only within the same class. No other class can access these members, even
subclasses or classes
in the same package.

Example:

//Private modifier

class MyClass {
private int age;

private void display() {


System.out.println("Private method called!");
}

public void setAge(int age) {


this.age = age;
}

public int getAge() {


return age;
}
}

public class PrivateTest {


public static void main(String[] args) {
MyClass obj = new MyClass();
obj.setAge(25);
System.out.println("Age: " + obj.getAge());

// The following line will cause a compilation error:


// obj.display(); // display() is private and cannot be accessed here
}
}

-----------------------------------------------------------------------------------
--------------------------------
2. default (Package-Private)
When no access specifier is explicitly mentioned, it is known as default access.
The members are accessible only within the same package. They are not accessible
outside
the package, even if it's a subclass.

Example:

//Default Example

class MyClass {
int number; // default access

void show() {
System.out.println("Default method");
}
}

public class Test {


public static void main(String[] args) {
MyClass obj = new MyClass();
obj.number = 10; // Accessible because Test is in the same package
obj.show(); // Accessible because Test is in the same package
}
}

-----------------------------------------------------------------------------------
--------------------------------
3. protected
The protected access specifier allows members to be accessed within the same
package and by subclasses
(even if they are in different packages).

Example:
//protected Example
class MyClass {
protected int num;

protected void show() {


System.out.println("Protected method");
}
}

class SubClass extends MyClass {


public void display() {
num = 20; // Accessing protected variable from superclass
show(); // Accessing protected method from superclass
}
}

public class Test {


public static void main(String[] args) {
SubClass obj = new SubClass();
obj.display(); // Calling display method of SubClass
}
}

-----------------------------------------------------------------------------------
--------------------------------
4. public
The public access specifier makes members accessible from any other class,
regardless of package.

Example:

java
Copy
public class MyClass {
public int number;

public void display() {


System.out.println("Public method called!");
}
}

public class Test {


public static void main(String[] args) {
MyClass obj = new MyClass();
obj.number = 10; // Accessible from any class
obj.display(); // Accessible from any class
}
}

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

Access Specifier Same Class Same Package Subclass (Different Package) World
(Anywhere)
private Yes No No No
default Yes Yes No No
protected Yes Yes Yes No
public Yes Yes Yes Yes

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

Abstraction in Java is one of the fundamental Object-Oriented Programming (OOP)


concepts that hides
the implementation details and only shows the functionality to the user.
The main goal of abstraction is to reduce complexity and allow the programmer to
focus on
what an object does, instead of how it achieves it.

In Java, abstraction is achieved using:

Abstract classes
Interfaces
1. Abstract Class
An abstract class is a class that cannot be instantiated directly. It can have
abstract methods (methods without a body) as well as concrete methods (methods with
a body). An abstract class is meant to be extended by other classes to provide
implementations for the abstract methods.

Key Points about Abstract Classes:


An abstract class can have both abstract (unimplemented) and concrete (implemented)
methods.
It can have member variables and constructors.
A class that contains at least one abstract method must be declared as abstract.
Example:

abstract class Animal {


public abstract void sound();

public void sleep() {


System.out.println("This animal is sleeping");
}
}

class Dog extends Animal {

public void sound() {


System.out.println("Barking!");
}
}

public class Test {


public static void main(String[] args) {
// Cannot instantiate an abstract class directly
// Animal animal = new Animal(); // Error

// Can create an instance of a subclass


Dog dog = new Dog();
dog.sound(); // Output: Barking!
dog.sleep(); // Output: This animal is sleeping
}
}
2. Interface
An interface is a reference type, similar to a class, that can contain only
constants, method signatures,
default methods, static methods, and nested types. Interfaces cannot contain
instance fields
or constructors. A class implements an interface, thereby inheriting the abstract
methods of
the interface.

Key Points about Interfaces:


All methods in an interface are implicitly abstract (unless they are default or
static).
A class implements an interface and provides the implementation of the abstract
methods.
A class can implement multiple interfaces (Java supports multiple inheritance
through interfaces).
A class must implement all methods of the interface (unless the class is abstract).
Example:

java
Copy
interface Animal {

void sound();

default void sleep() {


System.out.println("This animal is sleeping");
}
}
class Dog implements Animal {

public void sound() {


System.out.println("Barking!");
}
}

public class Test {


public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // Output: Barking!
dog.sleep(); // Output: This animal is sleeping
}
}

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

Encapsulation is one of the key concepts in Object-Oriented Programming (OOP), and


it refers to the
bundling of data (variables) and the methods (functions) that operate on that data
into a single unit
known as a class. Encapsulation also helps in controlling the access to the data,
making sure that the data
is protected from unauthorized access and modification.

In simpler terms, encapsulation is about restricting access to some of an object's


components and ensuring that only necessary and controlled access is provided.

Key Concepts of Encapsulation:


Private Data Members: Data members (variables) of a class are marked as private so
that they are hidden from other classes.
Public Getter and Setter Methods: These methods provide controlled access to the
private data members.
Getter methods retrieve the value of a private variable.
Setter methods set the value of a private variable.
Access Control: You can control how a class's variables are accessed, modified, or
updated using getters and setters.
Why Encapsulation?
Data Hiding: The internal details of the implementation are hidden from outside
interference and misuse.
Improved Security: By controlling access to the data, you can ensure that the
object’s state remains valid and that only legitimate changes are made.
Flexibility: You can change the internal implementation without affecting other
parts of the program, as long as the getter and setter methods remain consistent.
Code Maintenance: Encapsulation promotes code reusability and easier maintenance.
Example of Encapsulation in Java:

class Employee {
// Private data members
private String name;
private int age;
private double salary;
// Getter for name
public String getName() {
return name;
}

// Setter for name


public void setName(String name) {
this.name = name;
}

// Getter for age


public int getAge() {
return age;
}

// Setter for age


public void setAge(int age) {
if (age > 0) { // Control the age to ensure it's a positive value
this.age = age;
} else {
System.out.println("Age must be positive.");
}
}

// Getter for salary


public double getSalary() {
return salary;
}

// Setter for salary


public void setSalary(double salary) {
if (salary > 0) { // Ensure the salary is positive
this.salary = salary;
} else {
System.out.println("Salary must be positive.");
}
}

// Method to display employee details


public void display() {
System.out.println("Employee Name: " + getName());
System.out.println("Employee Age: " + getAge());
System.out.println("Employee Salary: $" + getSalary());
}
}

public class Test {


public static void main(String[] args) {
// Create an object of the Employee class
Employee emp = new Employee();

// Set values using setter methods


emp.setName("John Doe");
emp.setAge(30);
emp.setSalary(50000);

// Display the employee details using the display method


emp.display();
// Try setting invalid values
emp.setAge(-5); // Will show an error message: Age must be positive.
emp.setSalary(-1000); // Will show an error message: Salary must be
positive.
}
}

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

Arrays are used to store multiple values in a single variable,


instead of declaring separate variables for each value.
package Array;

public class example1_StringArray


{
public static void main(String[] args) {
//step1: Array declaration
String [] ar =new String[5];

//step2: Array initialization


ar[0] = "ganesh";
ar[1] = "mahesh";
ar[2] = "suresh";
ar[3] = "ramesh";
ar[4] = "rahul";
//ar[5] = "rohit";

System.out.println(ar.length); //5

//step3: usage
System.out.println(ar[2]);

System.out.println("-------Print all info in String array---------");

//5<=4 5
// for(int i=0; i<=4; i++)
// { //ar[4]
// System.out.println(ar[i]);
// }

for(int i=0; i<=ar.length-1; i++)


{ //ar[4]
System.out.println(ar[i]);
}

}
}
-----------------------------------------------------------------------------------
--------------------------------------------------------------------

package Array;

public class example2_intArray {


public static void main(String[] args) {

int [] ar1=new int[5];


ar1[0]=200;
ar1[1]=300;
ar1[2]=400;
ar1[3]=100;
ar1[4]=500;
//ar1[5]=600; exception

//array declaration & initialization in single step


int [] ar1= {200,300,400,100,500,600};

System.out.println(ar1.length);
System.out.println(ar1[3]);

System.out.println("---print all info in int array----");

// for(int i=0; i<=4; i++)


// {
// System.out.println(ar1[i]);
// }

for(int i=0; i<=ar1.length-1; i++)


{
System.out.println(ar1[i]);
}

}
}
-----------------------------------------------------------------------------------
--------------------------------------------------------------
import java.util.Arrays;

public class example4_Sort_intArray


{
public static void main(String[] args) {

int [] ar1=new int[5];

ar1[0]=200; //100
ar1[1]=300; //200
ar1[2]=400; //300
ar1[3]=100; //400
ar1[4]=500; //500

System.out.println("----print original info-----");

for(int i=0; i<=ar1.length-1; i++)


{
System.out.println(ar1[i]);
}

System.out.println("----print info in asscending order-----");


Arrays.sort(ar1);

for(int i=0; i<=ar1.length-1; i++) {


System.out.println(ar1[i]);
}

System.out.println("----print info in desscending order-----");

for(int i=ar1.length-1; i>=0; i--) {


System.out.println(ar1[i]);
}

}
}
-----------------------------------------------------------------------------------
---------------------------------------------------------
import java.util.Arrays;

public class example5_Sort_StringArray


{
public static void main(String[] args) {

String [] ar =new String[5];

ar[0] = "ganesh";
ar[1] = "mahesh";
ar[2] = "suresh";
ar[3] = "ramesh";
ar[4] = "rahul";

//array declaration & initialization in single step


String [] ar= {"rahul","mahesh", "ganesh","ramesh"};

Arrays.sort(ar);

for(int i=0; i<=ar.length-1; i++)


{
System.out.println(ar[i]);
}

}
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------
public class example8
{// 0 1 2
//0 10 20 30
//1 40 50 60

public static void main(String[] args) {


//step1: array declaration
int [][] ar=new int[2][3];

//step2: array initialization


ar[0][0]=10;
ar[0][1]=20;
ar[0][2]=30;
ar[1][0]=40;
ar[1][1]=50;
//ar[1][2]=60;
//step3: usage
System.out.println(ar.length); //print only row size
System.out.println(ar[1][2]);
System.out.println("---print all info from array---");

//outer for loop for rows

//2<=1 2
for(int i=0; i<=1; i++)
{
//inner for loop for col
//3<=2 3
for(int j=0; j<=2; j++)
{ // 1 2
System.out.print(ar[i][j]+" "); //10 20 30
} // 40 50 60
System.out.println(); //
}

}
-----------------------------------------------------------------------------------
------------------------------------------------------
package Array;

public class example9 {


public static void main(String[] args) {
//0 1
int [][] ar= {{10,20,30},{40,50,60}};

//outer for loop for rows


for(int i=0; i<=1; i++)
{
//inner for loop for col
for (int j = 0; j <=2; j++)
{
System.out.print(ar[i][j]+ " ");
}
System.out.println();
}

}
}
-----------------------------------------------------------------------------------
------------------------------------------------------
String class:
1. String is non-primitive data type, memory size is not fixed.
2. String is use to store collection of characters.
3. String is a inbuilt class present inside "java.lang" package.
4. String class is final class can't be inherited to other classes.
5. At the time of String declaration, initialization, object creation takes
place.
6. String objects are immutable in nature/cant be change.
7. object creation of String can be done in 2 ways:
1. without using new keyword
2. using new keyword
8. String objects are going to get stored inside String pool area which is
present inside heap area.
String pool area:
It is use to store String objects.
It is classified into 2 areas:
1. constant pool area
2. non-constant pool area.

1. constant pool area:


1. During object creation time if you don't make use of new keyword then
object creation takes place inside constant pool area.

Duplicate objects are not allowded inside constant pool area.

2. non-constant pool area:

2. During object creation time if you make use of new keyword then
object creation takes place inside non-constant pool area.

Duplicate objects are allowded inside non-constant pool area.


-----------------------------------------------------------------------------------
------------------------------------------------------------------------------
package StringClass_Methods;

public class sample1 {


public static void main(String[] args) {

String str="abc";

str=str+"d"; //abcd
System.out.println(str);

//1. create object of string without using new keyword


String s1="xyz";

//2. create object of string using new keyword


String s2=new String("xyz1");

}
}
-----------------------------------------------------------------------------------
---------------------------------

package StringClass_Methods;

public class sample2 {


public static void main(String[] args) {

//1. create object of string without using new keyword


String s1="xyz";
String s2="xyz";
String s3="xyz1";

//2. create object of string using new keyword


String s4=new String("xyz");
String s5=new String("xyz");

System.out.println(s1==s2); //true

System.out.println(s1==s3); //false

System.out.println(s1==s4); //false

System.out.println(s4==s5); //false

}
}
-----------------------------------------------------------------------------------
----------------------------------------

public class StringClass_Methods


{
public static void main(String[] args) {

String s1="abhishek";
String s2="ABHISHEK";
String s3="";

System.out.println(s1.length()); //8

System.out.println(s1.toUpperCase());
//s1=s1.toUpperCase(); //re-initialization

System.out.println(s2.toLowerCase());
//s2=s2.toLowerCase();

System.out.println(s1.isEmpty()); //false
System.out.println(s3.isEmpty()); //true

}
}
-----------------------------------------------------------------------------------
------------------------------------
public class StringClass_Methods
{
public static void main(String[] args) {
String s1="abhishek";
String s2="ABCD";
String s3="";
String s4="ABHISHEK";
String s5 ="city";
String s6 ="abcabcab";
String s7 = "java classes";

System.out.println(s1.substring(4,6)); //ci
System.out.println(s1.substring(6)); //city

System.out.println(s7.replace("java", "selenium"));

System.out.println(s2.concat(s5)); //ABCDcity
System.out.println(s2+s5); //ABCDcity
System.out.println(s1.endsWith("city")); //true
System.out.println(s1.startsWith("ve")); //true

System.out.println(s6.lastIndexOf('a')); //6
System.out.println(s6.indexOf('b')); //1

System.out.println(s1.charAt(5)); //i

System.out.println(s1.contains(s5)); //true
System.out.println(s1.equalsIgnoreCase(s4)); //true
System.out.println(s1.equals(s4)); //false

System.out.println(s1.length()); //8

System.out.println(s1.toUpperCase());

// s1=s1.toUpperCase(); //re-initialization
// System.out.println(s1);

System.out.println(s2.toLowerCase()); //abcd

// s2=s2.toLowerCase(); //re-initialization
// System.out.println(s2);

System.out.println(s1.isEmpty()); //false
System.out.println(s3.isEmpty()); //true

}
}
-----------------------------------------------------------------------------------
------------------------------------------

You might also like