WebDriver java concepts - Private final static / public, default, protected, and private / Abstract / Interface / Final / Super / this
Continuing the WebDriver java concepts category here are a set of simple yet complex terminologies used in webdriver frameworks wrt Java:
Private final static attribute vs private final attribute -
Static means "associated with the class"; without it, the variable is associated with each instance of the class. If it's static, that means you'll have only one in memory; if not, you'll have one for each instance you create. static means the variable will remain in memory for as long as the class is loaded; without it, the variable can be gc'd when its instance is.
In general, static means "associated with the type itself, rather than an instance of the type."
That means you can reference a static variable without having ever created an instances of the type, and any code referring to the variable is referring to the exact same data. Compare this with an instance variable: in that case, there's one independent version of the variable per instance of the class. So for example:
Test x = new Test();
Test y = new Test();
x.instanceVariable = 10;
y.instanceVariable = 20;
System.out.println(x.instanceVariable);prints out 10: y.instanceVariable and x.instanceVariable are separate, because x and y refer to different objects.
You can refer to static members via references, although it's a bad idea to do so. If we did:
Test x = new Test();
Test y = new Test();
x.staticVariable = 10;
y.staticVariable = 20;
System.out.println(x.staticVariable);then that would print out 20 - there's only one variable, not one per instance. It would have been clearer to write this as:
Test x = new Test();
Test y = new Test();
Test.staticVariable = 10;
Test.staticVariable = 20;
System.out.println(Test.staticVariable);That makes the behaviour much more obvious. Modern IDEs will usually suggest changing the second listing into the third.
There is no reason to have a declaration such as
private final int NUMBER = 10;If it cannot change, there is no point having one copy per instance.
Difference between public, default, protected, and private?
Abstract Methods and Classes
An abstract method is a method that is declared without an implementation (without braces, and followed by semicolon), like this:
abstract void moveTo(double deltaX, double deltaY);
If a class includes abstract methods, the class itself must be declared abstract, as in:
public abstract class GraphicObject {
// declare fields
// declare non-abstract methods
abstract void draw();
}
When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.
Note: All of the methods in an interface are implicitly abstract, so the abstract modifier is not used with interface methods (it could be—it's just not necessary).
What Is an Interface?
In its most common form, an interface is a group of related methods with empty bodies. A bicycle's behavior, if specified as an interface, might appear as follows:
interface Bicycle {
// wheel revolutions per minute
void changeCadence(int newValue);
void changeGear(int newValue);
void speedUp(int increment);
void applyBrakes(int decrement);
}
To implement this interface, the name of your class would change (to a particular brand of bicycle, for example, such as ACMEBicycle), and you'd use the implements keyword in the class declaration:
class ACMEBicycle implements Bicycle {
// remainder of this class
// implemented as before
}
Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
Java Final Keyword
A java variable can be declared using the keyword final. Then the final variable can be assigned only once.
A variable that is declared as final and not initialized is called a blank final variable. A blank final variable forces the constructors to initialise it.
Java classes declared as final cannot be extended. Restricting inheritance!
Methods declared as final cannot be overridden. In methods private is equal to final, but in variables it is not.
Java local classes can only reference local variables and parameters that are declared as final.
A visible advantage of declaring a java variable as static final is, the compiled java class results in faster performance
Super :
super keyword is used to call a superclass constructor and to call or access super class members(instance variables or methods).
syntax of super : super(arg-list)
When a subclass calls super() it is calling the constructor of its immediate superclass.
super() must always be the first statement executed inside a subclass constructor.
super.member
Here member can be either method or an instance variables.
This second form of super is most applicable to situation in which member names of a subclass hide member of superclass due to same name.
this
‘this’ is used for pointing the current class instance. It can be used with variables or methods. Look into the following example:
class Test{
private int i=10;
public void m(){
System.out.println(this.i);
}}
In the above code this is used for the current instance. Since this is instance of a class it cannot be used inside a static method.
Webdriver Java Concepts - Private Final Static / Public, Default, Protected, And Private / Abstract / Interface / Final / Super / This >>>>> Download Now
ReplyDelete>>>>> Download Full
Webdriver Java Concepts - Private Final Static / Public, Default, Protected, And Private / Abstract / Interface / Final / Super / This >>>>> Download LINK
>>>>> Download Now
Webdriver Java Concepts - Private Final Static / Public, Default, Protected, And Private / Abstract / Interface / Final / Super / This >>>>> Download Full
>>>>> Download LINK Ph