0% found this document useful (0 votes)
7 views11 pages

J - OOPS Concept

The document outlines key Object-Oriented Programming (OOP) principles including encapsulation, inheritance, polymorphism, and abstraction, detailing their definitions, advantages, and applications in Selenium. It explains the differences between abstract classes and interfaces, as well as concepts like constructors, method overloading, and exceptions in Java. Additionally, it discusses the String class, its immutability, and the use of StringBuffer and StringBuilder for mutable string manipulation.

Uploaded by

rajat.kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

J - OOPS Concept

The document outlines key Object-Oriented Programming (OOP) principles including encapsulation, inheritance, polymorphism, and abstraction, detailing their definitions, advantages, and applications in Selenium. It explains the differences between abstract classes and interfaces, as well as concepts like constructors, method overloading, and exceptions in Java. Additionally, it discusses the String class, its immutability, and the use of StringBuffer and StringBuilder for mutable string manipulation.

Uploaded by

rajat.kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Object-Oriented Programming (OOP) principle

Encapsulation-:
 Encapsulation is an (OOP) principle that combines the state and
behavior of an object into a single class. It restricts direct access to
the internal details of an object, ensuring better control and security
of data.
 The main advantage of encapsulated class is to achieve data hiding.
 The process of restricting the direct access to the states of an
object and providing control access with the help of behavior of
some object/class is known as Data Hiding.
Step to achieve Data Hiding-:
 Prefix the state of an object by using private class modifier.
 Define getter & setter method for the private data member.
Q. Where you used in Selenium?
In POM we have use encapsulation because we declare the element
using @findBy annotation along with private keyword and we accessing
it outside the class with the help of getter & setter method.
Inheritance -:
 Inheritance is a OOPs principle used to establish "is-a
relationship” between two classes, where one class (child)
acquires the properties and methods of another class (parent). It
helps achieve code reusability, generalization, and specialization.

 Generalization: Extracting common features (fields or methods)


from multiple classes to create a base class.
Example: Creating a Vehicle superclass for Car and Bike.

 Specialization: Adding unique features or overriding behaviors in


subclasses specific to their functionality.
 We can achieve inheritance in java using extends and
implements keyword.
 There are 5 types of inheritance
 Single level, Multi-level, Hierarchical level, Multiple level, Hybrid
level.
Q. Where we used in Selenium?
In our framework BaseClass is an example for inheritance, because all
the classes will be extended by BaseClass because of code reusability.
Similarly, architecture of selenium WebDriver is an example for inheritance
Polymorphism-:
 Polymorphism is an OOPs principle that allows a single
method to represent different behaviors in different scenarios.
 It provides flexibility and ensure code reusability by enabling
objects to take on multiple forms. It is two types -:
1. Compiler Time Polymorphism-:
 It is a process where compiler decides which behavior of a
member should be use during compilation time.
 Achieve using Method overloading, Constructor overloading,
Variable shadowing.
2. Run Time Polymorphism-:
 Here the bind between method call statement and method
implementation is happening during run time.
 We can achieve it with the help of method over riding.
Method over-riding-: The process of providing new implementation for
the super class method with the help of sub class.
Abstraction-:
 It is an oops principle which help us to hide implementation
from the user and provide only necessary details.
 It simplifies the complexity by focusing on what an object does
rather than how it does it.
 We can achieve abstraction with the help of two members
1. Abstract class 2. Interface

Abstract Class:

 An abstract class can have both concrete (implemented) and abstract (unimplemented)
methods.
 We cannot create an object of an abstract class.
 It allows partial abstraction (i.e., some methods can have implementation).

Interface:

 An interface allows full abstraction by only declaring methods


 A class implementing the interface must define all its methods

Points to remember-:
 We can have a constructor inside abstract class.
 An abstract class can have both concreate as well as abstract
method.
 We cannot create an object for abstract class.
Q. Where you used in Selenium?
WebDriver driver = new ChromeDriver();
This is an example for abstraction. Because we know all the methods of
WebDriver how it functions but we don’t know how they have been
implemented.
Ex-: get(), close(), quit() etc…
Q. Difference between Abstract Class and Interface?

Methods:

 Abstract Class: Can have both abstract and concrete methods.


 Interface: Can only have abstract methods (until Java 8), but from
Java 8 onwards, it can also have default and static methods.

Constructor:

 Abstract Class: Can have constructors.


 Interface: Cannot have constructors.

Variables:

 Abstract Class: Can have instance variables with any access


modifier.
 Interface: Can only have public static final variables (constants).

Inheritance:

 Abstract Class: Supports single inheritance (a class can inherit from


only one abstract class).
 Interface: Supports multiple inheritance (a class can implement
multiple interfaces).

Object Creation:

 Abstract Class: Cannot create an object directly.


 Interface: Cannot create an object directly; the classes implementing
the interface must define its abstract methods.

interface -: It is a keyword. Interface is a user define non-primitive data


type which is use to achieve multiple inheritance and 100% abstraction.
 By default, interface is an abstract member.
 We cannot create an object for interface.
 We can compile an interface and generate a class file for that.
 We cannot make an interface either private or protected. It
can be either default or public.
 We cannot make an interface as final member.
 We can have static methods inside interface.
 We can execute the interface.
 Static method of interface will not get inherited.
 The abstract method and public static final variable of the
interface will be inherited.
Class -: It is a keyword. Class is a user defined non-primitive data type
which help us to store a detail of an object it is also known as blue print
of an object.
 In a class we can create members of class (variable & method),
variables are used to store states of an object. Methods are used
to store behaviours of an object.
 Every class name is a non-primitive data type.
 Java provide us with many built in class which can also be used
as non- primitive data type ex String, System etc…
 The members that we create inside the class block is known
as class members.
 The class members are classified into 2 types
 Static members and non-static members
static -:
 It is a keyword. It is a modifier. Any member which is prefix with
static modifier is known as static member of a class.
 A static member will have the memory allocated inside class static
area.
 Here we have 3 static members -: static method, static variable,
static initializer.
 A static variable is shared among all instances of a class. This
means that any instance of the class can modify the value, and
the change will be reflected across all instances.
 A static method belongs to the class and not to any specific
instance of the class. It can be called without creating an object
of the class.
 Static blocks are used for static initialization of a class. This
block runs once when the class is loaded into memory and is
used to initialize static variables.

Non-static members-:
 A member which is not prefix with static modifier is known as non-
static member of class
 There are 4 types of non-static members, Non-static variable,
method, initializer and constructor.
 The non-static member gets their memory allocated inside the
object of a class. We can access the non-static members of the
class only with the help of address of the object.
 To access the non-static members, it is mandatory to create an
object.
 Non-static (instance) variables are associated with an instance of
the class. Each object of the class has its own copy of the instance
variables.
 Instance variables are stored in the heap memory, and every object
has a unique memory space for these variables.
What is an object-:
A block of memory created at the run time inside heap area which
represents the real-world object.
Q. Constructor-:
 It is a non-static member of a class.
 It helps us to load all the non-static members into the object.
 The name of constructor should always be same as that of its
class name.

 It looks just similar to a method except return type.


There are 2 types constructor-:
No argument constructor -: which does not have any formal argument
declared.
Parameterize constructor-: which has formal argument declared.
In java it is mandatory to have a constructor defined inside each & every
class created.
If the user fails to create a constructor, then compiler will add a no-
argument constructor inside class during compilation, which is known as
default constructor.
Q. Constructor overloading-:
A class having more than one constructor is known as constructor
overloading.
We can have any number of constructors inside the class but they have to
follow certain rules
 The name of the constructor should always be the class name.
 The declaration of the constructor should be differing either in
length or data type or order of declaration.
Q. Constructor chaining -:
One constructor calling another constructor is known as constructor
chaining.
Q. What is Method Overloading and where you have used in Selenium?
The process of creating more than one method with the same name but
different formal arguments is known as method overloading.
 Names of methods should be same
 The formal argument must differ in either length or type or
order of declaration.
In our framework all the methods of ITestListener are overridden in
Listener Implementation class.
Ex-: onTestSuccess, onTestStart, onTestFailur etc…
 Similarly, all the methods of WebDriver, search context,
javaScriptExecutor, TakeScreenshot and WebElement are
overridden the RemoteWebDriver class
Loading process of class-:
 The class static area will be created for initial class.
 All the methods present inside the class will be loaded to method
area.
 If any static method is present its reference will be store inside
class static area.
 If any static variables are present, it should be assigned with
default value inside class static area.
 If any static initializer is present, they should execute directly
without storing.
 The loading process is said to be completed.
Q. Explain the use of this and super keywords in Java?
 this refers to the current object, used for accessing instance
variables and methods.
 super refers to the parent class, used to call methods and
constructors from the superclass.
Q. What’s the difference between Comparable and Comparator?
 Comparable is used to define the natural ordering of objects
within a class using the compareTo method.
 Comparator is used to define multiple ways to order objects,
typically in a separate class, using the compare method."
Q. Exceptions in Java:
Exceptions are unexpected events that occur during the execution of a
program.
 Checked Exceptions: These are checked at
compile-time.
 For example, InputOutputException, SQLException,
FileNotFoundException.
 Unchecked Exceptions: These occur at runtime and are not
checked during compilation.
For example, ArrayIndexOutOfBoundsException,
NullPointerException, ClassCastException, AirthmeticException
 Error: These are not exceptions but problems beyond the control
of the programmer. For example, OutOfMemoryError,
StackOverflowError.
Q. What is the difference between throw and throws?
 throw is used to explicitly throw an exception.
 throws are used in a method signature to declare that it can
throw exceptions.
Q. What is the difference between final, finally, and finalize in Java?
final:
 Used to define constants (e.g., final int MAX = 10;).
 Prevents a class from being inherited (e.g., final class MyClass {}).
 Prevents method overriding in subclasses (final void name() {}).
finally:
 A block used in exception handling that always runs after the try-
catch block, no matter if an exception is thrown or not.
 It’s commonly used for releasing resources, like closing files or
database connections.
finalize:
 A method in the Object class that is called by the garbage collector
before an object is destroyed.

Q. Explain the Singleton Design Pattern in Java.


 Ensures a class has only one instance and provides a global
point of access to it.
 It can be achieved by making the constructor private and
providing a static method to get the instance.
Q. What is String?

 String is an object that represents a sequence of characters.


 It is part of the java.lang package and is immutable, meaning
once created, it cannot be modified.
 Operations that seem to modify a String (like concatenation)
actually create a new String object.
 It supports methods like length(), substring(), charAt(), equals(),
toUpperCase(), and toLowerCase().
 It is commonly used in Selenium for locating elements,
generating test data, and handling dynamic content.

String has three types of constructor

1. String()
2. String(String string) -: it is use to create a string object with
the given string literal as its states.
3. String(Char [ ] ch) -: it is use to create a string object and
initializing with the characters present inside character array to
convert character array into string.

Convert char [ ] to string


 Using String class (String(Char [ ] ch );
 Using valueOf() method.
 Without using built-in
Example -:
char[] ch = { 'A', 'p', 'p', 'l', 'e' };
String s1 = new String(ch);
String value = s1.valueOf(ch);
String str = "";
for (int i = 0; i < ch.length; i++) {
str = str + ch[i];
}
System.out.println("with built in function : "+str);

Convert String to Char[ ]

We can convert a string to char [ ] by using


 toCharArray()-: it is a non-static method present inside string
class. The return type of it is char[ ].
 charAt(index)-: it helps us to obtain the character present in
given index. The return type of charAt() is character.
Example-:
String string = "orange";
char [] ch= string.toCharArray();
System.out.println(ch);
System.out.println(string.charAt(0));
System.out.println(string.charAt(4));
Drawback of String-:
String is immutable so every time we try to modify the
string object a new object will be created for even a smallest
change in the String which increase the memory as well as time
taken for the execution. This will reduce the performance of the
application.

String Buffer & String Builder -:

These two classes are used to store string literal in java. They are
present inside java.lang package.

String Buffer & String Builder both of them are exact identical copy
having similar methods there are only two major difference.

 String Buffer has synchronize method inside it,


 String Builder has non-synchronize method.

 String Buffer allows only one thread to perform the task at an


instance,

 String Builder help for multi-tasking which is more than one


thread can perform the task.

 Since only one thread perform the task at an instance in String


Buffer, the time taken to execute will be more compare to
String Builder.

 String Buffers are introduced in JDK 1.0


 String Builder are introduce in the JDK 1.5

String Builder-:

 It is used to store string literals.


 It is mutable in nature means if object once created can be
modified.

Constructor of String Builder-:

StringBuilder() -:
 It creates an empty StringBuilder object of capacity 16.
 Note-: if we try to add character more than the capacity then
automatically a new container is created implicitly with
 Formula -: (Previous capacity + 1 ) * 2
StringBuilder ( int capacity)-:
 It creates a StringBuilder object with the specified capacity.

StringBuilder ( String str )-:


 It creates a StringBuilder object and initialize it with given
string.

Here we have two String method is overridden.


equal() and hashcode() are not overridden.

Methods of String Builder-:


append (String str)-: this is user to merge the given data at the last
of String Builder object.

charAt(), length(), indexOf(), substring( int startIndex),


substring(start index, end index), reverse(),
setCharAt(int index, char ch), trimToSize(),
delete(Start index, end index), delete charAtIndex()

Inheritance-: OOPS Principal

Single level inheritance-: If the inheritance is the single level is


known as single level inheritance.

class Parent { }
class Child extends Parent {--}

Multiple Inheritance-: the inheritance of more than one level is


known as multi level inheritance.

class Parent {
class Chile extends Parent { --- }
class GrandChild extends Child { --- }

Hierarchical Inheritance-: If one parent class having more then one


child class is known as hierarchical inheritance.

Class Parent { }
Class Child extends Parent { --- }
Class Child1 extends Parent { --- }

Multiple Inheritance-: If one sub class is inheriting more than one


super class at the same level is known as multiple inheritance.

Hybrid Inheritance-: It is a combination of two or more types of


inheritance.
Compile-Time

 When: Happens when the code is being compiled (before running the program).
 What Happens: The Java compiler checks the code for syntax errors, type checking,
and other issues.
 Errors Detected: Compile-time errors such as syntax errors, missing semicolons,
undeclared variables, or mismatched data types.

int a = "Hello"; // Compile-time error: incompatible types


Run-Time

 When: Happens while the program is running (after the code has been compiled and
executed).
 What Happens: The program's logic is executed, and dynamic operations like user
inputs, memory allocation, or file handling occur.
 Errors Detected: Runtime errors such as division by zero, null pointer exceptions, or
array index out-of-bounds.

int a = 10 / 0; // Run-time error: ArithmeticException

Key Differences
Aspect Compile-Time Run-Time
Time Before program execution During program execution
Errors Detected Syntax, type, and structural issues Logical errors or exceptions
Tools Involved Java Compiler (javac) Java Virtual Machine (JVM)
Examples Missing semicolons, type mismatches NullPointerException, divide by zero

In short:

 Compile-time: Ensures the program is syntactically correct.


 Run-time: Ensures the program runs correctly based on its logic.

Wrapper Class-:
 It helps the programmer to store primitive literals in the form of
object.
 All the primitive datatypes in java has a corresponding wrapper
class to achieve the task.
 Therefor with the help of wrapper class we can convert a
primitive type into non-primitive type & vice versa.
 All these wrapper class available in java.lang package
therefore we can use them without importing.

Primitive Data Type -: byte, short, int, long, float, double, char,
boolean.
Non-Primitive Data Type -: Byte, Short, Integer, Long, Float,
Double, Character, Boolean, String

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy