Basics of Java for Selenium

JAVA

1.       Class is a template or blueprint from which objects are created.
2.       Object  is an instance of a class.
3.       Method is a collection of statements that are grouped together to perform an operation.
4.       Constructor - Constructor is a special type of method that is used to initialize the object.
5.       Modifiers - There are two types of modifiers access modifiers and non-access modifiers.
6.       Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object.
7.       Polymorphism is a concept by which we can perform a single action by different ways.
8.       Encapsulation is a process of wrapping code & data together into a single unit eg. capsule.
9.       Exception Handling handling is a mechanism to handle the runtime errors so that normal flow of the application can be maintained.


1. Class
It is a template or blueprint from which objects are created. A class can contain:
·         Fields or Instance Variable
·         methods
·         constructors

 Object
Object is an instance (result) of a class.

2. Instance variable
A variable which is created inside the class but outside the method.

3. Method
Method is a collection of statements that are grouped together to perform an operation.
eg. When you call the System.out.println() method, the system actually executes several statements in order to display a message on the console.

4. Constructor
Constructor is a special type of method that is used to initialize the object.

i. Difference between a constructor and a method:
a. A constructor doesn’t have a return type.
b. The name of the constructor must be the same as the name of the class.
c. A constructor is called automatically when a new instance of an object is created.
d. Constructors are called only once for a single object while regular methods could be called many times .

ii. Types of Constructor:
a. Default constructor (no-arg constructor)
b. Parameterized constructor

a.       Default Constructor - Default constructor provides the default values to the object like 0, null etc.

class Bike1{ 
Bike1()//default constructor
{
System.out.println("Bike is created");
                            //---------------------------------------------------------------------
public static void main(String args[]){ 
Bike1 b=new Bike1();  //Default constructor invoked
}

b.      Parameterized constructor - A constructor that have parameters is known as parameterized constructor.

class Student4{ 
int id;  //field or data member or instance variable
String name; 

Student4(inti,String n){          //Constructor having 2 parameters
id = i; 
name = n; 
    } 
void display(){System.out.println(id+" "+name);} 
                     //---------------------------------------------------------------------
public static void main(String args[]){ 
    Student4 s1 = new Student4(111,"Karan"); 
    Student4 s2 = new Student4(222,"Aryan");  //Parameterized constructor invoked
s1.display(); 
s2.display(); 
   } 

iii. Constructor Overloading
Constructor overloading is a technique in which a class can have any number of constructors that differ in parameter lists.

class Student5{ 
int id; 
String name; 
int age; 
Student5(inti,String n){ 
id = i; 
name = n; 
    } 
Student5(inti,Stringn,int a)    //constructor overloaded
id = i; 
name = n; 
age=a; 
    } 
void display(){System.out.println(id+" "+name+" "+age);} 
               //---------------------------------------------------------------------
public static void main(String args[]){ 
    Student5 s1 = new Student5(111,"Karan"); 
    Student5 s2 = new Student5(222,"Aryan",25); 
s1.display(); 
s2.display(); 
   } 
}

5. Modifiers
There are two types of modifiers access modifiers and non-access modifiers.

i. Access modifiers - specifies the scope of "instance variable", method, constructor or class.

a.       private - private access modifier is accessible only within class. If you make any class constructor private, you cannot create the instance of that class from outside the class.

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

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

d.      public - The public access modifier is accessible everywhere.

ii. Non-access modifiers - such as static, abstract, synchronized, native, volatile, transient etc.

a.       Static - static is used for memory management. The variable or Method that belong to the Class rather than to any particular instance.

6. public static intmethodName(int a, int b) {
}
·         public static − modifier
·         int − return type
·         methodName − name of the method
·         a, b − formal parameters
·         int a, int b − list of parameters

7. The this keyword
This keyword  is used as a reference to the object of the current class.

8. super keyword in java
super keyword is is used as a reference to the object of immediate parent class.

9. Inheritance
Inheritance  is a mechanism in which one object acquires all the properties and behaviors of parent object.
·         For Method Overriding (so runtime polymorphism can be achieved).
·         For Code Reusability.

Syntax of Inheritance
class Subclass-name extends Superclass-name 
   //methods and fields 
}

The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality.

10. Polymorphism
Polymorphism is a concept by which we can perform a single action by different ways.  Polymorphism means many forms. There are two types of polymorphism:
·         compile time polymorphism (Method overloading)
·         runtime polymorphism (Method overriding)

We can perform polymorphism by method overloading and method overriding.

                    i.            Method Overloading
If a class has multiple methods having same name but different in parameters, it is known as Method Overloading. Method overloading increases the readability of the program.

There are two ways to overload the method in java
·         By changing number of arguments
·         By changing the data type

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

Usage of Java Method Overriding
·         Method overriding is used to provide specific implementation of a method that is already provided by its super class.
·         Method overriding is used for runtime polymorphism

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.
·         must be IS-A relationship (inheritance).


iii. Final
The final keyword in java is used to restrict the user. Final can be:
1.       variable - If you make any variable as final, you cannot change the value of final variable(It will be constant).
2.       method - If you make any method as final, you cannot override it.
3.       class - If you make any class as final, you cannot extend it.

Can we declare a constructor final?
No, because constructor is never inherited.

11. Abstraction is a process of hiding the implementation details.
It shows only important things to the user and hides the internal details for example sending sms, you just type the text and send the message. You don't know the internal processing about the message delivery.

There are two ways to achieve abstraction in java
·         Abstract class (0 to 100%)
·         Interface (100%)

        i.            Abstract class
A class that is declared with abstract  keyword is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated.
Example - abstract class A{}

Abstract method
A method that is declared with abstract keyword and does not have implementation is known as abstract method.
Example - abstract void printStatus();//no body and abstract (declaring signature)

      ii.            Interface
An interface is a blueprint of a class. It has static constants and abstract methods. There are mainly three reasons to use interface.
·         It is used to achieve abstraction.
·         By interface, we can support the functionality of multiple inheritance.

iii. Difference between abstract class and interface
1) Abstract class can have abstract and non-abstract methods.
Interface can have only abstract methods.

2) Abstract class doesn't support multiple inheritance.
Interface supports multiple inheritance.

3) Abstract class can have final, non-final, static and non-static variables.
Interface has only static and final variables.

4) Abstract class can provide the implementation of interface.
Interface can't provide the implementation of abstract class.

5) The abstract keyword is used to declare abstract class.
The interface keyword is used to declare interface.

6) Example:
public abstract class Shape{
public abstract void draw();
}

Example:
public interface Drawable{
void draw();
}

Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface achieves fully abstraction (100%).

12. Encapsulation
Encapsulation is a process of wrapping code and data together into a single unit, for example capsule i.e. mixed of several medicines.
We can create a fully encapsulated class in java by making all the data members of the class private. We can use setter and getter methods to set and get the data in it.

get and set methods
They serve to set and get values from class variables that are defined as 'private' (because of security issue), but these method are defined as 'public'.

13. Exception Handling
The exception handling is a mechanism to handle the runtime errors so that normal flow of the application can be maintained.
The advantage of exception handling is to maintain the normal flow of the application.

Suppose there is 10 statements in your program and there occurs an exception at statement 5, rest of the code will not be executed i.e. statement 6 to 10 will not run. If we perform exception handling, rest of the statement will be executed. That is why we use exception handling in java.

Types of Exception
i. Checked Exception - Checked exceptions are checked at compile-time. Checked Exception uses "throws" keyword.
e.g. IOException, SQLException

ii. Unchecked Exception - Unchecked exceptions checked at runtime.
ArithmeticException
NullPointerException
NumberFormatException

There are 5 keywords used in java exception handling.
·         try
·         catch
·         finally
·         throw
·         throws

Java try-catch
Java try block is used to enclose the code that might throw an exception.


No comments:

Post a Comment