Java OOPS Concepts for Selenium
OOPS - Object Oriented Programming System
Four Fundamentals of OOPS:
i) Inheritance
ii) Polymorphism
iii) Abstraction
iv) Encapsulation
------------------------
i) Inheritance:
> It is a procedure of inheriting (reusing) the aeroplane members 9variables together with methods) from i aeroplane to unopen to other aeroplane is called Inheritance.
> Non static (object level) aeroplane members solely tin travel inherited.
> The aeroplane from where the aeroplane members are getting inherited is called equally Super class/ Parent class/base class
> The aeroplane to which the aeroplane members are getting inherited is called
Sub class/ Child aeroplane / Derived class.
> The Inheritance betwixt Super aeroplane together with Sub aeroplane is achieved using "extends" keyword.
Syntax:
Class SubClass extends SuperClass {
//body
}
They are iii types of Inheritance:
i) Single Inheritance
Ex:
Class B extends Class A
---------------------------
ii) Multi aeroplane inheritance:
Ex:
Class B extends Class A
Class C extends Class B
iii) Multiple Inheritance (*Java doesn't support)
Ex:
Class B extends Class A
Class C extends Class B
In Class C
add method
-----------------------------
Class C extends Class D
-------------------------------
Inheritance example:
----------------------
public aeroplane B {
int a = 10;
int b = 20;
populace void addition(){
System.out.println("Addition of a, b is: " + (a+b));
}
populace static void primary (String [] args){
B myObject = novel B();
myObject.addition();
}
}
---------------------------------
package javaiooperations;
public aeroplane C extends B {
/*int a =100;
int b =200;
public void addition(){
System.out.println("Addition of a, b is: " +(a+ b));
}*/
public static void primary (String [] args){
C abc = novel C();
abc.addition(); //
}
}
-------------------------------
package javaiooperations;
public aeroplane D extends C{
/*int a=1;
int b=2;
populace void addition(){
System.out.println("Addition of a, b is: "+(a + b));
}*/
populace static void primary (String [] args){
D obj = novel D();
obj.addition();
}
}
--------------------------------------
ii) Polymorphism:
Polymorphism means, beingness of Object demeanor inwards many forms.
They are 2 types of polymorphism:
i) Compile Time/ Static binding/ Method overloading
ii) Run Time polymorphism / Dynamic binding / Method overriding
-----------------------
i) Compile Time
-------------------
If 2 or to a greater extent than methods having same method cite inwards the same aeroplane but they differ inwards the next ways:
i) No of Arguments
Ex:
add (int a, int b){
}
add (int a, int b, int c) {
}
--------------------------
ii) Order of Arguments
iii) Type of Arguments
Ex:
add (int a, int b){
}
add (double a, double b){
}
------------------------------------
Example:
package javaiooperations;
public aeroplane MethodOverLoading {
populace void add together (int a, int b){
System.out.println(a+b);
}
populace void add together (int a, int b, int c){
System.out.println(a+b+c);
}
populace void add together (double a, double b){
System.out.println(a+b);
}
populace static void primary (String [] args){
MethodOverLoading obj = novel MethodOverLoading();
obj.add(2, 5);
obj.add(2, 5, 7);
obj.add(1.234, 4.567);
}
}
---------------------------------
ii) Run time/Method Overriding
If 2 methods are having same cite together with same arguments available inwards the Super aeroplane together with sub class, together with then nosotros telephone scream upward those 2 methods are overridden.
Ex:
public aeroplane Y {
int a = 10, b=20;
public void add together () {
System.out.println(a+b);
}
}
-----------------
public aeroplane Z extends Y{
int a = 1, b=2;
populace void add together () {
System.out.println(a+b);
}
populace static void primary (String [] args){
Z obj = novel Z();
obj.add(); // 3
Y obj1 = novel Y();
obj1.add(); //30
Y obj2 = novel Z();
obj2.add();
}
}
--------------------------------
iii) Abstraction:
> It is a procedure of hiding implementation details together with showing solely functionality to the user.
> In Java, nosotros bring 2 types of methods
i) Concrete Methods (The methods which are having body)
Ex:
public void addition()
{
// Method body
}
--------------
ii) Abstract Methods (The methods which are non having body)
Ex:
public void add-on ();
--------------------------------
If nosotros know the method name, but don't know what the method performs, together with then nosotros become for abstract methods.
Inheritance:
-------------
A sub aeroplane extends Super aeroplane is known equally Inheritance.
Class members inwards java:
Variables together with Methods.
Static Class Members (Class Level)
> using aeroplane cite nosotros tin access Static aeroplane members.
Non Static Class Members (Object Level)
> Using Object/Instance
-----------------------
Note 1: Static Class members are non inherited to the Sub class.
Note 2: Non Static aeroplane members are inherited to the Sub class.
--------------------------------------------
Example for Accessing Static together with Non static aeroplane members:
-----------
package JavaOOPS;
public aeroplane AbstractionExample {
static int a = 10, b = 20; //Static variables
int c = 30, d = 40; // Non static variables
populace static void add1() { // Static method
System.out.println(a+b);
}
populace void add2() // Non static method
{
System.out.println(c+d);
}
populace static void primary (String [] args) {
// Access Static Class Members using Class Name
System.out.println(AbstractionExample.a); // 10
System.out.println(AbstractionExample.b); // 20
AbstractionExample.add1();// 30
System.out.println("");
// Access Non static aeroplane members using Object /Instance
AbstractionExample obj = novel AbstractionExample();
System.out.println(obj.c); // 30
System.out.println(obj.d); // 40
obj.add2(); //70
}
}
---------------------------------------
Abstract Class
> coffee Class contains 100% concrete methods
> Abstract (incomplete) aeroplane contains i or to a greater extent than abstract methods.
> Abstract aeroplane may bring abstract together with concrete methods.
Ex:
Class1 (having 10 methods)
10 methods are concrete methods
----------
Class2 (having 10 methods)
5 methods concrete
5 methods Abstract
* Abstract class
----------------------
Class3 (having 10 methods)
10 methods Abstract
* Abstract class
------------------------------
Example for Abstract Class:
--------------------------
Syntax:
public abstract aeroplane ClassName{
public void methodName(){
//Body
}
public abstract void methodname();
}
-----------------------------
Super Class
package JavaOOPS;
public abstract aeroplane Bike {
public void engine(){
System.out.println("Bikes bring Engine");
}
public abstract void handle();
public abstract void seat();
}
--------------------------------
Sub Class:
package JavaOOPS;
public aeroplane HeroHonda extends Bike{
@Override
populace void handle() {
System.out.println("Bikes bring Handle");
}
@Override
populace void seat() {
System.out.println("Bikes bring Seats");
}
populace static void primary (String [] args){
HeroHonda obj = novel HeroHonda();
obj.engine();
obj.handle();
obj.seat();
}
}
------------------------------------------------
iv) Encapsulation:
It is a procedure of wrapping code together with information together into a unmarried unit.
Encapsulation is the technique making the fields inwards a aeroplane someone together with providing access via populace methods.
Advantages:
> It provides us the command over the Data.
> By providing solely setter together with getter methods, nosotros tin brand a aeroplane read solely or write only.
If nosotros don't define setter method together with then read only
If nosotros don't define getter method together with then write only.
---------------------------------------------
Java Interfaces:
> Interface is a Java type Definition block which is 100% abstract.
(Contains 100% abstract methods)
> All the Interface methods are yesteryear default populace together with abstract.
> Static together with in conclusion modifiers are non allowed for Interface methods.
> In Interfaces, variables bring to initialize at the fourth dimension of declaration.
ex:
int a = 10; // Correct
int a; // Incorrect
> In Interfaces variables are populace static in conclusion yesteryear default.
> Interface is going to travel used using "implements" keyword.
------------------------
Example:
package JavaOOPS;
public interface Example2 {
int a = 10, b =20;
public abstract void add();
void sub();
}
--------------------------
package JavaOOPS;
public aeroplane Exa4 implements Example2{
@Override
populace void add() {
System.out.println("Addition");
}
@Override
populace void sub() {
System.out.println("Subtraction");
}
populace static void primary (String []args){
Exa4 obj= novel Exa4();
obj.add();
obj.sub();
}
}
--------------------------------------------------
Sumber http://www.gcreddy.com/
OOPS - Object Oriented Programming System
Four Fundamentals of OOPS:
i) Inheritance
ii) Polymorphism
iii) Abstraction
iv) Encapsulation
------------------------
i) Inheritance:
> It is a procedure of inheriting (reusing) the aeroplane members 9variables together with methods) from i aeroplane to unopen to other aeroplane is called Inheritance.
> Non static (object level) aeroplane members solely tin travel inherited.
> The aeroplane from where the aeroplane members are getting inherited is called equally Super class/ Parent class/base class
> The aeroplane to which the aeroplane members are getting inherited is called
Sub class/ Child aeroplane / Derived class.
> The Inheritance betwixt Super aeroplane together with Sub aeroplane is achieved using "extends" keyword.
Syntax:
Class SubClass extends SuperClass {
//body
}
They are iii types of Inheritance:
i) Single Inheritance
Ex:
Class B extends Class A
---------------------------
ii) Multi aeroplane inheritance:
Ex:
Class B extends Class A
Class C extends Class B
iii) Multiple Inheritance (*Java doesn't support)
Ex:
Class B extends Class A
Class C extends Class B
In Class C
add method
-----------------------------
Class C extends Class D
-------------------------------
Inheritance example:
----------------------
public aeroplane B {
int a = 10;
int b = 20;
populace void addition(){
System.out.println("Addition of a, b is: " + (a+b));
}
populace static void primary (String [] args){
B myObject = novel B();
myObject.addition();
}
}
---------------------------------
package javaiooperations;
public aeroplane C extends B {
/*int a =100;
int b =200;
public void addition(){
System.out.println("Addition of a, b is: " +(a+ b));
}*/
public static void primary (String [] args){
C abc = novel C();
abc.addition(); //
}
}
-------------------------------
package javaiooperations;
public aeroplane D extends C{
/*int a=1;
int b=2;
populace void addition(){
System.out.println("Addition of a, b is: "+(a + b));
}*/
populace static void primary (String [] args){
D obj = novel D();
obj.addition();
}
}
--------------------------------------
ii) Polymorphism:
Polymorphism means, beingness of Object demeanor inwards many forms.
They are 2 types of polymorphism:
i) Compile Time/ Static binding/ Method overloading
ii) Run Time polymorphism / Dynamic binding / Method overriding
-----------------------
i) Compile Time
-------------------
If 2 or to a greater extent than methods having same method cite inwards the same aeroplane but they differ inwards the next ways:
i) No of Arguments
Ex:
add (int a, int b){
}
add (int a, int b, int c) {
}
--------------------------
ii) Order of Arguments
iii) Type of Arguments
Ex:
add (int a, int b){
}
add (double a, double b){
}
------------------------------------
Example:
package javaiooperations;
public aeroplane MethodOverLoading {
populace void add together (int a, int b){
System.out.println(a+b);
}
populace void add together (int a, int b, int c){
System.out.println(a+b+c);
}
populace void add together (double a, double b){
System.out.println(a+b);
}
populace static void primary (String [] args){
MethodOverLoading obj = novel MethodOverLoading();
obj.add(2, 5);
obj.add(2, 5, 7);
obj.add(1.234, 4.567);
}
}
---------------------------------
ii) Run time/Method Overriding
If 2 methods are having same cite together with same arguments available inwards the Super aeroplane together with sub class, together with then nosotros telephone scream upward those 2 methods are overridden.
Ex:
public aeroplane Y {
int a = 10, b=20;
public void add together () {
System.out.println(a+b);
}
}
-----------------
public aeroplane Z extends Y{
int a = 1, b=2;
populace void add together () {
System.out.println(a+b);
}
populace static void primary (String [] args){
Z obj = novel Z();
obj.add(); // 3
Y obj1 = novel Y();
obj1.add(); //30
Y obj2 = novel Z();
obj2.add();
}
}
--------------------------------
iii) Abstraction:
> It is a procedure of hiding implementation details together with showing solely functionality to the user.
> In Java, nosotros bring 2 types of methods
i) Concrete Methods (The methods which are having body)
Ex:
public void addition()
{
// Method body
}
--------------
ii) Abstract Methods (The methods which are non having body)
Ex:
public void add-on ();
--------------------------------
If nosotros know the method name, but don't know what the method performs, together with then nosotros become for abstract methods.
Inheritance:
-------------
A sub aeroplane extends Super aeroplane is known equally Inheritance.
Class members inwards java:
Variables together with Methods.
Static Class Members (Class Level)
> using aeroplane cite nosotros tin access Static aeroplane members.
Non Static Class Members (Object Level)
> Using Object/Instance
-----------------------
Note 1: Static Class members are non inherited to the Sub class.
Note 2: Non Static aeroplane members are inherited to the Sub class.
--------------------------------------------
Example for Accessing Static together with Non static aeroplane members:
-----------
package JavaOOPS;
public aeroplane AbstractionExample {
static int a = 10, b = 20; //Static variables
int c = 30, d = 40; // Non static variables
populace static void add1() { // Static method
System.out.println(a+b);
}
populace void add2() // Non static method
{
System.out.println(c+d);
}
populace static void primary (String [] args) {
// Access Static Class Members using Class Name
System.out.println(AbstractionExample.a); // 10
System.out.println(AbstractionExample.b); // 20
AbstractionExample.add1();// 30
System.out.println("");
// Access Non static aeroplane members using Object /Instance
AbstractionExample obj = novel AbstractionExample();
System.out.println(obj.c); // 30
System.out.println(obj.d); // 40
obj.add2(); //70
}
}
---------------------------------------
Abstract Class
> coffee Class contains 100% concrete methods
> Abstract (incomplete) aeroplane contains i or to a greater extent than abstract methods.
> Abstract aeroplane may bring abstract together with concrete methods.
Ex:
Class1 (having 10 methods)
10 methods are concrete methods
----------
Class2 (having 10 methods)
5 methods concrete
5 methods Abstract
* Abstract class
----------------------
Class3 (having 10 methods)
10 methods Abstract
* Abstract class
------------------------------
Example for Abstract Class:
--------------------------
Syntax:
public abstract aeroplane ClassName{
public void methodName(){
//Body
}
public abstract void methodname();
}
-----------------------------
Super Class
package JavaOOPS;
public abstract aeroplane Bike {
public void engine(){
System.out.println("Bikes bring Engine");
}
public abstract void handle();
public abstract void seat();
}
--------------------------------
Sub Class:
package JavaOOPS;
public aeroplane HeroHonda extends Bike{
@Override
populace void handle() {
System.out.println("Bikes bring Handle");
}
@Override
populace void seat() {
System.out.println("Bikes bring Seats");
}
populace static void primary (String [] args){
HeroHonda obj = novel HeroHonda();
obj.engine();
obj.handle();
obj.seat();
}
}
------------------------------------------------
iv) Encapsulation:
It is a procedure of wrapping code together with information together into a unmarried unit.
Encapsulation is the technique making the fields inwards a aeroplane someone together with providing access via populace methods.
Advantages:
> It provides us the command over the Data.
> By providing solely setter together with getter methods, nosotros tin brand a aeroplane read solely or write only.
If nosotros don't define setter method together with then read only
If nosotros don't define getter method together with then write only.
---------------------------------------------
Java Interfaces:
> Interface is a Java type Definition block which is 100% abstract.
(Contains 100% abstract methods)
> All the Interface methods are yesteryear default populace together with abstract.
> Static together with in conclusion modifiers are non allowed for Interface methods.
> In Interfaces, variables bring to initialize at the fourth dimension of declaration.
ex:
int a = 10; // Correct
int a; // Incorrect
> In Interfaces variables are populace static in conclusion yesteryear default.
> Interface is going to travel used using "implements" keyword.
------------------------
Example:
package JavaOOPS;
public interface Example2 {
int a = 10, b =20;
public abstract void add();
void sub();
}
--------------------------
package JavaOOPS;
public aeroplane Exa4 implements Example2{
@Override
populace void add() {
System.out.println("Addition");
}
@Override
populace void sub() {
System.out.println("Subtraction");
}
populace static void primary (String []args){
Exa4 obj= novel Exa4();
obj.add();
obj.sub();
}
}
--------------------------------------------------