Selenium and Java

Showing posts with label Keyword. Show all posts
Showing posts with label Keyword. Show all posts

Saturday, 25 July 2015

Java- super Keyword

super keyword in java

The super keyword in java is a reference variable that is used to refer immediate parent class object.
Whenever you create the instance of subclass, an instance of parent class is created implicitly i.e. referred by super reference variable.

Usage of java super Keyword

  1. super is used to refer immediate parent class instance variable.
  2. super() is used to invoke immediate parent class constructor.
  3. super is used to invoke immediate parent class method.
Without super keyword...
class Vehicle{
  int speed=50;
}
class Bike3 extends Vehicle{
    int speed=100;
    void display(){
    System.out.println(speed);//will print speed of Bike
  }
  public static void main(String args[]){
    Bike3 b=new Bike3();
    b.display();
  }
}
Output : 100

With super keyword...

//example of super keyword
class Vehicle{
  int speed=50;
}
class Bike4 extends Vehicle{
  int speed=100;
   
  void display(){
   System.out.println(super.speed);//will print speed of Vehicle now
  }
  public static void main(String args[]){
   Bike4 b=new Bike4();
   b.display();
  
  }
}
Output : 50

 

Thursday, 25 June 2015

Java- Final , Finally and Finalize

Java- Final , Finally and Finalize

Final:

Final is a keyword. The variable decleared as final should be initialized only once and cannot be changed. Java classes declared as final cannot be extended. Methods declared as final cannot be overridden.Final is a keyword.

final can be used to mark a variable "unchangeable"

private final String name = "foo";  //the reference name can never change

final can also make a method not "overrideable"

public final String toString() {  return "NULL"; }

final can also make a class not "inheritable". i.e. the class can not be subclassed.

public final class finalClass {...}
public class classNotAllowed extends finalClass {...} // Not allowed

Finally:

Finally is a block. The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling - it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated. Finally is a block.


lock.lock();
try {
  //do stuff
} catch (SomeException se) {
  //handle se
} finally {
  lock.unlock(); //always executed, even if Exception or Error or se
}

Finalize:

Finalize is a method. Before an object is garbage collected, the runtime system calls its finalize() method. You can write system resources release code in finalize() method before getting garbage collected. Finalize is a method.