HomeSelenium

Modifiers and Interfaces (Selenium 4 – Session 8)


Modifiers

  • Modifiers in Java can be categorized as below:
public Access Modifier
 
  • Classes/variables/methods specified with ‘public’ access modifier can be accessed directly by the classes which are in the same package – Demonstrate
  • Classes/variables/methods specified with ‘public’ access modifier can be accessed by the classes outside the package after importing the classes – Demonstrate
private Access Modifier
 
  • Java classes cannot be specified with ‘private’ access modifier – Demonstrate
  • variables/methods specified with ‘private’ access modifier can be accessed only with in the same class – Demonstrate
default Access Modifier
  • When no modified is specified before classes/variables/methods, then we name it as default modifier – Demonstrate
  • default means public to all the classes inside the same package and private to the classes which are outside the package – Demonstrate
protected Access Modifier
  • protected means public to all the classes inside the same package and private to all the classes which are outside the package except child classes
  • Java classes cannot be specified with ‘protected’ access modifier – Demonstrate
  • While accessing the protected variables/methods outside the packages using sub-classes, we don’t have to create an object to access them as they are inherited variables and methods – Demonstrate
static Non-Access Modifier
  • Java classes cannot be specified with ‘static’ non-access modifier – Demonstrate
  • Variables declared directly inside the class but outside the methods and are specified with ‘static’ modifier are known as static variables
  • Memory allocated to the static variables is different from the memory allocated to the instance variables
  • static variables needs to be accessed with the help of Class name, as they belong to the Class memory
  • static variables are generally used to store common data, where as Object variables/Instance variables are used to store Object specific data.
  • wheels variable can be used as a static variable/class level variable as it has common data i.e. wheels count is 4 for all the cars in the market
  • Where as cost variable cannot be used as a static variable as its value changes from car to car, hence we use it as an Object variable/Instance variable.
  • static can also be used with methods
  • static can only access static stuff
  • You have to create object to overcome this
final Non-Access Modifier
  • The value of the variable cannot be changed on specifying it with final non-access modifier
  • final modifier specified classes cannot be inherited/extended by other classes
  • final modifier specified methods in a class cannot be overridden by its sub-classes
abstract Non-Access Modifier
  • variables cannot be specified with ‘abstract’ non-access modifier – Demonstrate
  • On specifying a method with abstract modifier, we can just declare the method without implementing it
  • Classes having at-least one abstract specified method must be specified as abstract
  • Sub-Class inheriting the Super-Class needs to implement the abstract specified methods in Super-Class
  • Purpose of abstract methods – Used when the super-class dont have to implement everything, and when the sub-classes inheriting the super-class needs to implement them.
  • Objects cant be created for abstract classes, we have to create a Sub-Class and access its variables/methods using Sub-Class object reference
Interfaces
The purpose of an interface is to just to declare all the functionalities required before actually implementing them.
  • Interfaces looks similar to Classes and are extensions of abstract classes
  • Create an interface say ‘Bank’ in Eclipse IDE and create variables & methods inside it
  • Variables in the interfaces are of static and final type
  • In abstract classes, we can have both methods (i.e. implemented and non-implemented), where as in interfaces, we cannot implement any methods.
  • Classes use implements keyword to implement any interface
  • Classes implementing an interface can have their own specific methods apart from methods which are acquired from an interface
  • Objects cannot be created for an interface – Demonstrate
  • Object can be created for the Classes which are implementing the interfaces, for accessing interface defined methods and class specific methods  – Demonstrate
  • Follow the below steps to provide the access the interface specific methods and not to access the class specific methods
  • Create an object for the Class which is implementing the interface
  • Assign the object of the class to the interface reference variable
  • Using the interface reference variables, we can now access only the methods which are declared in the interface

By,
Arun Motoori

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *

For FREE Testing Tutorials & Videos

X
Open chat
Contact Us on Whatsapp