Java for Selenium Part 11

Java Abstraction and Encapsulation
Java Object Oriented Program

i) Inheritance

ii) Polymorphism

iii) Abstraction

iv) Encapsulation
-------------------------------------------
iii) Abstraction
-------------------------------------------
> It is a Process of hiding implementation details and showing only functionality to the user

Two types of Methods

i) Concrete Methods (The Methods which are having body)

Syntax:

accessModifier retrunType / returnTypeNothing method Name() {
Statements
----------
-----------
-------------
}

ii) Abstract Methods (The Methods which are not having body)

Syntax:

accessModifier abstract returnType / returnTypeNothing MethodName();

> If we know the method name, but don't know the method functionality then we go for Abstract Methods

> Java Class contains 100% concrete Methods.

> Abstract Class contains one or more abstract methods.
------------------------------------
Example:

Class1 (having 10 methods)

10 Methods are concrete Methods.

It is a Java Class
------------------------------
Class2 (having 10 methods)

5 Methods are concrete Methods and 5 methods are abstract methods)

It is an Abstract Class
--------------------------------
Class3 (having 10 Methods)

All Methods are Abstract Methods

It is an Abstract Class
--------------------------------
Example for Abstract Class:

public abstract class Bikes {

public void handle(){
System.out.println("Bikes have Handle");
}
   
public void seat(){
System.out.println("Bikes have Seats");
}

public abstract void engine();

public abstract void wheels();

public static void main(String[] args) {
//Bikes obj = new Bikes();
   
}
}
--------------------------------
Note: We cannot create Object/Instance in Abstract Class.

Note 2: In order to use Methods from Abstract Class then first we need to implement abstract methods
in sub class.

Using methods from Abstract Class:

public class HeroHonda extends Bikes{

public void engine() {
System.out.println("Bikes have Engine");
}

public void wheels() {
System.out.println("Bikes have Wheels");
}

public static void main (String [] args){
HeroHonda abc = new HeroHonda();

abc.seat();
abc.engine();
abc.wheels();
abc.handle();
}
}
-------------------------------------
Calling Methods in Abstract Class

public abstract class Bikes {

public void handle(){
System.out.println("Bikes have Handle");
}
   
public void seat(){
System.out.println("Bikes have Seats");
}

public abstract void engine();

public abstract void wheels();

public static void main(String[] args) {
//Bikes obj = new Bikes();
HeroHonda xyz = new HeroHonda();
xyz.engine();
xyz.wheels();
}
}
------------------------------------------
How to inherit Abstract Class Methods?

    By implementing abstract methods in Sub Class.

How to call Methods in Abstract Class?

    By creating the Object using Sub Class.
---------------------------------------------
Java Interfaces

> Interface is a Java type definition block which is 100% abstract.

i) Class vs. Abstract Class

Java Classes have 100% concrete Methods

Abstract Classes have one or more/all abstract methods.
------------------------------------
ii) Class vs. Interface

Java Classes have 100% concrete Methods

Java Interfaces have 100% Abstract Methods
------------------------------------
iii) Abstract Class vs. Interface

Abstract Classes have one or more/all abstract methods.

Java Interfaces have 100% Abstract Methods
-----------------------------------------------
> All Interface Methods by default public and abstract.

> static and final modifiers are not allowed for Interface methods

> In Interfaces Variables have to initialize at the time of declaration only.

Ex:

int a;//Incorrect
a=100;

int b=200;//Correct

> In Interfaces Variables are public and final bydefault.

> Interfaces are going to be used using "implements" keyword.
-----------------------------------------------
Create Java Interface

Java Project
    Java Package
        Java Class / Java Interface

Example:

public interface Interface1 {
   
public void engine();
public void seat();
public void wheels();
public void handle();
}

Reuse Methods from Interface to Class

public class ClassNew implements Interface1 {

public void engine() {
System.out.println("Bikes have Engine");
}

public void seat() {
System.out.println("Bikes have Seat");       
}

public void wheels() {
System.out.println("Bikes have Wheels");
}
public void handle() {
System.out.println("Bikes have Handle");   
}
public static void main (String [] args){
ClassNew obj = new ClassNew();
obj.seat();
obj.wheels();
obj.handle();
}
}
--------------------------------------------
Call methods in Interface

public interface Interface1 {
   
public void engine();
public void seat();
public void wheels();
public void handle();

public static void main (String [] args){
ClassNew abc = new ClassNew();   
abc.handle();
abc.wheels();
}
}
-----------------------------------------------
Reuse Methods from a Class to another Class - using "extends" keyword

Reuse Methods from an Abstract Class to Class - using "extends" keyword

Reuse Methods from an Interface to Class - using "implements" keyword
---------------------------------------
Assignment

How to reuse Methods from an Interface to another Interface?
-------------------------------------------
iv) Encapsulation

It is a process of wrapping code and data into a single unit.

General Example:

Capsule (Mixer of several medicines)

Encapsulation is the technique making the fields in a class private and providing access via public methods.

> It provides control over the data.

> By providing setter and getter methods we can make a class read only or write only.
-------------------------------------
Example:

Class 1:

public class Class1 {
private String name = "Test Automation";

public String getName(){
return name;
}

public void setName(String newName){
name=newName;   
}

public static void main(String[] args) {
Class1 obj = new Class1();
obj.setName("Selenium with Java");
System.out.println(obj.getName());
}
}
----------------------------------
Class 2:
public class Class2 extends Class1 {

public static void main(String[] args) {
Class2 obj2 = new Class2();
//obj2.setName("Selenium with Java");
System.out.println(obj2.getName());
}
}
--------------------------------------
Java for Selenium the Conclusion

Java Environment Setup
Java Program Structure / Java Syntax
---------------------------------------
A) Java Fundamentals / Basics

1) Comments
2) Data Types
3) Modifiers
4) Variables
5) Operators
6) Flow Control
    Conditional Statements
    Loop Statements
8) String Handling
9) Arrays
10) IO Operations, and File Handling
11) Methods
    Built in Methods
    User Defined Methods
13) Exception Handling
---------------------------------
B) Java OOPS

1) Inheritance
2) Polymorphism
3) Abstraction
4) Encapsulation
-------------------------------------------

0 comments:

Post a Comment