Java Flashcards
Versions of java you worked with? What version of java do you currently use in your framework? Difference between JRE, JDK ?
Currently I am using Java 1.8 version, JRE, or Java Runtime Environment, is the essential component for running Java applications, comprising the Java Virtual Machine and supporting libraries. JDK, or Java Development Kit, is a superset of JRE and includes development tools like compilers and debuggers. JVM, or Java Virtual Machine, translates and executes Java bytecode, enabling the portability of Java code. The bytecode is generated by the Java compiler and transformed into class files.
The difference between == and =
“=” is used to assign values, and “==” is a comparison operator used for equality checks in programming languages. It checks if two values are equal
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?
A method in Java is a set of instructions within a class that performs a specific task. The main
method is a special method serving as the entry point for Java program execution, essential for the Java Virtual Machine (JVM) to start running the code. Having a main
method is necessary in Java for creating an executable program; without it, the JVM doesn’t know where to begin execution.
Explain public static void main (String args[])?
public static void main(String args[]), shows the standard signature for the main method in Java, which is required for the JVM to recognize and initiate the execution of our program. public: it is an access specified which means it will be accessible by any Class. static: is a keyword to call this method directly using a 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 that 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 (Private, public, protected)? How did you use them?
Java provides access modifiers to set access levels for classes, variables, methods and constructors. public
: Anyone, from anywhere, can access a class, constructor, inner class, method, or field variable marked as public. - protected
: Classes in the same package or subclasses, even if in different packages, can access items marked as protected. - private
: Only the class in which they are declared can access items marked as private. - Default (no modifier specified): When no access modifier is specified, it is said to have default access, which means it’s accessible only within the same package. In simpler terms, public is for everyone, protected is for certain friends (same package or subclasses), private is for yourself (only within the class), and default is for neighbors (within the same package). In our framework we follow the page object model design pattern, in page classes we store WebElements as public to give visibility to our test classes in a different package, Also in our framework, we build utility classes where we store methods to work with web elements, property files, and excel files and give public accessibility to those methods.
What is an instance variable and how do you use it? What is the difference between local and instance variables?
local variables is variables inside a method, constructor, or blocks, made when the method is used and gone when it’s done. Instance variables, declared in a class but not inside a method, are used by creating an object of the class. They are created when we make an object with ‘new’ and disappear when the object is destroyed. an example from a framework could be the use of an instance variable like @FindBy(xpath=”//img[contains(@src, ‘logo’)]”) public WebElement logo;
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, declared with the static keyword outside methods, can be accessed across different classes without creating an object. On the other hand, instance variables, declared in a class, require creating an object to access them. Class variables (static) have a single shared copy among all objects of a class, while every object has its own copy of an instance variable. Changes to instance variables don’t reflect in other instances, but class variables have only one value across different objects. Static variables exist from program start to stop, whereas instance variables are created when an object is made with ‘new’ and disappear when the object is gone. The static keyword in Java signifies that the variable or method belongs to the class, shared among all instances. It allows accessing class variables and methods without an object reference. Static methods cannot call non-static members. In my framework, the static keyword is useful for common utility methods stored in a class, making them easily accessible without object creation. Example methods include waiting, frame switching, button clicking, and dropdown value selection. Static variables are also used in base classes and configuration readers.
Super vs super()? this vs this()? Can super() and this() keywords be in the same constructor?
super
is used to refer to the immediate parent class object, while super()
calls the parent class constructor;this
refers to the current instance of the class, and this()
calls the current class constructor, and though both super()
and this()
can be in the same constructor, but only one of them can be utilized as the first statement.
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 is a special method in a class that is automatically called when we create an object. It has the same name as the class and is used to set up the initial values of the object. Constructors don’t have a return type, and they ensure that the object starts with the right information. Unlike regular methods, constructors can’t be declared as static because their main job is to create instances of the class. Methods, on the other hand, are called explicitly in our code and perform actions or operations, we can have multiple versions of a constructor in the same class, a concept known as constructor overloading. This allows us to create objects in different ways, providing flexibility in how we initialize them. So constructors initialize object values and are automatically called when an object is created. They are distinct from methods, which are explicitly called and perform actions, and constructors can be overloaded for different ways of creating objects.
Difference between an abstract class and interface? Can we create an object for an abstract class? Interface? When to use abstract classes and interfaces in Java?
An interface is like a plan that tells classes what methods to have, and it also includes certain constant values. On the other hand, an abstract class, marked with the “abstract” keyword, can have both must-have methods and some that can be defined later. interface is only lists methods and constants that must be followed. But an abstract class is more flexible; it can have some methods already decided and others left for later. Interfaces can be thought of as a set of rules that multiple classes can follow at the same time, while abstract classes are like a starting point that other classes can build upon. In practical terms, when working with Selenium, WebDriver is like the rulebook, and FirefoxDriver is a class that follows those rules. It’s like saying, Here’s how a web driver should behave, and Firefox, you’re one way to do it. So, interfaces are strict rules, and abstract classes are a bit like a starting point for classes to follow rules and build upon.
Explain OOPS concepts? Is java 100% object oriented?
No, Java is not 100% object-oriented, since it has primitive data types, which are different from objects. Java’s Object-Oriented Programming (OOP) revolves around manipulating objects with a focus on four key concepts: inheritance for code reusability, polymorphism allowing objects to take on various forms, abstraction hiding implementation details and emphasizing functionality, and encapsulation binding code and data for security using private access and getter/setter methods.
What is inheritance and benefits of it? Types of inheritance? How do you use it in your code?
In programming, inheritance lets a new class inherit features from an existing class, helping code reuse, customization through method changes, and starting a hierarchy for well-organized and easily maintainable code. Types of Inheritance: Single Inheritance - single base class and single derived class. Hierarchical Inheritance - when a class has more than one child classes (subclasses) 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). In our current Cucumber framework, we have BaseClass where we initialize the WebDriver interface. 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.
What is polymorphism? Types of polymorphism?
Polymorphism is the ability of an object on many forms. Polymorphism allows us to perform a task in multiple ways. A combination of overloading and overriding is known as Polymorphism. There are two types of Polymorphism in Java 1. Compile time polymorphism (Static binding) – Method overloading 2. Runtime polymorphism (Dynamic binding) – Method overriding
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, 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.
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. 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.