0% found this document useful (0 votes)
372 views

Core Java: Object Class

Java is a high-level, robust, object-oriented programming language. Key concepts in Java include classes, objects, inheritance, polymorphism, encapsulation, and abstraction. A class defines the blueprint for objects, while an object is an instance of a class. Inheritance allows classes to access properties of other classes. Polymorphism allows multiple methods with the same name but different parameters. Encapsulation binds data and methods together, while abstraction hides implementation details.

Uploaded by

Prateek Dwivedi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
372 views

Core Java: Object Class

Java is a high-level, robust, object-oriented programming language. Key concepts in Java include classes, objects, inheritance, polymorphism, encapsulation, and abstraction. A class defines the blueprint for objects, while an object is an instance of a class. Inheritance allows classes to access properties of other classes. Polymorphism allows multiple methods with the same name but different parameters. Encapsulation binds data and methods together, while abstraction hides implementation details.

Uploaded by

Prateek Dwivedi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Core Java

Java is a programming language and a platform.


Java is a high level, robust, object-oriented and secure programming language.

What is a Class?
All java codes are defined in class and Collection of Object is called class. It is a logical entity.

What is an Object of class?


An instance of a class is called object. The objet has state and behavior.

What is an Instance of a Class?


Method and variable that are not class method or class variables are known as Instance method and
instance variable.
Class variables and class methods are associated with a class and Occur once per class. Instance
methods and variables occur once per instance of a class.

What is a Different between Class and object?

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.

What is a variable and Type of variables?


Variable is a piece of memory and used to store information. The memory is stored the one information
is at a time.

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.

What is Main Method in java?


The Main method is a starting execution point.

Difference between Print and Println Method


Println method prints the String and Moves cursor to a new line.
Print method prints just the string but does not move the cursor to a new line.

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.

 Using Abstract class (0% - 100%)


 Using interfaces (100%)

Limitations / drawbacks of abstract class:

1> Using abstract class, abstraction is not guaranteed to be 100%.


2> We can achieve 100% abstraction using abstract class, but it’s not guaranteed.
3> Reasons:
a. We can write non-abstract methods also in an abstract class. So, if we have even a single
non-abstract method declared in an abstract class, then abstraction will fall below 100%.
Can we create and object of abstract class?

ANS: NO

It is not allowed to create and Object of an abstract class in a java.

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.

** Difference between abstract class and 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

Class  Class: - extends

Class  Interface: - Implements

Interface  Interface: - extends

String is java define class not a data type. This is in build in java itself.

String name =” selenium”;

Here String is a class, Name is an object of the string class, and selenium is a value which can be
anything.

If we want to use any method in JAVA class, we need to create an object.

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.

The keyword to create relation ship between two classes is Extends.

There are three types of inheritance:

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.

It has two types:

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

Difference between error and exception:


1> We cannot handle errors, but we can handle exceptions.
2> If there is an error in our code, then code execution does not start after we tried to run the
code. But if we have an exception in the code then code is executed until first exception occurs
in a code.
Why we need to handle exception?
If we do not handle exceptions, then our code terminates when the execution occurs and remining
code after exception will not be executed.

Exception handling technique:


Try and Catch Block: We write the executable code in a try block if there occurs some exception in a
code which is written in a try block then control is moved in catch block based upon type of
exception handles in catch block.

There are multiple types of exceptions on high level:


1> Unchecked exceptions: These types of exceptions are not checked during compile time. We get
to know about these exceptions only after execution of the code. Examples: Arithmetic
exceptions, string out of bond exceptions etc.
2> Checked Exceptions: These types of exceptions are checked during compile time itself only.
Examples: File I/O Exceptions. We need to handle these exceptions before starting executions of
the code.
Collection
Collection is java defined framework which consist of java defined interfaces and java defined classes.
These Java defined classes implements java defined interfaces. The major use of collection is to store
and manipulate data.
Collection types:
 Array list: Array list is a java defined class Which implements list interfaces.
Properties of array list:
1> Sequence of a data which is added into array list is maintained because Java assign index
starting from zero to all data values.
2> It can store duplicate values.

 Hash Set: This is Java defined class which implements set interface.

Properties of hash set:

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.

The JVM performs the following main tasks:

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:

o Standard Edition Java Platform


o Enterprise Edition Java Platform
o Micro Edition Java Platform

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.

int data=50;//Here data is variable

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.

A local variable cannot be defined with "static" keyword.

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

boolean false 1 bit

char '\u0000' 2 byte

byte 0 1 byte

short 0 2 byte

int 0 4 byte

long 0L 8 byte

float 0.0f 4 byte

double 0.0d 8 byte

Why char uses 2 byte in java and what is \u0000 ?

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>Java.util : Collection , iterator for hashset ,

2>java.lang : system classes , string classes

3>awt : button classes

Two basic selenium classes:

1>org.openqa.selenium.*

2>org.openqa.selenium.firefox.FirefoxDriver
What is clone() method newinstance() method.

JDK,JVM,JRE difference

Public Static void main (String agr[])  meaning of this line ,

Code for inverted triangle

Xpath
Select Dropdown , alert , navigate to
Frame Work
Per Page

Obj Repository (write code for obj repository at least


first 1 ,2 line))

Create Object

Per Page

Reports /Extend
Reports
TestNG/Cucumber Test Case ( we need to write selenium first 4 lines)

Extends

Only one class Utility Class

Reusable Methods (Ex : folder creation)

Driver class

Screenshot

Excel connectivity

Data base connectivity

Page Object model

You might also like