String - immutable
StringBuffer - mutable, thread-safe, synchronized, slower than builder
StringBuilder - mutable, non thread safe, unsynchronized, faster than buffer
Abstract class Vs Interface
Points | Abstract Class | Interface |
---|---|---|
Definition | Cannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation) | Specifies a set of methods a class must implement; methods are abstract by default. |
Implementation Method | Can have both implemented and abstract methods. | Methods are abstract by default; Java 8, can have default and static methods. |
Inheritance | class can inherit from only one abstract class. | A class can implement multiple interfaces. |
Access Modifiers | Methods and properties can have any access modifier (public, protected, private). | Methods and properties are implicitly public. |
Variables | Can have member variables (final, non-final, static, non-static). | Variables are implicitly public, static, and final (constants). |
abstract
keyword is used to create an abstract class and it can be used with methods also whereasinterface
keyword is used to create interface and it can’t be used with methods.- Subclasses use
extends
keyword to extend an abstract class and they need to provide implementation of all the declared methods in the abstract class unless the subclass is also an abstract class whereas subclasses useimplements
keyword to implement interfaces and should provide implementation for all the methods declared in the interface. - Abstract classes can have methods with implementation whereas interface provides absolute abstraction and can’t have any method implementations. Note that from Java 8 onwards, we can create default and static methods in interface that contains the method implementations.
- Abstract classes can have constructors but interfaces can’t have constructors.
- Abstract class have all the features of a normal java class except that we can’t instantiate it. We can use
abstract
keyword to make a class abstract but interfaces are a completely different type and can have only public static final constants and method declarations. - Abstract classes methods can have access modifiers as public, private, protected, static but interface methods are implicitly public and abstract, we can’t use any other access modifiers with interface methods.
- A subclass can extend only one abstract class but it can implement multiple interfaces.
- Abstract classes can extend other class and implement interfaces but interface can only extend other interfaces.
- We can run an abstract class if it has
main()
method but we can’t run an interface because they can’t have main method implementation. - Interfaces are used to define contract for the subclasses whereas abstract class also define contract but it can provide other methods implementations for subclasses to use.
Interface or Abstract Class
Whether to choose between Interface or abstract class for providing a contract for subclasses is a design decision and depends on many factors. Let’s see when Interfaces are the best choice and when can we use abstract classes.
- Java doesn’t support multiple class level inheritance, so every class can extend only one superclass. But a class can implement multiple interfaces. So most of the times Interfaces are a good choice for providing the base for class hierarchy and contract. Also coding in terms of interfaces is one of the best practices for coding in java.
- If there are a lot of methods in the contract, then abstract class is more useful because we can provide a default implementation for some of the methods that are common for all the subclasses. Also if subclasses don’t need to implement a particular method, they can avoid providing the implementation but in case of interface, the subclass will have to provide the implementation for all the methods even though it’s of no use and implementation is just empty block.
- If our base contract keeps on changing then interfaces can cause issues because we can’t declare additional methods to the interface without changing all the implementation classes, with the abstract class we can provide the default implementation and only change the implementation classes that are actually going to use the new methods.
Use Abstract classes and Interface both
Using interfaces and abstract classes together is the best approach to design a system. For example, in JDK java.util.List
is an interface that contains a lot of methods, so there is an abstract class java.util.AbstractList
that provides a skeletal implementation for all the methods of List
interface so that any subclass can extend this class and implement only
required methods. We should always start with an interface as the base
and define methods that every subclass should implement and then if
there are some methods that only certain subclass should implement, we
can extend the base interface and create a new interface with those
methods. The subclasses will have the option to chose between the base
interface or the child interface to implement according to its
requirements. If the number of methods grows a lot, it’s not a bad idea
to provide a skeletal abstract class implementing the child interface
and providing flexibility to the subclasses to chose between interface
and an abstract class.
Java 8 interface changes
From Java 8 onwards, we can have method implementations in the interfaces. We can create default as well as static methods in the interfaces and provide an implementation for them. This has bridged the gap between abstract classes and interfaces and now interfaces are the way to go because we can extend it further by providing default implementations for new methods.
Checked Exceptions (Compile time exceptions)
Java
IOException
ClassNotFoundException
FileNotFoundException
InterruptedException
Selenium
ElementNotVisibleException
NoSuchElementException
NoSuchFrameException
NoSuchWindowException
NoAlertPresentException
SessionNotFoundException
StaleElementReferenceException
Unchecked Exceptions (Run time exceptions)
Java
ArithmeticException
NullPointerException - when trying to use a variable or method that doesn't point to a object
ArrayIndexOutOfBoundException
Selenium
overloading and overriding examples
Framework (hybrid with POM pattern)
org.openqa.selenium.By
com.pearson.adam.pages
--Loginpage.java
--Homepage.java
--DashboardPage.java
--StudentSearchPage.java
--StudentDetailsPage.java
com.pearson.adam.testscripts
--Login_TC1.java
--StudentSearch_TC2.java
TestData
TestReports
pom.xml
TestNG.xml
equals(string) vs == (numeric)
dependency for reports - sure-fire
extent reports
jasper reports
collection - interface
collections - class
Arrays.sort
Collections.sort
StringBuilder sb = new StringBuilder()
sb.reverse()
can we write two classes in same file
get, post methods blocked in headless browser
inner classs and outer class
If a method A is calling another method B which is having
IOException, then method A must handle IOException
or declare that it throws IOException
catching order of remote exception, io exception, exception
storage of java variabless in heap, stack
if the button is not identified, use mouse movements
movebyoffset(x-offset, y-offset) and context click
class rectangle extends quadrilateral implements shape
default access specifier for interface - public
can we create object for interface - no
***in anonymous class we can create inerface object by mentioning
all methods within the curly braces. Need to avoid anonymous classes
due to repetition of code
instanceof - boolean method
a class or an interface can extend multiple inerfaces(interface inherit another interface) - to build more specific interface
can an interface extends abstract(class)? - No
can a class extend an interface? - No
Can an interface implement another interface - No
don't modify variables and methods in interface as it impacts all implemented classes
a class should implement all the methods of a interface
but abstract class can implment only some of the methods
in interface (partial implementation of interface)
difference between local variables and member variables
access specifier Vs access modifiers
access specifiers - public, private, protected
access modifiers - abstract (Ex - public abstract cars)
Usage of abstract classes over concrete classes
- if we define parent class as abstract class user is restricted to create object of parent class.
if it is concrete class, user can create object but can't access child class members
usage of abstract methods over concrete methods - we can override abstract methods in inherited classes
abstract methods can't be static as we can't overrider in extended classes
all collections(list, dictionary, hash table, map..) are derived from an Interface, IEnumerable.
WebDriver(interface) implemented by RemoteWebDriver(Class)
**diamond problem - A inherits B and C, B and C together inherits D
---------------------------------------------------------------------
Java 1.8, Funcational interfaces, lambda expressions, interfaces with static members
Lambda expression - an instance of a functinal interface(interface containing only one abstract method and any no. of default methods)
interface FuncInterface
{
void abstractFunc(int x);
default void normalFunc()
{
System.out.println("Hello");
}
}
class Test
{
public static void main(String[] args)
{
FuncInterface fobj = (int x)->system.out.println(3*x);
fobj.abstractFunc(5);
}
}
zero parameter:
() -> system.out.println("zero parameter lambda");
one parameter:
(p) -> system.out.println("one parameter:" +p)
multiple parameters:
(p1,p2) -> system.out.println("multiple parameters:"+p1+", "+p2")