Selenium – Basics of Java Part 1

We will focusing on Java as a programming language in selenium WebDriver we need to be comfortable with the basics of Java. This topic we will discuss about basic concepts around Java language, Syntax for basic Java statements, exception handling and concept of classes and objects.

Key Objectives:

  • Object oriented programming concepts
  • Language and syntax basics
  • Object, classes and method
  • Exception Handling

Object Oriented Programming (OOPS) Concepts


Objects are key to understanding object-oriented technology. Look around right now and you’ll find many examples of real-world objects: your dog, your desk, your television set, your bicycle.

These real-world objects share two characteristics: they all have state and they all have behavior. For example, dogs have state (name, color, breed, hungry) and dogs have behavior (barking, fetching, and slobbering on your newly cleaned slacks).

Software objects are modeled after real-world objects in that they, too, have state and behavior. A software object maintains its state in variables and implements its behavior with methods.

Definition: An object is a software bundle of variables and related methods.

Bundling code into individual software objects provides a number of benefits, including:

  • Modularity
  • Information hiding
  • Code re-use
  • Plug-ability and debugging ease


A class is nothing but a blueprint or a template for creating different objects which defines its properties and behaviors. Java class objects exhibit the properties and behaviors defined by its class. A class can contain fields and methods to describe the behavior of an object.

A class in java can contain:

  • Data member (Fields)
  • Method
  • Constructor
  • Block

Example of Object and Class

class Student{
int id; //data member (fields )
String name;//data member(fields)

public static void main(String args[]){
Student1 s1=new Student1();//creating an object of Student


Inheritance refers to a feature of Java programming that lets you create classes that are derived from other classes. A class that’s based on another class inherits the other class. The class that is inherited is the parent class, the base class, or the superclass.

Syntax of Java Inheritance

class Subclass-name extends Superclass-name  {
//methods and fields

Types of inheritance in java

Basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.

Example of Simple inheritance

class Employee{
float salary=40000;
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println(“Programmer salary is:”+p.salary);
System.out.println(“Bonus of Programmer is:”+p.bonus);


Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does it.

Abstract class – A class that is declared as abstract is known as abstract class. can have abstract and non-abstract methods (method with body). It needs to be extended and its method implemented. It cannot be instantiated.

  • Abstract classes may or may not contain abstract methods, i.e., methods without body ( public void get(); )
  • But, if a class has at least one abstract method, then the class must be declared abstract.
  • If a class is declared abstract, it cannot be instantiated.
  • To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it.
  • If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.

public abstract class Employee {
private String name;
private String address;
private int number;

public abstract double computePay();
// Remainder of class definition

Suppose Salary class inherits the Employee class, then it should implement the computePay() method as shown below

public class Salary extends Employee {
private double salary;   // Annual salary

public double computePay() {
return salary/52;
// Remainder of class definition


In its most common form, an interface is a group of related methods with empty bodies.

An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.

Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements.

Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.

An interface is similar to a class in the following ways −

  • An interface can contain any number of methods.
  • An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
  • The byte code of an interface appears in a .class file.
  • Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

However, an interface is different from a class in several ways, including −

  • You cannot instantiate an interface.
  • An interface does not contain any constructors.
  • All of the methods in an interface are abstract.
  • An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
  • An interface is not extended by a class; it is implemented by a class.
  • An interface can extend multiple interfaces.


interface Animal {

public void eat();

public void travel();



A package is a namespace for organizing classes and interfaces in a logical manner. Placing your code into packages makes large software projects easier to manage.

Conceptually you can think of packages as being similar to different folders on your computer. You might keep HTML pages in one folder, images in another, and scripts or applications in yet another. Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.

Method Overloading

If a class have multiple methods by same name but different parameters, it is known as Method Overloading.


class Calculation {
void sum(int a,int b) {

void sum(double a,double b) {

public static void main(String args[]) {
Calculation obj=new Calculation();


Method Overriding

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

In other words, If subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.


class Vehicle{
void run(){
System.out.println(“Vehicle method”);

class Bike2 extends Vehicle{
void run(){
System.out.println(“Bike method”);

public static void main(String args[]){
Bike2 obj = new Bike2();;

Output: Bike method


Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one IS-A test is considered to be polymorphic.

Leave a Reply Cancel reply