Core Java: Object Class
Core Java: Object Class
What is a Class?
All java codes are defined in class and Collection of Object is called class. It is a logical entity.
Object Class
Object is an instance of a class. Class is a blueprint or template from which
object are created.
Object is a real world entity such as pen, leptop, Class is a group of similar Objects.
mobile, etc.
Object is a physical entity. Class is a logical entity.
Object is created through new keyword mainly Class is declared using class Keyword. Ex.
Student S1=new Student(); class Student{}
Object is created many times as per requirement Class is Declared Once.
Objects allocates memory when it is created Class doesn’t allocated memory when it is
created.
There are many way to create object in java such There is only one way to define class in java
as new keyword, newInstance() method, using class keyword.
clone()method and deserialization.
Type of variable:
1. Local Variable:-
1. It is declare inside the methods.
2. It’s scope each only inside the method, in which it is declared.
2. Instance Variable:-
1. It is defined inside the class by outside of all methods.
2. Scope of the variable is inside inter class, we can access this variable in only
method of a class.
Static Variable:-
Static variable does not make by local variable, because the static only access to class variable. It is not
to object memory.
OOP Concept
Type of OOP’s Concept: -
Inheritance
Polymorphism
Encapsulation
Abstraction
Abstraction:
It’s an OOPs concept where we can hide implementation details of a method
Abstract method:
A method for which its declaration is written in an abstract class and its definition is written in child
class of an abstract class.
Abstract class:
A class which is declared using abstract key word and it can contain abstract methods is called as
abstract class
*Notes: Abstract class can contain non-abstract methods also it is not mandatory to have at least one
abstract method in abstract class there can be zero abstract methods in abstract class.
ANS: NO
Reason:
If java allows user to create and object of an abstract class, then user can call abstract methods also for
execution. In this scenario’s compiler will not get the definition of an abstract method for execution. So,
it will be a confusion situation for the compiler and our program execution will fail. So, to avoid this
situation it is not allowed to create an object of an abstract class.
(we can create main method, but it is of no use in abstract class as we can’t create Object of the abstract
class)
Interface: -
Interface is a blue print of a class.
It contains only abstract methods. So, to write the definition of abstract methods which are present in
an interface, we need child class which we implement the interface and we write the definition of all
abstract methods which are present in an interface.
If any class is implementing an interface, then it is mandatory for a child class to write definition all
abstract methods present in an interface.
We can have non-abstract methods also in an abstract class. But we cannot have any non-abstract
methods in an interface. All methods in an interface are abstract only. Due to which 100% abstraction is
guaranteed in an interface.
Multiple inheritance is achieved in a java using only interfaces. This is because if a child class have
multiple parent interfaces having same method names but while execution of the code compiler gets
only one definition for multiple methods which are declare in multiple parent interfaces.
Inheritance concepts is applicable for interfaces also like one interface can extends another interface.
Access modifier:
There are four types of access modifiers:
1> Private: (only in the same class) – Accessible only inside a class in which (method or variable) is
declared.
2> Default: (in all the classes only in one package) – Accessible in only single package in which the
class is written. It means, it accessible in all class which belongs to the same package.
3> Public: (in all the classes in all the packages) – Accessible in all classes which belongs to separate
packages also by using inheritance as well as creation of an object.
4> Protected: (only with inheritance) – Accessible in classes which are in other packages by using
inheritance only.
Key words to create a relationship
String is java define class not a data type. This is in build in java itself.
Here String is a class, Name is an object of the string class, and selenium is a value which can be
anything.
String in JAVA:
Length will start from 1(1,2,3,4,) but index will start from 0 (0,1,2,3,4).
CharAt(); length();indexOf();equal();equalsIgnoreCase();concat();trim();
uppercase(); lowercase();substring();contains();substring();split();
Inheritance
It’s OOPS concept where one class can access properties of another class. If one class needs to access
properties (variables + methods) of another class and that is where we need to create relationship
between these two class.
1> Single inheritance: If one child class is having only one parent class then it’s called a single
inheritance.
2> Multilevel inheritance: When there is a hierarchy of parent child class like B extends A, C
Extends B.
3> Multiple inheritance: If a child class is having multiple parent classes at a time then it’s called as
multiple inheritance. Multiple inheritance is not supported in a JAVA using classes. Due to
diamond problem. There can be a situation where multiple parent classes can have same
method name. If java allows multiple inheritance, then child class will be multiple methods of
same name from multiple parent classes. If child class calls the same methods which is present
in multiple parent classes, then compiler will get confused as there will be multiple definitions of
the same method name in multiple parent classes. So, to avoid this situation multiple
inheritance is not supported in a java using classes.
Polymorphism
It’s an OOP’s concept where we can have multiple methods with the same name, but the difference has
to be in either count of parameters or types of parameters of the methods.
1> Method over loading: If multiple methods of same name are defined in same class (in any one
class only) then it’s called as methods overloading.
2> Method Overriding: If multiple method of same name is defined in multiple classes then it is
called as methods overriding. This situation is possible in case of inheritance. Ex: If parent and
child classes are having same method name then it will be a method overriding.
*If in case of overriding parent class and child class have same name of a method and parameter
count as well as type, child class object will always execute the method which is declared in child
class only.
Encapsulation
Binding data and methods together. If child class want to perform some operations on private
variables of parent class in that case, we need to use encapsulation concept. Here parent class
writes a method which perform desired operations on its private data. This method can be either
public or protected or default. Child class access this method and its able to perform the same
operation without directly accessing private data(variable) from parent class.
Constructor
Properties:
1.Name should be same as class name (method name and class name will be same)
2.No return type
3.We need to create obj to call constructor
4.whenever we are not creating constructor, in backend java will create its own constructor in
backend, which will not be used
5.many Constructor can have same name
6.when we pass parameter in constructor it is called as parameterized constructor
7.two constructor with same name should be differ in type of parameter or no of parameter
8.we can call different constructor by passing same parameter as constructor
2 types of constructor
default --> created by java its self
Parameter --> created by user
Exception Handling
Hash Set: This is Java defined class which implements set interface.
1> Sequence of a data is not maintained in hash set. This is because there is no index assign to any
of the data values added in hash set.
2> Hash Set removes duplicate values added automatically.
Hash Map: This is Java defined class which implements Map Interface.
Properties of hash Map:
1> It Stores data in key value pair.
2> It Removes duplicate keys and values associated with duplicate keys automatically.
Difference between JDK, JRE and JVM
JVM
JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine because it doesn't
physically exist. It is a specification that provides a runtime environment in which Java bytecode can be
executed. It can also run those programs which are written in other languages and compiled to Java
bytecode.
JVMs are available for many hardware and software platforms. JVM, JRE, and JDK are platform
dependent because the configuration of each OS is different from each other. However, Java is platform
independent. There are three notions of the JVM: specification, implementation, and instance.
o Loads code
o Verifies code
o Executes code
o Provides runtime environment
JRE
JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. The Java Runtime
Environment is a set of software tools which are used for developing Java applications. It is used to
provide the runtime environment. It is the implementation of JVM. It physically exists. It contains a set
of libraries + other files that JVM uses at runtime.
The implementation of JVM is also actively released by other companies besides Sun Micro Systems.
JDK
JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software development
environment which is used to develop Java applications and applets. It physically exists. It contains JRE +
development tools.
JDK is an implementation of any one of the below given Java Platforms released by Oracle Corporation:
The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as an
interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc), etc.
to complete the development of a Java Application.
Java Variables
A variable is a container which holds the value while the java program is executed. A variable is assigned
with a datatype.
Variable is a name of memory location. There are three types of variables in java: local, instance and
static.
There are two types of data types in java: primitive and non-primitive.
1) Local Variable: A variable declared inside the body of the method is called local variable. You can use
this variable only within that method and the other methods in the class aren't even aware that the
variable exists.
2) Instance Variable: A variable declared inside the class but outside the body of the method, is called
instance variable. It is not declared as static.
It is called instance variable because its value is instance specific and is not shared among instances.
3) Static variable: A variable which is declared as static is called static variable. It cannot be local. You
can create a single copy of static variable and share among all the instances of the class. Memory
allocation for static variable happens only once when the class is loaded in the memory.
Data Type Default Value Default size
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
It is because java uses Unicode system not ASCII code system. The \u0000 is the lowest range of
Unicode system. To get detail explanation about Unicode visit next page.
Two basics JAVA classes (sub-package of java):
1>org.openqa.selenium.*
2>org.openqa.selenium.firefox.FirefoxDriver
What is clone() method newinstance() method.
JDK,JVM,JRE difference
Xpath
Select Dropdown , alert , navigate to
Frame Work
Per Page
Create Object
Per Page
Reports /Extend
Reports
TestNG/Cucumber Test Case ( we need to write selenium first 4 lines)
Extends
Driver class
Screenshot
Excel connectivity