0% found this document useful (0 votes)
2K views134 pages

Java Notes For Selenium

The document provides an introduction to the Java programming language. It covers topics such as what Java is, why it should be used, how to download and install Java and the Eclipse IDE, Java variables, data types, operators, control statements including if/else, switch/case, while, do-while and for loops, and jump statements like break and continue. The document appears to be teaching materials for a Java programming course.

Uploaded by

pavan.teens127
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2K views134 pages

Java Notes For Selenium

The document provides an introduction to the Java programming language. It covers topics such as what Java is, why it should be used, how to download and install Java and the Eclipse IDE, Java variables, data types, operators, control statements including if/else, switch/case, while, do-while and for loops, and jump statements like break and continue. The document appears to be teaching materials for a Java programming course.

Uploaded by

pavan.teens127
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 134

Java

WWW.PAVANONLINETRAININGS.COM
Agenda

▪ Java Introduction

▪ Download and install Java

▪ Download and install Eclipse IDE

▪ Java Variables

▪ Java Data Types

▪ Java Operators

WWW.PAVANONLINETRAININGS.COM
What is Java?

▪ Java is a popular programming language, created in 1995.

▪ It is owned by Oracle, and more than 3 billion devices run Java.

▪ It is used for:
– Mobile applications (specially Android apps)
– Desktop applications
– Web applications
– Web servers and application servers
– Games
– Database connection
– And much, much more!

WWW.PAVANONLINETRAININGS.COM
Why Use Java?

▪ Java works on different platforms (Windows, Mac, Linux etc.)

▪ It is one of the most popular programming language in the world

▪ It is easy to learn and simple to use

▪ It is open-source and free

▪ It is secure, fast and powerful

WWW.PAVANONLINETRAININGS.COM
JDK, JRE and JVM

▪ JDK (Java Development Kit)

▪ JDK contains everything that will be required to develop and run Java application.

▪ JRE (Java Runtime Environment)

▪ JRE contains everything required to run Java application which has already been compiled. It
doesn’t contain the code library required to develop Java application.

▪ JVM (Java Virtual Machine)

▪ JVM is a virtual machine which works on top of your operating system to provide a recommended
environment for your compiled Java code. JVM only works with bytecode. Hence you need to
compile your Java application(.java) so that it can be converted to bytecode format (also known as
the .class file).

▪ Which then will be used by JVM to run an application. JVM only provide the environment needed
to executed Java Bytecode.

WWW.PAVANONLINETRAININGS.COM
JDK, JRE and JVM

WWW.PAVANONLINETRAININGS.COM
Download & Install Java

1) Download Link
▪ http://download.oracle.com/otn-
pub/java/jdk/9.0.4+11/c2514751926b4512b076cc82f959763f/jdk-9.0.4_windows-x64_bin.exe

WWW.PAVANONLINETRAININGS.COM
2) Once the download is complete, run the exe for install JDK. Click Next

WWW.PAVANONLINETRAININGS.COM
Java Environment Setup

Right click on myPC → Properties → Advanced system Settings →


Environment variables → System settings → path variable → New button
→ Provide path of java → OK

WWW.PAVANONLINETRAININGS.COM
Eclipse IDE

▪ https://www.eclipse.org/downloads/download.php?file=/oomph/epp/photon/R/eclipse-inst-
win64.exe

WWW.PAVANONLINETRAININGS.COM
Download and Start Eclipse IDE

1
2

WWW.PAVANONLINETRAININGS.COM
5

WWW.PAVANONLINETRAININGS.COM
WWW.PAVANONLINETRAININGS.COM
Create a Java Project

▪ To create a new Java project in Eclipse

Go to File → New → Java Project → Specify Project Name → Finish

WWW.PAVANONLINETRAININGS.COM
IntelliJ IDE

WWW.PAVANONLINETRAININGS.COM
Java Variables

▪ What is a Variable?

▪ A variable is a container which holds value.

▪ Every variable is assigned a data type which designates the type and quantity of value it can hold.

▪ In order to use a variable in a program you to need to perform 2 steps


– Variable Declaration
– Variable Initialization

WWW.PAVANONLINETRAININGS.COM
Java Variables(Cont..)

WWW.PAVANONLINETRAININGS.COM
Java Data Types

WWW.PAVANONLINETRAININGS.COM
Java Operators

WWW.PAVANONLINETRAININGS.COM
Assignment

1. Write a Java program to print 'Hello' on screen and then print your name on a separate line.
▪ Expected Output :
▪ Hello
▪ Pavan
2. Write a Java program to print the sum of two numbers.
▪ Test Data: 4 + 36
▪ Expected Output : 50

3. Write a Java program to swap two numbers.

4. Write a Java Program to swap two numbers without using third variable.

WWW.PAVANONLINETRAININGS.COM
Agenda

▪ Control Statements

▪ Conditional/Statements(Selection Statements)

▪ Loops /Iterative Statements

▪ Jump Statements

WWW.PAVANONLINETRAININGS.COM
Control Statements

WWW.PAVANONLINETRAININGS.COM
If..else

▪ In this flowchart, the code will respond in the following way:


1. First of all, it will enter the loop where it checks the condition.
2. If the condition is true, the set of statements in ‘if’ part will be executed.
3. If the condition is false, the set of statements in the ‘else’ part will be executed.

WWW.PAVANONLINETRAININGS.COM
Switch..case

▪ The switch statement defines multiple paths for execution of a set of statements. It is a better
alternative than using a large set of if-else statements as it is a multi-way branch statement.

▪ In this Switch case flowchart, the code will respond in the following steps:
1. First of all it will enter the switch case which has an expression.
2. Next it will go to Case 1 condition, checks the value passed to the condition. If it is true, Statement block
will execute. After that, it will break from that switch case.
3. In case it is false, then it will switch to the next case. If Case 2 condition is true, it will execute the
statement and break from that case, else it will again jump to the next case.
4. Now let’s say you have not specified any case or there is some wrong input from the user, then it will go
to the default case where it will print your default statement.

WWW.PAVANONLINETRAININGS.COM
Switch..case (Cont..)

WWW.PAVANONLINETRAININGS.COM
While loop

▪ While statement: Repeat a group of


statements while a given condition is
true. It tests the condition before
executing the loop body.

▪ In this flowchart, the code will


respond in the following steps:
1. First of all, it will enter the loop
where it checks the condition.
2. If it’s true, it will execute the set of
code and repeat the process.
3. If it’s False, it will directly exit the
loop.

WWW.PAVANONLINETRAININGS.COM
Do..while loop

▪ Do-while statement: It is like a while


statement, but it tests the condition at
the end of the loop body. Also, it will
executes the program at least once.

▪ In this do-while flowchart, the code will


respond in the following steps:
1. First of all, it will execute a set of
statements that is mentioned in your
‘do’ block.
2. After that, it will come to ‘while’ part
where it checks the condition.
3. If the condition is true, it will go back
and execute the statements.
4. If the condition is false, it will directly
exit the loop.

WWW.PAVANONLINETRAININGS.COM
For loop

▪ For statement: For statement execute


a sequence of statements multiple
time where you can manage the loop
variable. You basically have 3
operations here: initialization,
condition and iteration.

▪ In this flowchart, the code will


respond in the following steps:
1. First of all, it will enter the loop
where it checks the condition.
2. Next, if the condition is true, the
statements will be executed.
3. If the condition is false, it directly
exits the loop.

WWW.PAVANONLINETRAININGS.COM
When we use while, do..while & for loops

▪ Scenario 1: If u want to travel by your own vehicle(two wheeler or four wheeler),You should know
how much petrol available in your vehicle and you know how much distance to travel.

▪ For loop --- Know the initial condition and no of iterations.

▪ Scenario 2: If u want to travel in a FLIGHT, You should buy a ticket then only you are eligible to
enter into the Flight.

▪ While loop ---- First satisfy the condition then Proceed.

▪ Scenario 3: If u want to travel in BUS, You can board the bus then buy the ticket.

▪ Do..while ---- Proceed first then checking.

WWW.PAVANONLINETRAININGS.COM
Jump Statements

▪ Jump statement: Jump statement are used to transfer the control to another part of your
program. These are further classified into – break and continue.

WWW.PAVANONLINETRAININGS.COM
Break statement

▪ Break statement: Whenever a break


statement is used, the loop is
terminated and the program control is
resumed to the next statement following the
loop

▪ In this flowchart, the code will respond in


the following steps:
1. First of all, it will enter the loop where it
checks the condition.
2. If the loop condition is false, it directly exits the
loop.
3. If the condition is true, it will then check the
break condition.
4. If break condition is true, it exists from the
loop.
5. If the break condition is false, then it will
execute the statements that are remaining in
the loop and then repeat the same steps.

WWW.PAVANONLINETRAININGS.COM
Continue statement

▪ Continue statement: Continue statement is


another type of control statements. The
continue keyword causes the loop to
immediately jump to the next iteration of
the loop.

▪ In this flowchart, the code will respond in


the following steps:
1. First of all, it will enter the loop where it
checks the condition.
If the loop condition is false, it directly exits
the loop.
2. If the loop condition is true, it will execute
block 1 statements.
3. After that it will check for ‘continue’
statement. If it is present, then the
statements after that will not be executed in
the same iteration of the loop.
4. If ‘continue’ statement is not present, then
all the statements after that will be executed.

WWW.PAVANONLINETRAININGS.COM
Input From the User

▪ Scanner class is to take the input from user which is imported from java.util.Scanner package.
Scanner input = new Scanner(System.in);

▪ Get Integer Input From the User


int number = input.nextInt();

▪ Get float, double and String Input


float myFloat = input.nextFloat();
double myDouble = input.nextDouble();
String myString = input.next();

WWW.PAVANONLINETRAININGS.COM
Assignment

1. Write a Java program to get a number from the user and print whether it is positive or negative

2. Write a Java program to find greatest of 3 numbers.

3. Write a Java program that takes a year from user and print whether that year is a leap year or
not.

4. Write a Java to display the multiplication table of a given integer using for loop.

5. Write a Java program count the number of digits of the number using while loop.

6. Write a Java program to reverse a number using while loop.

7. Write a Java program to check Number is Palindrome or not using while loop.

8. Write a Java Program to print factorial of a given number using while loop, for loop.

9. Write a program to print Fibonacci Series.

WWW.PAVANONLINETRAININGS.COM
Agenda

▪ Arrays

▪ Single Dimensional Array

▪ Multi Dimensional Array

▪ Strings

WWW.PAVANONLINETRAININGS.COM
What are Java Arrays?

▪ An array is a container object that holds a fixed number of values of a single type.
▪ The length of an array is established when the array is created. After creation, its
length is fixed.
▪ There are 2 types of arrays
1. Single Dimensional
2. Two Dimensional(Double Dimensional)

WWW.PAVANONLINETRAININGS.COM
Single Dimensional Array

▪ Declare array

▪ Insert values into array

▪ Find the size of array

▪ How to read/access values from array

WWW.PAVANONLINETRAININGS.COM
Multi Dimensional Array

• Declare array

• Insert values into array

• Find the size of array

• How to read/access values from array

WWW.PAVANONLINETRAININGS.COM
Assignment (Arrays)

1. Write a Java program to calculate sum values of an array.

2. Write a Java program to calculate average of values of an array.

3. Write a Java program to print even and odd numbers in an array.

4. Write a Java program to find missing number in an array.

5. Write a program to find greatest and smallest element in an array.

6. Write a program to find duplicate elements in an array.

7. Write a program to check equality of arrays.


• Using for loop
• Uisng Arrays.equals() method

8. Add two matrices using 2 dimensional array

WWW.PAVANONLINETRAININGS.COM
Assignment (Arrays) – Searching & Sorting

1. Write a Java program to search specific element in an array.

2. Write a program to sort an array.

WWW.PAVANONLINETRAININGS.COM
Strings
▪ length(): It returns count of total number of characters present in the String.

String s=“Welcome” s.length() 7

▪ concat() : Combines a specific string at the end of another string and ultimately returns a
combined string. It is like appending another string.

String s=“Welcome”
s.concat(s1) Welcome To Java
String s1=“ To Java”

▪ trim() : The java string trim() method removes the leading and trailing spaces.

String s=“ Welcome ” s.trim() Welcome

WWW.PAVANONLINETRAININGS.COM
Strings
▪ charAt(): Returns a char value at the given index number. The index number starts from 0.

String s=“Welcome” s.charAt(3) c

▪ contains() : Searches the sequence of characters in this string. It returns true if sequence of char values are
found in this string otherwise returns false.

String s= “Welcome” s.contains(“Wel”) True

▪ equals() : Compares the two given strings based on the content of the string. If any character is not matched, it
returns false. If all characters are matched, it returns true.

String s=“Welcome” s.equals(“Welcome”) True

String s=“Welcome” s.equals(“welcome”) False

WWW.PAVANONLINETRAININGS.COM
Strings

▪ equalsIgnoreCase() : Compares two string on the basis of content but it does not check the case
like equals() method. In this method, if the characters match, it returns true else false.

String s=“Welcome” s.equalsIgnoreCase(“Welcome”) True

String s=“Welcome” s.equalsIgnoreCase(“welcome”) True

▪ replace(): Returns a string, replacing all the old characters or CharSequence to new characters.
There are 2 ways to replace methods.

String s=“Welcome” s.replace(’e’, ’a’) Walcoma

String s=“Welcome To Java” s.replace(“Java”, ”Selenium”) Welcome To Selenium

WWW.PAVANONLINETRAININGS.COM
Strings
Strings

▪ Substring() : Returns substring of a string based on starting index and ending index.

Starting Index 0 1 2 3 4 5 6
s W E L C O M E
Ending Index
1 2 3 4 5 6 7

String s=“Welcome” s.substring(1,3) el

String s=“Welcome” s.substring(0,4) Welc

String s=“Welcome” s.substring(2,4) lc

WWW.PAVANONLINETRAININGS.COM
Strings
Strings

▪ toLowerCase(): returns the string in lowercase letter.

String s=“WELCOME” s.toLowerCase() welcome

▪ toUpperCase(): returns the string in Uppercase letter.

String s=“welcome” s.toUpperCase() WELCOME

WWW.PAVANONLINETRAININGS.COM
Assignment (Strings)

1. Write a java program to compare two strings, ignoring case differences.


2. Write a Java program to concatenate a given string to the end of another string.
3. Write a java program to get the length of a given string
4. Write a Java program to get a substring of a given string between two specified positions
5. Write a Java program to convert all the characters in a string to uppercase.
6. Write a Java program to convert all the characters in a string to lowercase.
7. Write a Java program to reverse a string.
8. Write a Java program to count number of time a character repeated in a string.
9. Write a Java program to convert integer to string.
10.Write a Java program to convert string to integer.
11.Write a Java program to Swap the 2 strings.
12.Write a program to check a string is palindrome or not

WWW.PAVANONLINETRAININGS.COM
Agenda

▪ Class & Object

▪ Java Methods

▪ Java Constructor

WWW.PAVANONLINETRAININGS.COM
Java Class & Object

Object1
class Employee
{ Employee emp1=new Employee();
int eid; emp1.eid=1020;
String ename; emp1.ename="John";
double sal; Variables emp1.sal=80000;
String job; emp1.job="Manager";
void display()
emp1.display();
{ Object2
System.out.println(eid);
Class

System.out.println(ename);
Employee emp2=new Employee();
System.out.println(sal);
System.out.println(job); emp2.eid=1021;
emp2.ename="David";
} Methods emp2.sal=50000;
void bonus()
{ emp2.job="Tech Assistant";
System.out.println((sal *10) /100);
} emp2.display();
}

WWW.PAVANONLINETRAININGS.COM
Class & Object

▪ main() within class

WWW.PAVANONLINETRAININGS.COM
Class & Object

▪ main() outside class


▪ In real time development, we create classes and use it from another class. It is a better approach
than previous one.

▪ We can have multiple classes in different java files or single java file.

Student.java Student1.java

WWW.PAVANONLINETRAININGS.COM
Class & Object

▪ 3 ways to initialize object variables in java.


– By reference variable
– By method
– By constructor

WWW.PAVANONLINETRAININGS.COM
Class & Object

▪ Initialization through reference variable

Student.java Student2.java

WWW.PAVANONLINETRAININGS.COM
Class & Object

▪ Initialization through method

Student.java Student3.java

WWW.PAVANONLINETRAININGS.COM
Class & Object

▪ Initialization through constructor

Student.java Student4.java

WWW.PAVANONLINETRAININGS.COM
Java Methods

▪ A method is a set of code which is referred to by name and can be called (invoked) at any point in
a program simply by utilizing the method's name.

▪ A method as a subprogram that acts on data and often returns a value.

▪ Each method has its own name.

Parameter/s Returned Value


Case1  

Case2  

Case3  

Case4  

WWW.PAVANONLINETRAININGS.COM
Java Constructor

▪ Constructor in java is a special type of method that is used to initialize the object.

▪ Java constructor is invoked at the time of object creation.

▪ Rules for creating java constructor:


1. Constructor name must be same as its class name.
2. Constructor must have no explicit return type.

▪ There are 2 Types of Constructors.

Constructor

Default Parameterized

WWW.PAVANONLINETRAININGS.COM
Method V/s Constructor

• Method name can be anything.


• Method can return a value.

Method
• Need to call method explicitly.

• Constructor name must be same as class name.



Constructor

Constructor doesn't return a value.


• Automatically invoked at the time of object creation.

WWW.PAVANONLINETRAININGS.COM
Assignment

1. Create a Student class contains the following variables and methods.


– Class Name: Student
– Variables : SID , Sname, Sub1,Sub2,Sub3
– Methods:
▪ getStuData() Takes student details SID and Sname as parameters and assign them to variables.
▪ getStuMarks() Takes student marks as parameters and assign them to Sub1, Sub2, Sub3.
▪ totalMarks() Calculate total marks and print the student details with total marks.
– Now, create objects from Student class stu1, stu2 etc. Then call Student class methods.
2. Write a program to demonstrate constructor.
– Create a class ‘Calculation’ with 3 integer variable.
– Create a constructor for assign the values into variables.
– Then create another method ‘sum’ to calculate sum of 3 numbers.
– Now, create object and call constructor by passing 3 integer values then call sum method.

WWW.PAVANONLINETRAININGS.COM
Agenda

▪ Overloading

▪ this keyword

▪ Static variables & static methods

WWW.PAVANONLINETRAININGS.COM
Method Overloading

▪ Method Overloading in Java is a concept related to Object Oriented Programming (OOP). Java
supports overloading of methods and can distinguish between different methods with method
signatures. A situation, wherein, in the same class there are two or more methods with same
name, having different functions or different parameters, it is called Method Overloading.

▪ Why Method Overloading?


– Using Method Overloading in Java is very common among Java programmers, because it:
– Provides flexibility to call similar method for different data types
– Saves memory
– Saves time

▪ Method Overloading can be done in two ways:


➢ By changing Arguments’ data types
➢ By changing number of Arguments

WWW.PAVANONLINETRAININGS.COM
Overloading

10, 20 add(int x, int y)


10, 20, 30 add(int x, int y, int z)
10.5, 20.0 add(double x, double y)
10.5, 20.0, 30.5 add(double x, double y, double z)
10, 20.5 add (int x, double y)
20.5, 10 add (double y, int x)

WWW.PAVANONLINETRAININGS.COM
62

Can we overload java main() method?

▪ Yes, by method overloading. You can have any number of main methods in a class by
method overloading. But JVM calls main() method which receives string array as
arguments only.

WWW.PAVANONLINETRAININGS.COM
this keyword
▪ The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and
parameters, this keyword resolves the problem of ambiguity.

Output: Output:
0 10
0 20

WWW.PAVANONLINETRAININGS.COM
static

▪ The static keyword in java is used for memory management mainly.

▪ We can apply java static keyword with variables and methods

▪ The static can be:


– variable (also known as class variable)
– method (also known as class method)

static

variable method

WWW.PAVANONLINETRAININGS.COM
static
Employee

int empno;
String ename;
int deptno;
void bonus()

emp1 emp2 emp3

int empno=101; int empno=102; int empno=103;


String ename=“John”; String ename=“David”; String ename=“Scott”;
int deptno=10; int deptno=10; int deptno=10;
void bonus() void bonus() void bonus()

• Every object occupy certain amount of space in memory.


• Objects are independent. So if modify a variable value in obj1 , that will not reflect other objects
variable values.

WWW.PAVANONLINETRAININGS.COM
static

Employee

int empno;
String ename;
static int deptno=10; deptno=10
void bonus()

emp1 emp2 emp3

int empno=101; int empno=102; int empno=103;


String ename=“John”; String ename=“David”; String ename=“Scott”;
void bonus() void bonus() void bonus()
emp1.deptno=20 emp2.deptno=30
emp3.deptno=40
➢ static variable is common across all the objects. So if static variable modified, this will effect other objects.

WWW.PAVANONLINETRAININGS.COM
static variables and methods

static Non-static

Variables Methods Variables Methods

static methods    

Non-static methods    

Direct Access

Through Object

WWW.PAVANONLINETRAININGS.COM
System.out.println()

class Test class System


{ {
static String s="Welcome"; static PrintStream out;
} }

Test.s.length(); System.out.println();

WWW.PAVANONLINETRAININGS.COM
Assignment

▪ 1) Create a class Calculation with the following methods.

▪ Class Name: Calculation

➢ int sum(int x, int y) : Should accept two integer parameters and returns sum of two numbers.

➢ int sum(int x, int y, int z) : Should accept three integer parameters and returns sum of three
numbers.

➢ double sum(double x, double y) : Should accept two double type parameters and returns sum
of two numbers.

➢ double sum(double x, double y, double z) : Should accept three double type parameters and
returns sum of three numbers.

▪ Now, create object for Calculations class ‘cal’ then call different methods by passing different
inputs.

WWW.PAVANONLINETRAININGS.COM
Agenda

▪ Java Inheritance

▪ Method Overriding

▪ super Keyword

▪ final Keyword

WWW.PAVANONLINETRAININGS.COM
Inheritance

▪ In OOP, computer programs are designed in such a way where everything is an object that interact with one
another. Inheritance is one such concept where the properties of one class can be inherited by the other.

▪ It helps to reuse the code and establish a relationship between different classes.

▪ In Java, there are two classes:


1. Parent class ( Super or Base class)
2. Child class (Subclass or Derived class )

▪ A class which inherits the properties is known as Child Class whereas a class whose properties are inherited is
known as Parent class.

WWW.PAVANONLINETRAININGS.COM
Types of Inheritance

▪ Inheritance is further classified into 4 types.

WWW.PAVANONLINETRAININGS.COM
Single Inheritance

▪ In single inheritance, one class inherits the properties of another. It enables a derived class to
inherit the properties and behavior from a single parent class.

▪ This will in turn enable code reusability as well as add new features to the existing code.

▪ Here, Class A is your parent class and Class B is your child class which inherits the properties and
behavior of the parent class.

WWW.PAVANONLINETRAININGS.COM
Multilevel Inheritance

▪ When a class is derived from a class which is also derived from another class, i.e. a class having
more than one parent class but at different levels, such type of inheritance is called Multilevel
Inheritance.

▪ If we talk about the flowchart, class B inherits the properties and behavior of class A and class C
inherits the properties of class B. Here A is the parent class for B and class B is the parent class for
C. So in this case class C implicitly inherits the properties and methods of class A along with Class
B. That’s what is multilevel inheritance.

WWW.PAVANONLINETRAININGS.COM
Hierarchical Inheritance

▪ When a class has more than one child classes (sub classes) or in other words, more than one child
classes have the same parent class, then such kind of inheritance is known as hierarchical.

▪ If we talk about the flowchart, Class B and C are the child classes which are inheriting from the
parent class i.e Class A.

WWW.PAVANONLINETRAININGS.COM
Hybrid Inheritance

▪ Hybrid inheritance is a combination of multiple inheritance and multilevel inheritance.


Since multiple inheritance is not supported in Java as it leads to ambiguity, so this type
of inheritance can only be achieved through the use of the interfaces.
▪ If we talk about the flowchart, class A is a parent class for class B and C, whereas Class B
and C are the parent class of D which is the only child class of B and C.

WWW.PAVANONLINETRAININGS.COM
Method Overriding

▪ If subclass (child class) has the same method as declared in the parent class, it is known
as method overriding in java.

▪ Rules for Java Method Overriding


– method must have same name as in the parent class
– method must have same parameter as in the parent class.

WWW.PAVANONLINETRAININGS.COM
super keyword

▪ The super keyword in java is a reference variable which is used to refer


immediate parent class object.
▪ Usage of java super Keyword
➢ super can be used to refer immediate parent class instance variable.
➢ super can be used to invoke immediate parent class method.
➢ super() can be used to invoke immediate parent class constructor.

WWW.PAVANONLINETRAININGS.COM
Super Keyword

▪ super is used to refer immediate parent class instance variable.

WWW.PAVANONLINETRAININGS.COM
Super Keyword

▪ super can be used to invoke parent class method.

WWW.PAVANONLINETRAININGS.COM
Super Keyword

▪ super is used to invoke parent class constructor

WWW.PAVANONLINETRAININGS.COM
final Keyword

▪ The final keyword in java is used to restrict the user. The java final keyword can be
used for variables, methods and classes.
– variable
– method
– class

WWW.PAVANONLINETRAININGS.COM
Java final variable

▪ If you make any variable as final, you cannot change the value of final variable(It
will be constant).

Output:Compile Time Error

WWW.PAVANONLINETRAININGS.COM
Java final method

▪ If you make any method as final, you cannot override it.

Output:Compile Time Error

WWW.PAVANONLINETRAININGS.COM
Java final class

▪ If you make any class as final, you cannot extend it.

Output:Compile Time Error

WWW.PAVANONLINETRAININGS.COM
Assignment

▪ Assignment-1
▪ Create a class ‘Teacher’ which contains following variables and methods
– designation = "Teacher";
– collegeName = "BusyQA";
– does() → Teaching

▪ Create another class ‘ComputerTeacher’ which extends ‘Teacher’ class then create
objects then call methods.

WWW.PAVANONLINETRAININGS.COM
Agenda

▪ Java Interfaces

▪ Java Packages

▪ Access Modifier’s

WWW.PAVANONLINETRAININGS.COM
Java Interface

▪ An interface in java is a blueprint of a class.


▪ Interface contains final and static variables.
▪ Interface contains abstract methods.
▪ An abstract method is a method contains definition but not body.
▪ Methods in interface are public by default.
▪ Interface supports the functionality of multiple inheritance.
▪ We can define interface with interface keyword.
▪ A class extends another class, an interface extends another interface but a class implements an
interface.
▪ We can create Object reference for Interface but we cannot instantiate interface.

class interface interface


extends implements extends

class class interface

WWW.PAVANONLINETRAININGS.COM
Java Interface

class interface interface


extends implements extends

class class interface


class A interface I1
interface I
{ {
{
//Variables //abstract methods
//abstract methods
//Methods //final static variables
//final static variables
} }
}
class B extends A Interface I2 extends I1
class B implements I
{ {
{
//Variables //abstract methods
//Method implementation;
//Methods //final static variables
}
} }
class B implements I2
{
//Methods implementation;
}

WWW.PAVANONLINETRAININGS.COM
Multiple Inheritance in Java by Interface
interface interface interface interface

implements extends

interface
class implements

class

interface I extends I1, I2


class A implements I1, I2 {
{ //final static variables
//Implement all the methods from I1 & I2 //abstract methods
}
} class A implements I
{
//Implement all the methods from I
}

WWW.PAVANONLINETRAININGS.COM
Hybrid inheritance in java by interface

Class A

Interface B1 Interface B2

Class C

class C extends A implements B1, B2


{
//Implements methods from B1 & B2
}

WWW.PAVANONLINETRAININGS.COM
Selenium WebDriver is an interface

WebDriver

ChromeDriver InternetExplorerDriver FirefoxDriver

WWW.PAVANONLINETRAININGS.COM
Java Packages

▪ A java package is a group of similar types of classes, interfaces and sub-packages.


▪ Package in java can be categorized in two forms.
• Built-in package
• User-defined package
▪ There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql
etc.

WWW.PAVANONLINETRAININGS.COM
Access package from another package

▪ There are two ways to access the package from outside the package.
• import package.*;
• import package.classname;

WWW.PAVANONLINETRAININGS.COM
Access Modifiers in java

▪ The access modifiers in java specifies accessibility (scope) of a data member, method,
constructor or class.
▪ There are 4 types of java access modifiers:
• private
• default
• protected
• public

WWW.PAVANONLINETRAININGS.COM
private access modifier

• The private access modifier is accessible only within class.

WWW.PAVANONLINETRAININGS.COM
default access modifier

▪ If you don't use any modifier, it is treated as default by default. The default modifier is
accessible only within package.

▪ * In the above example, the scope of class A and its method msg() is default so it cannot be
accessed from outside the package.

WWW.PAVANONLINETRAININGS.COM
protected access modifier

▪ The protected access modifier is accessible within package and outside the package but through
inheritance only.

▪ The protected access modifier can be applied on the data member, method and constructor. It
can't be applied on the class.

WWW.PAVANONLINETRAININGS.COM
public access modifier

▪ The public access modifier is accessible everywhere. It has the widest scope among all
other modifiers.

WWW.PAVANONLINETRAININGS.COM
Access modifiers

Access Modifier within class within package outside package by outside package
subclass only

Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y

WWW.PAVANONLINETRAININGS.COM
Assignment

1. Write a program to demonstrate interface.


– Interface A : int a, int b sum()
– Class B : Implements method from A and calculate sum of a and b

▪ 2. Write a program for multiple inheritance by using interface.


– Interface A : int a, int b add()
– Interface B : int x, int y mul()
– Class Calculation : Implements methods from A and B interfaces.

WWW.PAVANONLINETRAININGS.COM
Agenda

▪ Exception Handling

WWW.PAVANONLINETRAININGS.COM
Java Exceptions

▪ Exception is an abnormal condition.

▪ In java, exception is an event that disrupts the normal flow of the program.

▪ There are two types of exceptions.


1. Checked Exceptions
2. Un-checked Exceptions

Exception

Checked Un-Checked

WWW.PAVANONLINETRAININGS.COM
Un Checked Exceptions

▪ Exceptions that are NOT checked by compiler are called Un-Checked Exceptions.
▪ Un checked Exceptions successfully compiled by Java compiler.
▪ At run time it throws exception.
▪ Examples:
– ArithmeticException
– NullPointerException
– NumberFormatException
– ArrayIndexOutOfBoundsException

WWW.PAVANONLINETRAININGS.COM
Common Un-Checked exceptions

int a=50/0 ArithmeticException

String s=null; NullPointerException


System.out.println(s.length());

String s="abc";
int i=Integer.parseInt(s); NumberFormatException

int a[]=new int[5];


a[10]=50; ArrayIndexOutOfBoundsException

WWW.PAVANONLINETRAININGS.COM
Checked Exceptions

▪ Exceptions that are checked by compiler are called Checked Exceptions.

▪ If a program contains checked-Exception code is not compiled.

▪ Examples:
– InterruptedException
– IOException
– FileNotFoundException etc.

WWW.PAVANONLINETRAININGS.COM
Common Checked exceptions

Thread.sleep(3000); InterruptedException

FileReader fr = new FileReader("C:\\Test.txt"); FileNotException


BufferedReader bfr = new BufferedReader(fr);
System.out.println(bfr.readLine()); IOException

WWW.PAVANONLINETRAININGS.COM
Java Exception Handling Keywords

▪ try

▪ catch

▪ finally

▪ throws

WWW.PAVANONLINETRAININGS.COM
Java try..catch block

▪ Java try block is used to enclose the code that might throw an exception.
▪ It must be applied at statement level within the method.
▪ Java try block must be followed by either catch or finally block.
▪ Used for both Un-checked and Checked Exceptions.
▪ Java catch block is used to handle the Exception. It must be used after the try block only.
▪ You can use multiple catch block with a single try.
Syntax of java try-catch

WWW.PAVANONLINETRAININGS.COM
Problem without exception handling

▪ Output: Exception in thread main java.lang.ArithmeticException:/ by zero

WWW.PAVANONLINETRAININGS.COM
Solution by exception handling

Output: Exception in thread main java.lang.ArithmeticException:/ by zero

rest of the code...

WWW.PAVANONLINETRAININGS.COM
Java Multi catch block

▪ If you have to perform different tasks at the occurrence of different Exceptions, use java multi
catch block.

Output:task1 completed
rest of the code...

WWW.PAVANONLINETRAININGS.COM
Java finally block

▪ Java finally block is a block that is used to execute important code such as closing connection,
stream etc.
▪ Java finally block is always executed whether exception is handled or not.
▪ Java finally block follows try or catch block.

WWW.PAVANONLINETRAININGS.COM
Usage of Java finally

▪ Cases
1. Exception doesn't occur.
2. Exception occurs and not handled.
3. Exception occurs and handled.

WWW.PAVANONLINETRAININGS.COM
Case 1: Java finally example where exception doesn't occur

Output:5
finally block is always executed
rest of the code...

WWW.PAVANONLINETRAININGS.COM
Case 2: Java finally example where exception occurs and not handled.

▪ Output:finally block is always executed

▪ Exception in thread main java.lang.ArithmeticException:/ by zero

WWW.PAVANONLINETRAININGS.COM
Case 3: Java finally example where exception occurs and
handled.

Output:Exception in thread main java.lang.ArithmeticException:/ by zero finally block is always


executed rest of the code...

WWW.PAVANONLINETRAININGS.COM
throws

▪ Used for only Checked Exceptions.


▪ It should be applied at Method level.

WWW.PAVANONLINETRAININGS.COM
throws – Example1

WWW.PAVANONLINETRAININGS.COM
throws – Example2

WWW.PAVANONLINETRAININGS.COM
Un-Checked Checked Method Level Within the method
Try..Catch Y Y N Y
throws N Y Y N

WWW.PAVANONLINETRAININGS.COM
Assingment

1. Write a java program for the following and handle exceptions by using try..catch and
finally blocks.
• Any number divide by zero.
• int a[]=null;
• a.length
• String s="abc";
• int i=Integer.parseInt(s);

2. Write a java program to handle IO Exception by using throws.

WWW.PAVANONLINETRAININGS.COM
Agenda

▪ ArrayList

▪ HashMap

▪ JDBC

WWW.PAVANONLINETRAININGS.COM
ArrayList

▪ ArrayList is pre defined class in Java used for dynamic array for storing elements.
▪ ArrayList can contains duplicate elements.
▪ We can add, insert and remove elements from ArrayList.
Syntax:

WWW.PAVANONLINETRAININGS.COM
Java ArrayList Example1

WWW.PAVANONLINETRAININGS.COM
Java ArrayList Example2

WWW.PAVANONLINETRAININGS.COM
HashMap

▪ The important points about Java HashMap:


– A HashMap contains values based on the key.
– It contains only unique elements.
– It maintains no order.

WWW.PAVANONLINETRAININGS.COM
Java HashMap Example

WWW.PAVANONLINETRAININGS.COM
JDBC – Java Database Connectivity

▪ Java JDBC is a java API to connect and execute query with the database.

▪ JDBC API uses jdbc drivers to connect with the database.

WWW.PAVANONLINETRAININGS.COM
Database and SQL

▪ Database: stores the data in the tables.


▪ SQL- a language used for communicate to the database.
– DML : Data Manipulation Language
– DDL : Data Definition Language
– DCL : Data Control Language
– TCL : Transaction Language
• DML : Data Manipulation Language
• Insert
• update
• delete
• select ( DRL - Data Retrieval Language)

WWW.PAVANONLINETRAININGS.COM
Database Components

▪ Database Client
– CLI
– GUI
▪ Database Server

WWW.PAVANONLINETRAININGS.COM
4 Steps to connect to the database in java

▪ Creating connection

▪ Creating statement

▪ Executing queries

▪ Closing connection

WWW.PAVANONLINETRAININGS.COM
JDBC Example1

WWW.PAVANONLINETRAININGS.COM
JDBC Example2

WWW.PAVANONLINETRAININGS.COM

You might also like