J - OOPS Concept
J - OOPS Concept
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.
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:
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:
Constructor:
Variables:
Inheritance:
Object Creation:
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.
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.
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 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.
class Parent { }
class Child extends Parent {--}
class Parent {
class Chile extends Parent { --- }
class GrandChild extends Child { --- }
Class Parent { }
Class Child extends Parent { --- }
Class Child1 extends Parent { --- }
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.
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.
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:
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