Java Flashcards
Difference between JVM, JRE, JDK ?
JVM stands for Java Virtual machine. It translates and executes the Java bytecode. It’s the entity which transforms Java to become a “portable language” (i.e., write once, run anywhere). Java compiler generates bytecode for all the Java code and converts into class files.
JRE stands for Java Runtime Environment which we usually download as Java software. The JRE consists of the Java Virtual Machine, Java platform classes, and supporting libraries. The JRE is the runtime component of Java software and is all we need to run any Java application.
JDK stands for Java Development Kit is a superset of the JRE and includes everything that the JRE contains. Additionally, it comes with the compilers and debuggers tools required for developing Java applications.
- What is a method? What is the main method? Why do we need one in java? Do we have to have a main method in java?
Method is a collection of statements that are grouped together to perform an operation.
A method can be called (invoked) at any point in a program by the method’s name.
Main method is the starting point of an application. JVM starts execution by invoking the main method of some specified class, passing it a single argument, which is an array of strings. Whenever we execute a program, the main() is the first function to be executed. We can call other functions from the main to execute them. It is not mandatory to have the main method in java, without main() our Java code will compile but won’t run.
- Explain public static void main (String args[])?
public: it is an access modifier that means it will be accessible by any Class.
static: is a keyword to call this method directly using class name without creating an object of it.
void: it is a return type i.e. it does not return any value.
main(): it is the name of the method which is searched by JVM as a starting point for an application with a particular signature only. It is the method where the main executions occur.
string args[]: it’s a command line argument passed to the main method.
- What are Access Modifiers? How did you use them?
Java provides access modifiers to set access levels for classes, variables, methods and constructors.
public: A class or interface may be accessed from outside the package. Constructors, inner classes, methods and field variables may be accessed wherever their class is accessed.
protected: Accessed by other classes in the same package or any subclasses of the same
package or different package.
default: When no access modifier is specified for a class , method or data member – It is
said to be having the default access modifier by default.
private: Accessed only within the class in which they are declared.
In our framework we follow page object model design pattern, in page classes we store WebElements as public to give visibility to our test classes in different package
Note: Access modifier cannot be applied to local variables.
- What is an instance variable and how do you use it? What is the difference between local and instance variables?
Variables which are declared inside a method or constructor, or blocks are called local variables. Local variables are created when a method is called and destroyed when the method exits.
Variables which are declared inside the class, but outside a method, constructor or any block are called instance variables. We can access instance variables by creating an Object of the class they belong to. Instance variables are created when an object is created with the use of the keyword ‘new’ and destroyed when the object is destroyed.
class HealthyDog {
// when the details are different from object to object
// store that info in instance variable
String name; // instance
String color; // instance
void eat() { int noOfTimes = 3; // local System.out.println(name + " eats " + noOfTimes + " times per day"); }
- How can we access variables without creating an object instance of it? Difference between Instance Variable and static Variable? What is a static keyword in java? Where did you use static in your framework?
- Static variables are declared with the static keyword in a class, but outside a method, constructor or a block. By declaring a variable as a static, we can access it from different classes without creating an Object - those variables called class variables and also known as static variables.
Whereas, Instance variables are declared in a class, but outside a method, constructor or any block. To access instance variables, we need to create an object of the Class they belong to.
2. Class variables only have one copy that is shared by all the different objects of a class, whereas every object has its own personal copy of an instance variable. So, instance variables across different objects can have different values and when we make changes to the instance variable, they don't reflect in other instances of that class whereas class variables across different objects can have only one value.
3. Static variables are created when the program starts and when the program stops whereas instance variables need an object created with the use of the keyword 'new' and destroyed when the object is destroyed. Static keyword in java: ● Static keyword means that the variable or method belongs to the class and shared between all instances. ● Using static keyword, we can access class variables and methods without object reference ● Static methods cannot call/refer non-static members Usage of static keyword in framework: In our utility package we have a class where we store common methods, such as wait, switch between frames, clicking on buttons, selecting values from drop down. So those methods are written using static keyword and we can easily access them in our program.
public static void click(WebElement element) {
waitForClickability(element);
element.click();
}
In our Constants Class we have static variables
public static final String CONFIGURATION_FILEPATH =
System.getProperty(“user.dir”)+”/src/test/resources/config/config.properties”;
public static final String TESTDATA_FILEPATH =
System.getProperty(“user.dir”)+”/src/test/resources/testdata/TestDataBatch11H
rms.xlsx”;
- What is a constructor? Use of constructor in class? Can you make the constructor static? What is the difference between constructor and method? Can we overload a constructor?
A constructor in Java is a block of code similar to a method. Constructor is called when an instance of a class is created. A constructor is a “special method” whose task is to initialize the object of its class.
Constructors cannot be abstract, final, static.
Rules to create constructor:
- Constructor name class name must be the same.
- Constructor do not have any return type.
- Constructor may or may not have parameters.
Usage of Constructor ● The primary use of constructor is to initialize the instance variables. ● Constructors are special functions which are called automatically when we create objects of the class. So, once we create the object of the class all the variables get initialized, and we don't need to write extra code for initialization of variables. ● Constructor is the property of an object while static has nothing to do with the object. That's why there is nothing like a static constructor. But we have a static block to do a similar task as constructor i.e., initialization of static fields etc.
Difference between Constructor and Method ● Constructor must not have a return type whereas methods must have a return type. ● Constructor name is the same as the class name where the method may or may not be the same class name. ● Constructor will be called automatically whenever an object is created whereas the method invokes explicitly. ● Compiler provides a default constructor when no constructor is created whereas the compiler doesn’t create any methods.
WE CAN OVERLOAD CONSTRUCTOR (using different number or type of parameters)
Example of constructor from framework creating constructor to initialize instance variables public class AddEmployeePage { @FindBy(id="firstName") public WebElement firstName; public AddEmployeePage() { PageFactory.initElements(CommonMethods.driver, this); } }
- Super vs super()? this vs this()? Can super() and this() keywords be in the same constructor?
this vs this() ● this keyword is used to refer to the current object and differentiate between local and instance variables public class ThisKeyword { String name; int age; ThisKeyword(String name, int age){ this.name=name; this.age=age; } } ● this() is used to access one constructor from another where both constructors belong to the same class. public class ThisKeyword4 { int z; ThisKeyword4() { System.out.println("This a default constructor"); } ThisKeyword4(String a) { this(); System.out.println("Parameterized constructor); } } super vs super() Both are used in a subclass as a way to invoke or refer to its superclass. ● super keyword is used to call super class (parent class/ base class) variables and methods by the subclass object when they are overridden by subclasses. ● super() is used to call a superclass constructor from a subclass constructor. public class SuperKeyword1 extends SuperKeyword{ SuperKeyword1(){ super(4); System.out.println("This is a child default constructor"); } We can use super() and this() only in the constructor, not anywhere else, any attempt to do so will lead to a compile-time error. This() and super() always have to be in the first line within the constructor and for that reason we CANNOT use them within the same constructor. We have to keep either super() or this() as the first line of the constructor but NOT both simultaneously.
17.Difference between an abstract class and interface? Can we create an object for an abstract class? interface? When to use abstract class vs interface in Java?
Interface is a blueprint for your class that can be used to implement a class. Interface is a collection of public static methods and public static final variables
An abstract class is a class that is declared with abstract keyword and can contain defined(concrete) and undefined(abstract) methods.
We cannot create an object of interface or an abstract class!
●An abstract class is good if you think you will plan on using inheritance since it provides a common base class implementation to derived classes. ●An abstract class is also good if you want to be able to declare non-public members. In an interface, all methods must be public. ●If you think you will need to add methods in the future, then an abstract class is a better choice. Because if you add new method headings to an interface, then all of the classes that already implement that interface will have to be changed to implement the new methods. That can be quite a hassle.
Practical Example of an Interface:
Basic statement we all know in Selenium is
WebDriver driver = new FirefoxDriver();
WebDriver itself is an Interface. We are initializing Firefox browser using Selenium WebDriver. It means we are creating a reference variable (driver) of the interface (WebDriver) and creating an Object. Here WebDriver is an Interface and FirefoxDriver is a class.
18.Explain OOPS concepts? Is java 100% object oriented?
OOP concepts in Java are the main idea behind Java’s Object Oriented Programming. OOP mainly focuses on the objects that are required to be manipulated instead of logic
4 main OOPS concepts: inheritance, polymorphism, abstraction and encapsulation. Inheritance is a mechanism in which one object acquires all the properties and behaviors of a parent object. It provides code reusability.
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in Java is when a parent class reference type of variable is used to refer to a child class object.
Abstraction is a process of hiding the implementation of internal details and showing the functionality to the users. Abstraction lets you focus on what the object does instead of how it does it.
Encapsulation is a mechanism of binding code and data together in a single unit. We can hide direct access to data by using a private key and we can access private data by using getter and setter methods.
No, Java is not 100% object oriented, since it has primitive data types, which are different from objects.
19.What is inheritance and benefits of it? Types of inheritance? How do you use it in your code?
Inheritance
●The process of acquiring properties (variables) & methods (behaviors) from one class to another class is called inheritance. ●We are achieving the inheritance concept by using extends keyword. Also known as is-a relationship. ●Extends keyword is providing a relationship between two classes. ●The main objective of inheritance is code extensibility whenever we are extending the class automatically code is reused.
Types of Inheritance:
●Single Inheritance - single base class and single derived class. ●Hierarchical Inheritance - when a class has more than one child classes (sub classes) ●Multilevel Inheritance - single base class, single derived class and multiple intermediate base classes. ●Multiple Inheritance - multiple classes and single derived class (Possible through interface only) ●Hybrid Inheritance - combination of both Single and Multiple Inheritance (Possible through interface only)
Usage of inheritance in real time project:
In our current Cucumber framework we have BaseClass where we initialize the WebDriver interface. And after we extend the Base Class in other classes such as PageInitializer and to the Common methods where we have functions to work with Web Browser.
20.What is polymorphism? Types of polymorphism?
Polymorphism is the ability of an object to take on many forms. Polymorphism allows us to perform a task in multiple ways.
Combination of overloading and overriding is known as Polymorphism.
There are two types of Polymorphism in Java
- Compile time polymorphism (Static binding) – Method overloading
- Runtime polymorphism (Dynamic binding) – Method overriding
21.Method overloading & overriding? How do you use it in your framework? Any example or practical usage of Run time polymorphism?
Method overloading in Java occurs when two or more methods in the same class have the exact same name but different parameters (remember that method parameters accept values passed into the method).
Overloading: Same method name with different arguments in the same class
Method overriding: Declaring a method in child class which is already present in the parent class is called Method Overriding.In simple words, overriding means to override the functionality of an existing method.
With method overriding a child class can give its own specific implementation to an inherited method without modifying the parent class method. Assume we have multiple child classes. In case one of the child classes wants to use the parent class method and the other class wants to use their own implementation then we can use overriding features.
Practical Usage:
1.Implementation of WebDriver interface.
WebDriver driver = new FirefoxDriver(); WebDriver driver = new ChromeDriver();
- Selenium WebDriver provides an interface WebDriver, which consists of abstract methods getDriver() and closeDriver(). So any implemented class with respect to the browser can override those methods as per their functionality, like ChromeDriver implements the WebDriver and can override the getDriver() and closeDriver().
22.Can we override/overload the main method? Explain the reason? Can you override the static method? Can we overload and override private methods?
We cannot override a static method, so we cannot override the main method. However, you can overload the main method in Java. But the program doesn’t execute the
overloaded main method when you run your program; you have to call the overloaded main method from the actual main method.
Practically I do not see any use of it and we don’t use it in my framework.
Static methods are bound with class it is not possible to override static methods.
In java not possible to override private methods because these methods are specific to classes, not visible in child classes.
23.Can we achieve 100% abstraction in JAVA?
In JAVA abstraction can be achieved with the help of Abstract Classes and Interfaces. Using abstract class we can achieve 0 to 100% or partial abstraction.
Using interfaces we can achieve 100% or full abstraction.