Java Flashcards
What is the difference between an interface and an abstract class?
Interface
- Interface can have only abstract, default and static methods
- The interface keyword is used to declare an interface
- The implement keyword is used to make a class abide by the methods defined by the interface
- Interface supports multiple inheritance
- Interface can’t provide the implementation of abstract class
- Interface has only static and final variables
- All method declarations are implicitly public
- You can implement any number of interfaces
Abstract
- Abstract class can have abstract and non-abstract methods
- Abstract class doesn’t support multiple inheritance
- You can declare fields that have final, non-final, static and non-static variables
- Abstract class can provide the implementation of methods, have concrete methods.
- The abstract keyword is used to declare abstract class
- All fields are automatically public, static, and final
- All methods that you declare or define are public
- You can extend only one class, whether or not it is abstract
Both
- Can not be instantiated
- Can be subclassed
- If the subclass does not provide the implementation for the parent class abstract methods, the subclass must be declared abstract.
What are abstract methods?
An abstract method is a method that is declared without an implementation.
What are default methods?
Default methods enable you to add new functionality to the interfaces by specifying the method definition and using the default keyword as the method signature
What is an interface class?
Interface is a reference type and is a contract that binds specific method signatures and fields to the class that implements it
What is an abstract class?
An abstract class is a class that is declared abstract. It may or may not include abstract methods.
What is the static keyword?
Variables that are declared static is called a class variable that is initialized one and all instances share the same copy of the variable. It can be accessed directly without creating an instance of the class. It can be accessed directly by the class name and doesn’t need any object.
What is an instance variable
A variable without the static keyword and each instance of the class has its own copy of the variable.
What is the difference between an ArrayList and a Vector?
Vector is synchronized, which means only one thread is working and no other thread can perform an operation at a time.
Arraylist is not synchronized, so multiple threads can work on ArrayList at the same time.
Both ArrayList and a Vector can grow and shrink dynamically, but the way they resize is different. Vector double itself by ArrayList grow by half of its size.
ArrayList is faster since it is non-synchronized and Vectors are slower because it is thread-safe since the Vector gets locked until the work is complete by the thread.
An ArrayList is fail-fast since it is not thread safe unlike a Vector
What is a hashcode?
A number generated from an object. It is used to store and retrieve objects quickly in a hashtable.
What type of Collections that does not allow duplicates?
Set
A set is a Collection that cannot contain duplicate elements.
What is the difference between HashTable and HashMap?
HashMap
- It is not synchronized, so it is not thread safe
- HashMap allows one null key and any number of null values
- HashMap object values are iterated by using iterator
- Iterator is fail-fast
- HashMap is much faster and uses less memory than HashTable since it is not synchronized
HashTable
- It is synchronized, so it is thread safe
- HashTable do not allow null keys and null values
- HashTable is the only class other than Vector which uses enumerator to iterate the object values
- Not fail-fast
- Slower performance since it is synchronized
- Legacy
What is JDBC?
JDBC stands for Java Database Connectivity, which is a standard Java API for database-independent connectivity between the Java programming language and the database.
- Can make connection to a database
- Can create SQL statements
- Can execute SQL queries in the database
- Can view and modify records
- Can write Java Applications
- Can write Java Servlets
- Can write Java ServerPages (JSP)
How do you establish a connection for JDBC?
DriverManager
.getConnection(“jdbc:mysql://localhost:3306/test”, “username”,”password”)
test is a database schema
What is JSP?
JavaServer Pages
It is a server-side programming technology that enables the creation of dynamic, platform-independent method for building Web-based applications.
What is the difference between Statement, PreparedStatement, CallableStatement?
Statement
- It is used to execute normal SQL queries
- It is preferred when a particular SQL query is to be executed only once
- You cannot pass the parameters to SQL query using this interface
- This interface is mainly used for DDL statements like CREATE, ALTER, DROP
- The performance of this interface is very low
PreparedStatement
- It is used to execute parameterized or dynamic SQL queries
- It is preferred when a particular query is to be executed multiple times
- You can pass the parameters to SQL query at run time using this interface, making it more readable
- It is used for any kind of SQL queries which are to be executed multiple times
- The performance of this interface is better than the Statement interface (when used for multiple execution of same query)
- It is precompiled by the JVM so the database doesn’t have to compile the SQL each and every time it is executed
- It will properly escape reserved characters to prevent SQL injection attacks
CallableStatement
- It is used to call the stored procedures
- It is preferred when the stored procedures are to be executed
- You can pass 3 types of parameters using this interface (In, Out, In Out)
- It is used to execute stored procedures and functions
- The performance of this interface is high