top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

JAVA OOPS Concepts - Implementation of OOPS concept In SELENIUM FRAMEWORK

JAVA OOPS Concepts - Implementation of OOPS concept In SELENIUM FRAMEWORK - Part 1

In almost all of the Automation interviews, you may face this question:


“What do you know about JAVA OOPS?

Where have you actually used Java Object Oriented Concepts in Selenium Automation Framework.?”


To answer such questions,

1.We need to clearly understand the basic structure of Java Programming

2. Understand OOPs Concept

3. Be able to analyze the framework used in our projects and explain how the concepts have been implemented


Basic concepts of Java


Class and Object in JAVA

Class : Collection of objects is called class. It is a logical entity. It is a template or a blueprint of all the objects of a similar kind(State) with some behavior. It does not occupy any space or memory.

Example: If Laptop Is a class, There are various brands of Laptops which can be referred as State and Different Operating Systems , RAM speed etc in each of them can be called Behaviors

Classes define states as instance variables

Behaviors are instance methods.


Object : is an Entity - that has state and behavior. It’s a real-world entity. An Object can be defined as an instance of a class. It occupies some space or memory

State tells us how the object looks or what properties it has.

Example: Dell, Acer, Mac books can be objects in the class of Laptop


Behavior tells us what the object does.

Example : Windows OS, MAC OS, Linux etc can be considered as Behaviors of the object in Laptop



Constructor and Methods In JAVA


Constructor of a CLASS has the same name as the Class doesn't have a return type. Every class has a default constructor which is created at compilation. All objects are instantiated using constructor of its class using the keyword ‘new’. They can be parameterized or unparameterized

Example :









Method is a way to perform some task. A method is a block of code or collection of statements or a set of code grouped together to perform a certain task or operation

Reusability - We write a method once and use it many times.

The method is executed only when we call or invoke it. This way We do not require to write code again and again.


Java Constructor

Java Method

​A constructor is used to initialize the state of an object

A method is used to expose the behavior of an object

A constructor must not have a return type.

A method must have a return type.

The constructor is invoked implicitly.

The method is invoked explicitly

The Java compiler provides a default constructor if you don't have any constructor in a class.

The method is not provided by the compiler in any case.

The constructor name must be same as the class name.

The method name may or may not be same as the class name.

Interface and Class in JAVA

In Java, an Interface is an abstract type used to specify the behavior of a class. An interface in Java is a blueprint of a class. A Java interface contains static constants and abstract methods. The interface in Java is a mechanism to achieve abstraction.

Class

Interface

The keyword used to create a class is “class”

​The keyword used to create an interface is “interface”

A class can be instantiated i.e, objects of a class can be created

​An Interface cannot be instantiated i.e, objects cannot be created.

Classes does not support multiple inheritance. Which means there can be only one ‘Parent’ or “super”Class

​Interface supports multiple inheritance which means a class can implement multiple interfaces

A CLASS can inherit another class.

It cannot inherit a class.

It can be inherited by another class using the keyword ‘extends’.

It can be inherited by a class by using the keyword ‘implements’ and it can be inherited by an interface using the keyword ‘extends’.

It can contain constructors.

It cannot contain constructors.

It cannot contain abstract methods.

It contains abstract methods only.

Variables and methods in a class can be declared using any access specifier (public, private, default, protected)

All variables and methods in an interface are declared as public.

Variables in a class can be static, final or neither.

All variables are static and final.


syntax for Interface:

interface <computers> {

// declare constant fields

// declare methods that abstract

}


Example :


Public void computerType();
		
  Public void Usertype();
     // by default.
}




Class as we know is a collection of objects

Example for Class:



Public class Laptop  { 
body……
}

Example for Class:
Public class Laptop  { 
body……
}


// CLASS Lap top implements Interface Computers



Public class Laptop implements computer {

    Public void computerType() {
	System.out.println("Select Laptop from computers”);
     }
    Public void Usertype() {
	System.out.println("Select student from Usertype”);
   }

}


Why And When To Use Interfaces?

• To achieve security - hide certain details and only show the important details of an object (interface).

• Java does not support "multiple inheritance" between classes(a class can only inherit from one superclass)


However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma




JAVA Modifiers

A Java Modifier is a keyword used to make changes to the characteristics or define the scope of variables, methods, constructors and class in a java program


Access modifiers define the scope of variables, methods, constructors and classes which means the modifier used can control the access level across the program


• Class can be ”public” or “default “

“public” – can be accessed by other classes

“default” – can be accessed by classes only in the same package as the class


• Attributes, Methods, Constructors can be ”public” ,”private”, “default “ Or “protected”

”public” The code is accessible for all classes

”private” The code is only accessible within the declared class

“default “ The code is only accessible in the same package.

“protected” The code is accessible in the same package and subclasses


Non-Access modifiers do not control access level, but provides other functionality


  • Class can be declared as “final” or “abstract”

“final” The class cannot be inherited by other classes

“abstract” The class cannot be used to create objects

(To access an abstract class, it must be inherited from another class).



  • Attributes, Methods, Constructors can be defined to be either “final”, ”static”, ”abstract”, “transient”, ”synchronized”, or “volatile

“final” Attributes and methods cannot be overridden/modified

“static” Attributes and methods belongs to the class, rather than an object

“abstract” Can only be used in an abstract class, and can only be used on methods

The method does not have a body, for example abstract void run();

The body is provided by the subclass (inherited from).

“transient” Attributes and methods are skipped when serializing the object containing them

“synchronized” Methods can only be accessed by one thread at a time

“volatile” The value of an attribute is not cached thread-locally

and is always read from the "main memory"


After reviewing some important concepts about Java, We now try to understand the various OOPs concepts in Java as a programming Language with a few examples.


ABSTRACTION

Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user 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.

This is easily achieved using Abstract classes and Interfaces in Java


Example

When user logs in to his email after entering username and password, he is able to see his inbox and other mail folders This is the functionality. He is unable to see the backend transactions between the user laptop and the server. Those transactions remain hidden. This is due to Abstraction principle.


ENCAPSULATION

The meaning of Encapsulation, is to make sure that everything is in a tight ‘capsule’ which implies “sensitive" data is hidden from users. Encapsulation simply means binding object state(fields) and behaviour(methods) together. If you are creating class, you are doing encapsulation. To achieve this, you must:

• declare class variables/attributes as private

• provide public get and set methods to access and update the value of a private variable


Example:

In a laptop class, variable Username can be public and variable Password can be private. This way Password is secured from other classes won’t have access to important information



INHERITANCE

When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories:

• subclass (child) - the class that inherits from another class

• superclass (parent) - the class being inherited from

To inherit from a class, use the extends keyword.

Remember that inheritance into a class is possible only from one class. Multiple inheritance into a class can be achieved using interfaces.



POLYMORPHISM

Polymorphism refers to one of the OOPs concepts in Java which is the ability of a variable, object or function to take on multiple forms.

The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.


Example:


public interface Computers{}

public class Laptop{}

public class Dell extends Laptop implements Computers {}


Here, Dell class is considered to be polymorphic since this has multiple inheritances. Following are true for the above example

• Dell is a Computer

• Dell is a Laptop

• Dell is a Dell

• Dell is an object


When we apply the reference variable facts to a Dell object reference, the following declarations are legal −

Dell D = new Dell();

Computer c =D;

Laptop L =D;

Object O=D;



Static Polymorphism:

Polymorphism that is resolved during compiler time is known as static polymorphism. Method overloading can be considered as static polymorphism example.


Dynamic Polymorphism :

It is also known as Dynamic Method Dispatch. Dynamic polymorphism is a process in which a call to an overridden method is resolved at runtime rather, that’s why it is called runtime Polymorphism. Method Overriding is due to Dynamic polymorphism.


In the Next part of this blog we will learn about the implementation of these concepts in our Framework.


174 views0 comments

Recent Posts

See All
bottom of page