Search

Showing posts with label java tutorials for webdriver. Show all posts
Showing posts with label java tutorials for webdriver. Show all posts

Sunday, August 30, 2015

What Is The Use of "final" Keyword In Java

Interviewer can ask you question "What is final keyword in java". "final" is a keyword which is reserved in java. It restrict the user. "final" keyword In Java can be used with different three contexts in java like variable, method and class. If define an entity which may only be assigned once. Now let's try to understand how to use final keyword in java with practical examples for variable, method and class.
final variable
Once you initialize value to final variable, You can not change it latter on anywhere. It will stay as it is. In bellow given example, WHEELS is final variable and initialized with 2. Then i am trying to change it from 2 to 4 but it is showing me compilation error.

public class FinalKeyword {

final int WHEELS = 2; //final variable initialized.

void bikeWheels() {
WHEELS = 4; //This is not allowed. Shows you compile time error.
}

public static void main(String[] args) {
FinalKeyword fk = new FinalKeyword();
fk.bikeWheels();
}
}

In short, We can not change value of final variable.

final method
If method is declared with final keyword, It is called final method. You can not override final methods in sub class. So here final keyword will helps you to restrict method overriding. Example is as bellow. Here COLLEGENAME() is final method in parent class and then i am trying to override it in child class but it is showing me compile time error.

public class College {

final void COLLEGENAME(){ //Method declared as final in parent class
System.out.println("abc");
}
}

public class Department extends College{

void COLLEGENAME(){//It will show compile time error because it will not allow to override final methods.
System.out.println("xyz");
}

public static void main(String args[]){
Department dep= new Department();
dep.COLLEGENAME();
}
}

final class
If class is declared with final keyword then class is final class and any other class can not extend it. Here, WILDANIMALS is final class and i am trying to extend it in sub class Cow but it is showing me error.

final class WILDANIMALS {

}


//It will show compile time error as WILDANIMALS is final class. You can not extend it.
public class Cow extends WILDANIMALS{

}

This way we can restrict class extension too using final keyword.

Points to remember about final class
  1. final keyword can be used with variable, method or class to make them final.
  2. Local final variable must be initialized during declaration or inside constructor. Otherwise it will show you an error.
  3. We can not declare constructor as final. VIEW MORE about constructor in java.
  4. By default all variables declared in interface are implicitly final. VIEW MORE about interface in java.
  5. Value of final variable can not be changed.
  6. We can not override final method.
  7. Class can not be extended to final class. VIEW POST on inheritance to know more about class extend.
  8. final variable which is not initialized during declaration is called blank final variable and you must need to initialize it in constructor.
<< PREVIOUS || NEXT >>

Saturday, August 22, 2015

Similarities And Difference Between Abstract Class And Interface

Earlier we have learnt about interface in THIS POST and abstract class THIS POST. Now let's try to understand similarities difference between abstract class and interface. Interviewer can ask you this question when you will go for selenium webdriver with java interview. There are few similarities and differences between interface and abstract class as bellow.
Similarities
  • Interface can not be instantiated. Same way, you can not instantiate abstract class.
  • That means you can not create object of interface or abstract class.
Difference
Differences between interface and abstract class are as bellow.

Interface
Abstract Class
We can use interface keyword to declare interface.We can use abstract keyword to declare abstract class.
Interface can hold only abstract methods(without implementation).Abstract class can hold abstract(without implementation) as well as non abstract methods.
Interface can be implemented using implements keyword.Abstract class can be extended using extends keyword.
We can achieve multiple inheritance using interface as we can implement multiple interfaces to any class.Abstract class doesn't support multiple inheritance as we can not extend more than one class.
Interface can not hold main method, static methods or constructor.Abstract class can hold main method, static methods or constructor.
Also it can hold only static and final variables and mandatory to initialize them.It can hold static, non static, final, non final variables and also it is not mandatory to initialize them.
We can achieve 100% abstraction using interface as all methods are abstract by default. It can not hold concrete methods.We can achieve partial(0% to 100%) abstraction using abstract class as it can hold abstract as well concrete methods too.
When you add a new method in existing interface it breaks all its implementation and you need to provide an implementation in all clients which is not good.By using abstract class you can provide default implementation in super class by creating concrete method. It is not required to provide its implementation in sub class.

These are the difference between abstract class and interface in java which can helps you to choose abstract class or interface.

Sunday, August 16, 2015

What Is Method Overloading In Java?

Method Overloading
Many peoples get confused between method overloading and method overriding in java. Earlier we learnt about method overriding in THIS POST. Method overloading concept is totally different than method overriding. If interviewer asks you question about method overloading then your answer should be like this "When class have two or more methods with same name but different parameters, it is called method overloading". Lets try to understand method overloading concept with example.

Method Overloading Example
We can achieve method overloading by changing number of arguments or by changing data types of arguments for same name methods in single class.

Method overloading by changing number of arguments
In bellow given example you can see that we have created two overloaded methods. 1st sum method will accept 2 arguments and 2nd sum method will accept 3 arguments to perform sum operation. This is called method overloading by changing number of arguments.

public class OverloadingByArgument {

void Sum(int i, int j) {
System.out.println(i + j);
}

void Sum(int i, int j, int k) {
System.out.println(i + j + k);
}

public static void main(String args[]) {
OverloadingByArgument ol = new OverloadingByArgument();
ol.Sum(10, 10, 10); //It will call Sum(int i, int j, int k)
ol.Sum(20, 20);//It will call Sum(int i, int j)
}
}

OutPut
30
40

Method overloading by changing data types of arguments
You can also overload method by changing data types of argument. Here number of arguments can be same or different as per requirement. In bellow given example, Class have two sum methods with same number of arguments but different datatypes. 1st sum method will be called when you wants to sum two integer values and 2nd sum method will be called when you wants to sum two double values. You can use any other datatypes too.

public class OverloadingByDataTypes {

void Sum(int i, int j) {
System.out.println(i + j);
}

void Sum(double i, double j) {
System.out.println(i + j);
}

public static void main(String args[]) {
OverloadingByDataTypes ol = new OverloadingByDataTypes();
ol.Sum(10.75, 10.5); //It will call Sum(double i, double j)
ol.Sum(20, 20);//It will call Sum(int i, int j)
}
}

OutPut
21.25
40


Advantage of Method Overloading
Using method overloading, We can create multiple methods with same name in same class to perform same action in different ways (using different number of arguments and it's datatypes). It will increases the readability of the program. We can overload static methods too. view more java tutorials on THIS PAGE.

Sunday, August 9, 2015

What Is An Abstract Class In Java?

What Is An Abstract Class?
If you will go to attend an interview for selenium WebDriver with java , 90% Interviewer will ask you this question. Your answer should be like this : An abstract class is a class that is declared with abstract keyword. If class contains any abstract method then you must have to declare your class as abstract class.  Also abstract classes can be subclassed, but they cannot be instantiated. That means you can not create object of abstract class. It may or may not have abstract methods. it can have concrete methods too so that it does not provide 100% abstraction.

Earlier we learnt interface in THIS POST. It is 100% abstraction. View THIS POST to know difference between abstract class and interface.

Bellow given class has abstract method so it is declared as abstract class.

//abstract class
public abstract class Animal {

//concrete method
public void eat(String food) {
// do something with food....
}

// abstract method
public abstract void makeNoise();
}

Let's try to understand abstract class and abstract methods in detail.

What Is Abstraction in Java?
Abstraction is process of hiding implementation details from user. User can use only functionality but can not see how it is implemented. Example : Calling a friend is best example of abstraction. Here you can talk with each other but you don't know about internal implementation of your phone and it's network using which you can talk. It is called abstraction.

What Is Abstract Method?
If any method is declared with abstract keyword then it is called abstract method. Abstract method can not have a body. Actual implementation of abstract method will be done by it's child class. If any class extends abstract class then that subclass must have to implement all the abstract methods declared by it's super class(abstract class).

Example of abstract class with simple and concrete methods is as bellow.
Animal.java
package abstraction;

//abstract class
public abstract class Animal {

// concrete method. Sub class can use it if needed.
public void eat(String food) {
// do something with food....
}

// concrete method. Sub class can use it if needed.
public static void sleep(int hours) {
// do something with sleep....
}

// abstract method. Sub classes must have to implement it if extend this class.
public abstract void makeNoise();
}

Bellow given classes are sub classes of  Animal class so both of them have implemented abstract method of Animal abstract class. But they do not need to implement concrete methods of super class. They can declare and implement their own concrete methods independently.

Dog.java
package abstraction;

public class Dog {

// abstract method of super class is implemented in sub class.
public void makeNoise() {
System.out.println("Bark! Bark!");
}

// Concrete method
public void payingBall() {
System.out.println("Dog is playing with ball");
}
}

Cow.java
package abstraction;

public class Cow extends Animal {

// abstract method of super class is implemented in sub class.
public void makeNoise() {
System.out.println("Moo! Moo!");
}

// Concrete method
public void milking() {
System.out.println("Cow is milking");
}
}

When And Why To Use Abstract Class?
If you see at above example, Abstract class is not only template for it's child classes but it has it's own functionality too. Like eat(String food), sleep(int hours). Both these methods are concrete but child class can use them if required. 

You can use abstract class as parent class for sub classes
  • When you expect to implement same method in all sub classes with different implementation detail.
  • When you expect to implement methods in super class(abstract class) which can be used by its sub classes if needed.
  • When you expect to implement independent method in sub class which do not have any relation or use with its super class.
Points to Remember About Abstract Class
  1. If class is declared with abstract keyword then it is called abstract class.
  2. If class has abstract method, you must have to declare class as abstract class.
  3. In abstract class, You can only declare abstract methods but you can not define abstract methods.
  4. Sub classes of abstract class must have to implement all abstract methods of super abstract class.
  5. You can not instantiate(Can not create object of) abstract class.
  6. Abstract classes can have concrete methods, constructors and Member variables too.
I think now you are good with abstract class to use it in selenium.