Selenium/Java-OOPs-Abstraction

In last post, we learnt about different types of constructors Selenium/Java- CoC in Java which are important and very useful while writing  Selenium automation scripts. Now we will move towards Java OOPs concepts and in this post we will deal with Abstraction.

Abstraction:

  • It is a process of hiding implementation details and showing only functionality to the user.

  • It hides internal details.

  • An abstract class is declared with Abstract keyword.

  • We need to declare class as abstract to use abstract methods.

  • We can not create objects of the Abstract class.

  • Abstract class can have both abstract as well as concrete(normal) methods.

  • It is most useful when two sub-classes are doing same thing in different ways.

  • When we define sub-classes, we use extends keyword to show that it is a child class of parent class.

  • Super keyword is also used in abstraction when we need to use immediate parent class instance variable, method or constructor.




Example:
// defining class named Abstraction using abstract keyword
public abstract class Abstraction {

int id;

String name;

// defining constructor

Abstraction(int id,String name){

this.id=id;

this.name=name;

}

abstract void info(); // defining abstract method which hides implementation details

 // defining sub-class named showid , here we only want to print the id.

static class showid extends Abstraction{

showid (int id, String name) {

super(id, name); // Using parent class constructor

}

//Here we provide the implementation details of  abstract method


void info(){

System.out.println("Id is: "+id);

}

}

// Another sub-class named showname, here we only want to print the name

static class showname extends Abstraction{

showname (int id, String name) {

super(id, name); // using parent class constructor

}

// Using same abstract method in a different way.
void info(){

System.out.println("Name is: "+name);

}

}

// Creating the objects of the sub-classes
public static void main(String[] args) {

showid id=new showid(752669,"Raj");

showname name=new showname(752669,"prince");

id.info(); // Calling the abstract method of showid sub-class

name.info(); // Calling the abstract method of showname sub-class
}
}

Bravo.. ! just try this code and explore more about Abstraction by manipulating the code. we will see other OOPs concepts in upcoming posts.

Cheers..!







Comments

Post a Comment

More posts on selenium...

Selenium/Java-OOPs-Encapsulation

Selenium: Automate Web Application Login

Selenium/Java- CoC

Selenium/Java-OOPs-Inheritance

Selenium: Waits

Selenium: All about XPath Locator in WebDriver

Selenium: Capturing the screenshot

Selenium: BDD- The Disruptive Framework

Selenium: Frameworks