5. Access Modifier
In Java, there 4 types of the access specifiers.
public: accessible in all class in your application.
protected: accessible within the package in which it is defined and in
its subclass(es)(including subclasses declared outside the package)
private: accessible only within the class in which it is defined.
default (declared/defined without using any modifier): accessible within same
class and package within which its class is defined.
Data Types
Java has two categories of data:
Primitive Data Type: such as boolean, char, int, short, byte, long, float, and double
Non-Primitive Data Type or Object Data type: such as String, Array, etc.
7. Class :
Collection of objects is called class. It is a logical entity.
Object : Any entity that has state and behavior is known as an object. For example, a
chair, pen, table, keyboard, bike, etc. It can be physical or logical.
Inheritance :
When one object acquires all the properties and behaviors of a parent object, it is
known as inheritance. It provides code reusability. It is used to achieve runtime
polymorphism.
8. Polymorphism:
If one task is performed in different ways, it is known as polymorphism. For
example: to convince the customer differently, to draw something, for example,
shape, triangle, rectangle, etc.
In Java, we use method overloading and method overriding to achieve polymorphism.
Abstraction :
Hiding internal details and showing functionality is known as abstraction. For
example phone call, we don't know the internal processing.
9. Encapsulation :
Binding (or wrapping) code and data together into a single unit are known as
encapsulation. For example, a capsule, it is wrapped with different medicines.
What is String in Java?
Generally, String is a sequence of characters. But in Java, string is an object that
represents a sequence of characters. The java.lang.String class is used to create a
string object.
10. How to create a string object?
There are two ways to create String object:
1.By string literal
2.By new keyword
Why String objects are immutable?
Java uses the concept of String literal. Suppose there are 5 reference variables, all
refer to one object "abc". If one reference variable changes the value of the object, it
will be affected by all the reference variables. That is why String objects are
immutable in Java
12. 1) Checked Exception
The classes that directly inherit the Throwable class except
RuntimeException and Error are known as checked exceptions. For
example, IOException, SQLException, etc. Checked exceptions are
checked at compile-time.
2) Unchecked Exception
The classes that inherit the RuntimeException are known as unchecked
exceptions. For example, ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException, etc. Unchecked exceptions are not
checked at compile-time, but they are checked at runtime.
3) Error
Error is irrecoverable. Some example of errors are
13. Throw Vs Throws
The throw and throws are the concepts of exception handling in Java where the throw
keyword throws the exception explicitly from a method or a block of code, whereas the
throws keyword is used in the signature of the method.
15. Collections
A Java Collection is a predefined architecture capable of storing a group of elements
and behaving like a single unit such as an object or a group.
It’s for all the data manipulation jobs such as storing data, searching, sorting,
insertion, deletion, and updating of data
16. Comparable Vs Comparator
Java provides two interfaces to sort objects using data members of the class:
1.Comparable
2.Comparator
17. Multithreading
Multithreading is a Java feature that allows concurrent execution of two or more parts
of a program for maximum utilization of CPU. Each part of such program is called a
thread. So, threads are light-weight processes within a process.
Threads can be created by using two mechanisms :
1.Extending the Thread class
2.Implementing the Runnable Interface
18. Life Cycle and States of Thread
A thread in Java at any point of time
exists in any one of the following states.
A thread lies only in one of the shown
states at any instant:
1.New
2.Runnable
3.Blocked
4.Waiting
5.Timed Waiting
6.Terminated
19. Synchronization
Synchronization in java is the capability to control the access of multiple threads to
any shared resource. In the Multithreading concept, multiple threads try to access the
shared resources at a time to produce inconsistent results. The synchronization is
necessary for reliable communication between threads.
Why use Synchronization
Synchronization helps in preventing thread interference.
Synchronization helps to prevent concurrency problems.
20. Design Patterns in Java
A design patterns are well-proved solution for solving the specific problem/task.
Problem Given :
Suppose you want to create a class for which only a single instance (or object) should
be created and that single object can be used by all other classes.
Solution:
Singleton design pattern is the best solution of above specific problem. So, every
design pattern has some specification or set of rules for solving the problems.
23. Default Methods
Before Java 8, interfaces could have only abstract methods. The implementation of
these methods has to be provided in a separate class. So, if a new method is to be
added in an interface, then its implementation code has to be provided in the class
implementing the same interface. To overcome this issue, Java 8 has introduced the
concept of default methods which allow the interfaces to have methods with
implementation without affecting the classes that implement the interface.
Functional Interface :
A functional interface is an interface that contains only one abstract method. They can
have only one functionality to exhibit. From Java 8 onwards, lambda expressions can
be used to represent the instance of a functional interface. A functional interface can
have any number of default methods. Runnable, ActionListener, Comparable are
some of the examples of functional interfaces.
24. Lambda Expression :
The Lambda expression is used to provide the implementation of an interface which
has functional interface. It saves a lot of code. In case of lambda expression, we don't
need to define the method again for providing the implementation.
Streams :
The Stream API is used to process collections of objects. A stream is a sequence of
objects that supports various methods which can be pipelined to produce the desired
result.
25. Features of Stream :
Stream does not store elements. It simply conveys elements from a source such as
a data structure, an array, or an I/O channel, through a pipeline of computational
operations.
Stream is functional in nature. Operations performed on a stream does not modify
it's source. For example, filtering a Stream obtained from a collection produces a
new Stream without the filtered elements, rather than removing elements from the
source collection.
Stream is lazy and evaluates code only when required.
The elements of a stream are only visited once during the life of a stream. Like an
Iterator, a new stream must be generated to revisit the same elements of the
source.