You are on page 1of 127

Final exam questions

1. What is Java?
2. What are the differences between C++ and Java?
3. List the features of Java Programming language.
4. What do you understand about Java virtual machine?
5. What is the difference between JDK, JRE, and JVM?
6. What are the various access specifiers in Java?
7. What is the purpose of static methods and variables?
8. What are the advantages of Packages in Java?
9. What is the output of the following Java program?
class Test
{
public static void main (String args[])
{
System.out.println(10 + 20 + "Javatpoint");
System.out.println("Javatpoint" + 10 + 20);
}
}

10. What is the output of the following Java program?


class Test
{
public static void main (String args[])
{
System.out.println(10 * 20 + "Javatpoint");
System.out.println("Javatpoint" + 10 * 20);
}
}

11. What is the output of the following Java program?


class Test
{
public static void main (String args[])
{
for(int i=0; 0; i++)
{
System.out.println("Hello Javatpoint");
}
}
}

12. What is object-oriented paradigm?


13. What is an object?
14. What is the constructor?
15. How many types of constructors are used in Java?
16. What is the purpose of a default constructor?
17. Does the constructor return any value? And why?
18. Can we overload the constructors?
19. What are the differences between the constructors and methods?
20. What is the output of the following Java program?
public class Test
{
Test(int a, int b)
{
System.out.println("a = "+a+" b = "+b);
}
Test(int a, float b)
{
System.out.println("a = "+a+" b = "+b);
}
public static void main (String args[])
{
byte a = 10;
byte b = 15;
Test test = new Test(a,b);
}
}

21. What is the output of the following Java program?


class Test
{
int i;
}
public class Main
{
public static void main (String args[])
{
Test test = new Test();
System.out.println(test.i);
}
}

22. What is the output of the following Java program?


class Test
{
int test_a, test_b;
Test(int a, int b)
{
test_a = a;
test_b = b;
}
public static void main (String args[])
{
Test test = new Test();
System.out.println(test.test_a+" "+test.test_b);
}
}

23. What is the static variable?


24. What is the static method?
25. What are the restrictions that are applied to the Java static methods?
26. Why is the main method static?
27. What is this keyword in java?
28. What are the main uses of this keyword?
29. What is Inheritance?
30. Why is Inheritance used in Java?
31. Why is multiple inheritance not supported in java? How to use
multiple inheritance principle in java?
32. What is aggregation?
33. What is composition?
34. What is the difference between aggregation and composition?
35. What is super in java?
36. How can constructor chaining be done by using the super keyword?
37. What are the main uses of the super keyword?
38. What are the differences between this and super keyword?
39. What is the output of the following Java program?
class Person
{
public Person()
{
System.out.println("Person class constructor called");
}
}
public class Employee extends Person
{
public Employee()
{
System.out.println("Employee class constructor called");
}
public static void main (String args[])
{
Employee e = new Employee();
}
}

40. Can you use this() and super() both in a constructor?


No, because this() and super() must be the first statement in the class constructor.
Example:
public class Test{
Test()
{
super();
this();
System.out.println("Test class object is created");
}
public static void main(String []args){
Test t = new Test();
}
}

41. What is method overloading?


42. Why is method overloading not possible by changing the return type
in java?
43. What is method overriding:

If subclass (child class) has the same method as declared in the parent class, it is known as method
overriding in Java.

In other words, If a subclass provides the specific implementation of the method that has been declared by
one of its parent class, it is known as method overriding.
Usage of Java Method Overriding
o Method overriding is used to provide the specific implementation of a method which is already
provided by its superclass.
o Method overriding is used for runtime polymorphism

Rules for Java Method Overriding


1. The method must have the same name as in the parent class
2. The method must have the same parameter as in the parent class.
3. There must be an IS-A relationship (inheritance).

 
Understanding the problem without method overriding
Let's understand the problem that we may face in the program if we don't use method overriding.

1. //Java Program to demonstrate why we need method overriding  
2. //Here, we are calling the method of parent class with child  
3. //class object.  
4. //Creating a parent class  
5. class Vehicle{  
6.   void run(){System.out.println("Vehicle is running");}  
7. }  
8. //Creating a child class  
9. class Bike extends Vehicle{  
10.   public static void main(String args[]){  
11.   //creating an instance of child class  
12.   Bike obj = new Bike();  
13.   //calling the method with child class instance  
14.   obj.run();  
15.   }  
16. }  
Test it Now

Output:

Vehicle is running

Problem is that I have to provide a specific implementation of run() method in subclass that is why we use
method overriding.

Example of method overriding


In this example, we have defined the run method in the subclass as defined in the parent class but it has
some specific implementation. The name and parameter of the method are the same, and there is IS-A
relationship between the classes, so there is method overriding.

1. //Java Program to illustrate the use of Java Method Overriding  
2. //Creating a parent class.  
3. class Vehicle{  
4.   //defining a method  
5.   void run(){System.out.println("Vehicle is running");}  
6. }  
7. //Creating a child class  
8. class Bike2 extends Vehicle{  
9.   //defining the same method as in the parent class  
10.   void run(){System.out.println("Bike is running safely");}  
11.   
12.   public static void main(String args[]){  
13.   Bike2 obj = new Bike2();//creating object  
14.   obj.run();//calling method  
15.   }  
16. }  
Test it Now

Output:

Bike is running safely

A real example of Java Method Overriding


Consider a scenario where Bank is a class that provides functionality to get the rate of interest. However,
the rate of interest varies according to banks. For example, SBI, ICICI and AXIS banks could provide
8%, 7%, and 9% rate of interest.
Java method overriding is mostly used in Runtime Polymorphism which we will learn in
next pages.

1. //Java Program to demonstrate the real scenario of Java Method Overriding  
2. //where three classes are overriding the method of a parent class.  
3. //Creating a parent class.  
4. class Bank{  
5. int getRateOfInterest(){return 0;}  
6. }  
7. //Creating child classes.  
8. class SBI extends Bank{  
9. int getRateOfInterest(){return 8;}  
10. }  
11.   
12. class ICICI extends Bank{  
13. int getRateOfInterest(){return 7;}  
14. }  
15. class AXIS extends Bank{  
16. int getRateOfInterest(){return 9;}  
17. }  
18. //Test class to create objects and call the methods  
19. class Test2{  
20. public static void main(String args[]){  
21. SBI s=new SBI();  
22. ICICI i=new ICICI();  
23. AXIS a=new AXIS();  
24. System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());  
25. System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());  
26. System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());  
27. }  
28. }  
Test it Now
Output:
SBI Rate of Interest: 8
ICICI Rate of Interest: 7
AXIS Rate of Interest: 9

Can we override static method?


No, a static method cannot be overridden. It can be proved by runtime polymorphism, so we will learn it
later.

Why can we not override static method?


It is because the static method is bound with class whereas instance method is bound with an object.
Static belongs to the class area, and an instance belongs to the heap area.

Can we override java main method?


No, because the main is a static method.

44. Difference between method Overloading and Overriding.


Method Overloading Method Overriding

1) Method overloading Method overriding provides the specific


increases the readability of the implementation of the method that is already
program. provided by its superclass.

2) Method overloading occurs Method overriding occurs in two classes that have
within the class. IS-A relationship between them.

3) In this case, the In this case, the parameters must be the same.
parameters must be different.

45. What is the output of the following Java program? Explain it.
class Base
{
void method(int a)
{
System.out.println("Base class method called with integer a = "+a);
}

void method(double d)
{
System.out.println("Base class method called with double d ="+d);
}
}

class Derived extends Base


{
@Override
void method(double d)
{
System.out.println("Derived class method called with double d ="+d);
}
}

public class Main


{
public static void main(String[] args)
{
new Derived().method(10);
}
}

Base class method called with integer a = 10


Explanation

The method() is overloaded in class Base whereas it is derived in class Derived with
the double type as the parameter. In the method call, the integer is passed.

46. What is the output of the following Java program? Explain it.
class Base
{
public void baseMethod()
{
System.out.println("BaseMethod called ...");
}
}
class Derived extends Base
{
public void baseMethod()
{
System.out.println("Derived method called ...");
}
}
public class Test
{
public static void main (String args[])
{
Base b = new Derived();
b.baseMethod();
}
}
Output: Derived method called ...
Explanation:
The method of Base class, i.e., baseMethod() is overridden in Derived class. In Test
class, the reference variable b (of type Base class) refers to the instance of the Derived class.
Here, Runtime polymorphism is achieved between class Base and Derived. At compile time, the
presence of method baseMethod checked in Base class, If it presence then the program compiled
otherwise the compiler error will be shown. In this case, baseMethod is present in Base class;
therefore, it is compiled successfully. However, at runtime, It checks whether the baseMethod
has been overridden by Derived class, if so then the Derived class method is called otherwise
Base class method is called. In this case, the Derived class overrides the baseMethod; therefore,
the Derived class method is called.
47. What is the final variable?

In Java, the final variable is used to restrict the user from updating it. If we initialize the final
variable, we can't change its value. In other words, we can say that the final variable once
assigned to a value, can never be changed after that. The final variable which is not assigned to
any value can only be assigned through the class constructor.

1. class Bike9{  
2.  final int speedlimit=90;//final variable  
3.  void run(){  
4.   speedlimit=400;  
5.  }  
6.  public static void main(String args[]){  
7.  Bike9 obj=new  Bike9();  
8.  obj.run();  
9.  }  
10. }//end of class  

Output:Compile Time Error

48. What is the final method?

If you make any method as final, you cannot override it.

Example of final method

1. class Bike{  
2.   final void run(){System.out.println("running");}  
3. }  
4.      
5. class Honda extends Bike{  
6.    void run(){System.out.println("running safely with 100kmph");}  
7.      
8.    public static void main(String args[]){  
9.    Honda honda= new Honda();  
10.    honda.run();  
11.    }  
12. }  

Output:Compile Time Error

49. What is the final class?

If you make any class as final, you cannot extend it.

Example of final class

final class Bike{}  
  
class Honda1 extends Bike{  
  void run(){System.out.println("running safely with 100kmph");}  
    
  public static void main(String args[]){  
  Honda1 honda= new Honda1();  
  honda.run();  
  }  
}  

Output:Compile Time Error

50. What is the output of the following Java program?


class Main {
public static void main(String args[]){
final int i;
i = 20;
System.out.println(i);
}
}

Output

20

Explanation

Since i is the blank final variable. It can be initialized only once. We have
initialized it to 20. Therefore, 20 will be printed.

51. What is Polymorphism?


Polymorphism means existing in many forms. Variables, functions, and objects can exist in
multiple forms in Java and Python. There are two types of polymorphism which are run-time
polymorphism and compile-time polymorphism. Run time can take a different form while the
application is running and compile-time can take a different form during compilation.

An excellent example of Polymorphism in Object-oriented programing is a cursor behavior. A


cursor may take different forms like an arrow, a line, cross, or other shapes depending on the
behavior of the user or the program mode. With polymorphism, a method or subclass can define
its behaviors and attributes while retaining some of the functionality of its parent class. This
means you can have a class that displays date and time, and then you can create a method to
inherit the class but should display a welcome message alongside the date and time. The goal of
Polymorphism in Object-oriented programming is to enforce simplicity, making codes more
extendable and easily maintaining applications. Inheritance allows you to create class
hierarchies, where a base class gives its behavior and attributes to a derived class. You are then
free to modify or extend its functionality. Polymorphism ensures that the proper method will be
executed based on the calling object’s type.

There are two major types of polymorphisms in Object Oriented Programming (OOPS)
languages. They are Static Binding (Compile time Polymorphism) and Dynamic Binding
(Runtime Polymorphism). Method overriding would be the example of Dynamic Polymorphism
and Method Overloading would be the example of Static Polymorphism.

Compile-time Polymorphism

By using the method of overloading, one can reach the state of static polymorphism in Object
Oriented Programming languages. This method would allow the programmer to implement
various methods. They might use the same names but their parameters are different. Method
overloading is an example of Static Polymorphism. There are certain conditions that are essential
for static polymorphism, they are:
 All Parameters would have to be different in types.
 The order of the Parameter might have to different.
 The number of parameters of one method would have to be different from the other
method.
During the run time, the compiler of the language would identify various methods by identifying
signatures of these methods. The compiler would first identify the method signature and then
decide the method for a specific method call while compiling the program. The execution time
for Compile time Polymorphism is much faster but this process is not very flexible.

Runtime Polymorphism

This process is also known as Dynamic method dispatch. Under this process, a call to a single
overridden method is solved during the runtime of the program. Method overriding is the prime
example of Runtime Polymorphism. In this process, the call is not solved by the compiler. The
overriding is achieved through pointers and virtual functions. Method Overriding is the process
of declaring a single method in a sub-class that is present in a parent class. Through this process,
the child class would gain a method for implementation. This method is given by the parent class
of the child class. This process of polymorphism is slow but it is more flexible than the static
polymorphism. The main advantage of Runtime Polymorphism is the ability of the class to offer
the specification of its own to another inherited method. This transfer of implementation of one
method to another method is possible without changing or modifying the codes of the parent
class object. Thus, if a single child class needs the implementation method of the parent class
while other child class might use the overriding feature to have different implementation
methods.

Advantages of using Polymorphism

 Polymorphism is one of the core concepts of OOPS. This feature is applicable while
handling various classes and sub-classes. Here are some of the advantages of using
polymorphism in programming.
 One of the biggest advantages of using Polymorphism is that it allows the
programming to extend itself.
 This process allows the user to reuse the old classes and codes that were once tested
and already implemented successfully. These codes and classes would be applicable to
various other methods.
 Through this process, the user would be able to store several variables of different
types (double, Float, Int, Long, etc) in a single variable. This would make it easier for
the user to search for and implementing these variables.
 Polymorphism would also help in reducing coupling between two different functions.
Polymorphism is the core concept of the Object-Oriented Programming (OOPS) languages. This
language is used for executing various types of huge programming. This concept makes it
possible to access different classes of objects through the same interface. Through this concept,
the programming would become more efficient and quicker.

52. What is meant by Interface?


An interface in Java is a blueprint of a class. It has static constants and abstract methods.

The interface in Java is a mechanism to achieve abstraction. There can be only abstract
methods in the Java interface, not method body. It is used to achieve abstraction and
multiple inheritance in Java.

In other words, you can say that interfaces can have abstract methods and variables. It cannot
have a method body.

Java Interface also represents the IS-A relationship.

It cannot be instantiated just like the abstract class.

Since Java 8, we can have default and static methods in an interface.

Since Java 9, we can have private methods in an interface.

Why use Java interface?

There are mainly three reasons to use interface. They are given below.

o It is used to achieve abstraction.


o By interface, we can support the functionality of multiple inheritance.
o It can be used to achieve loose coupling.

An interface is declared by using the interface keyword. It provides total abstraction; means
all the methods in an interface are declared with the empty body, and all the fields are public,
static and final by default. A class that implements an interface must implement all the
methods declared in the interface.

53. What is meant by Abstract class?

A class that is declared as abstract is known as an abstract class. It needs to be extended and its
method implemented. It cannot be instantiated. It can have abstract methods, non-abstract
methods, constructors, and static methods. It can also have the final methods which will force the
subclass not to change the body of the method. Consider the following example.

1. abstract class Bike{  
2.   abstract void run();  
3. }  
4. class Honda4 extends Bike{  
5. void run(){System.out.println("running safely");}  
6. public static void main(String args[]){  
7.  Bike obj = new Honda4();  
8.  obj.run();  
9. }  
10. }  

Output

running safely

54. Java collections: Array/Array List.


Arraylist class implements List interface and it is based on an Array data structure. It is
widely used because of the functionality and flexibility it offers. Most of the
developers choose Arraylist over Array as it’s a very good alternative of traditional java
arrays. ArrayList is a resizable-array implementation of the List interface. It implements
all optional list operations, and permits all elements, including null.

S Array ArrayList
N

1 The Array is of fixed size, means ArrayList is not of the fixed size we
we cannot resize the array as per can change the size dynamically.
need.

2 Arrays are of the static type. ArrayList is of dynamic size.

3 Arrays can store primitive data ArrayList cannot store the primitive
types as well as objects. data types it can only store the
objects.

Why ArrayList is better than Array?


The limitation with array is that it has a fixed length so if it is full you cannot add any
more elements to it, likewise if there are number of elements gets removed from it the
memory consumption would be the same as it doesn’t shrink.

55. Difference between Abstract class and Interface.


The short answer: An abstract class allows you to create functionality that subclasses can
implement or override. An interface only allows you to define functionality, not implement it.
And whereas a class can extend only one abstract class, it can take advantage of
multiple interfaces

1. Main difference is methods of a Java interface are implicitly abstract and


cannot have implementations. A Java abstract class can have instance
methods that implements a default behavior.
2. Variables declared in a Java interface is by default final. An  abstract class
may contain non-final variables.
3. Members of a Java interface are public by default. A Java abstract class can
have the usual flavors of class members like private, protected, etc..
4. Java interface should be implemented using keyword “implements”; A Java
abstract class should be extended using keyword “extends”.
5. An interface can extend another Java interface only, an abstract class can
extend another Java class and implement multiple Java interfaces.
6. A Java class can implement multiple interfaces but it can extend only one
abstract class.
7. Interface is absolutely abstract and cannot be instantiated; A Java abstract
class also cannot be instantiated, but can be invoked if a main() exists.
8. In comparison with java abstract classes, java interfaces are slow as it
requires extra indirection

56. What is the meaning of Collections in Java?

The Collection in Java is a framework that provides an architecture to store and manipulate


the group of objects.

Java Collections can achieve all the operations that you perform on a data such as searching,
sorting, insertion, manipulation, and deletion.

Java Collection means a single unit of objects. Java Collection framework provides many
interfaces (Set, List, Queue, Deque) and classes (ArrayList,
Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).

Next →← Prev

Collections in Java
1. Java Collection Framework

2. Hierarchy of Collection Framework

3. Collection interface

4. Iterator interface

The Collection in Java is a framework that provides an architecture to store and


manipulate the group of objects.
Java Collections can achieve all the operations that you perform on a data such as
searching, sorting, insertion, manipulation, and deletion.

Java Collection means a single unit of objects. Java Collection framework provides
many interfaces (Set, List, Queue, Deque) and classes (ArrayList,
Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).

What is Collection in Java

A Collection represents a single unit of objects, i.e., a group.

What is a framework in Java


o It provides readymade architecture.

o It represents a set of classes and interfaces.

o It is optional.

What is Collection framework

The Collection framework represents a unified architecture for storing and


manipulating a group of objects. It has:

1. Interfaces and its implementations, i.e., classes

2. Algorithm

Hierarchy of Collection Framework


Let us see the hierarchy of Collection framework. The java.util package contains all
the classes and interfaces for the Collection framework.
Methods of Collection interface
There are many methods declared in the Collection interface. They are as follows:

No. Method Description

1 public boolean add(E e) It is used to insert an element in this


collection.

2 public boolean It is used to insert the specified


addAll(Collection<? extends E> c) collection elements in the invoking
collection.

3 public boolean remove(Object It is used to delete an element from the


element) collection.

4 public boolean It is used to delete all the elements of


removeAll(Collection<?> c) the specified collection from the
invoking collection.

5 default boolean It is used to delete all the elements of


removeIf(Predicate<? super E> the collection that satisfy the specified
filter) predicate.

6 public boolean It is used to delete all the elements of


retainAll(Collection<?> c) invoking collection except the specified
collection.

7 public int size() It returns the total number of elements


in the collection.

8 public void clear() It removes the total number of


elements from the collection.

9 public boolean contains(Object It is used to search an element.


element)

10 public boolean It is used to search the specified


containsAll(Collection<?> c) collection in the collection.

11 public Iterator iterator() It returns an iterator.

12 public Object[] toArray() It converts collection into array.

13 public <T> T[] toArray(T[] a) It converts collection into array. Here,


the runtime type of the returned array
is that of the specified array.
14 public boolean isEmpty() It checks if collection is empty.

15 default Stream<E> It returns a possibly parallel Stream


parallelStream() with the collection as its source.

16 default Stream<E> stream() It returns a sequential Stream with the


collection as its source.

17 default Spliterator<E> It generates a Spliterator over the


spliterator() specified elements in the collection.

18 public boolean equals(Object It matches two collections.


element)

19 public int hashCode() It returns the hash code number of the


collection.

Iterator interface
Iterator interface provides the facility of iterating the elements in a forward direction only.

Methods of Iterator interface

There are only three methods in the Iterator interface. They are:

No Method Description
.

1 public boolean It returns true if the iterator has more elements


hasNext() otherwise it returns false.

2 public Object next() It returns the element and moves the cursor pointer to
the next element.

3 public void remove() It removes the last elements returned by the iterator. It
is less used.
Iterable Interface
The Iterable interface is the root interface for all the collection classes. The Collection
interface extends the Iterable interface and therefore all the subclasses of Collection
interface also implement the Iterable interface.

It contains only one abstract method. i.e.,

1. Iterator<T> iterator()  

It returns the iterator over the elements of type T.

Collection Interface
The Collection interface is the interface which is implemented by all the classes in the
collection framework. It declares the methods that every collection will have. In other
words, we can say that the Collection interface builds the foundation on which the
collection framework depends.

Some of the methods of Collection interface are Boolean add ( Object obj), Boolean
addAll ( Collection c), void clear(), etc. which are implemented by all the subclasses of
Collection interface.

List Interface
List interface is the child interface of Collection interface. It inhibits a list type data
structure in which we can store the ordered collection of objects. It can have duplicate
values.

List interface is implemented by the classes ArrayList, LinkedList, Vector, and Stack.

To instantiate the List interface, we must use :

1. List <data-type> list1= new ArrayList();  
2. List <data-type> list2 = new LinkedList();  
3. List <data-type> list3 = new Vector();  
4. List <data-type> list4 = new Stack();  
There are various methods in List interface that can be used to insert, delete, and
access the elements from the list.

The classes that implement the List interface are given below.

ArrayList
The ArrayList class implements the List interface. It uses a dynamic array to store the
duplicate element of different data types. The ArrayList class maintains the insertion
order and is non-synchronized. The elements stored in the ArrayList class can be
randomly accessed. Consider the following example.

1. import java.util.*;  
2. class TestJavaCollection1{  
3. public static void main(String args[]){  
4. ArrayList<String> list=new ArrayList<String>();//Creating arraylist  
5. list.add("Ravi");//Adding object in arraylist  
6. list.add("Vijay");  
7. list.add("Ravi");  
8. list.add("Ajay");  
9. //Traversing list through Iterator  
10.Iterator itr=list.iterator();  
11.while(itr.hasNext()){  
12.System.out.println(itr.next());  
13.}  
14.}  
15.}  

Output:

Ravi
Vijay
Ravi
Ajay

LinkedList
LinkedList implements the Collection interface. It uses a doubly linked list internally to
store the elements. It can store the duplicate elements. It maintains the insertion order
and is not synchronized. In LinkedList, the manipulation is fast because no shifting is
required.

Consider the following example.

1. import java.util.*;  
2. public class TestJavaCollection2{  
3. public static void main(String args[]){  
4. LinkedList<String> al=new LinkedList<String>();  
5. al.add("Ravi");  
6. al.add("Vijay");  
7. al.add("Ravi");  
8. al.add("Ajay");  
9. Iterator<String> itr=al.iterator();  
10.while(itr.hasNext()){  
11.System.out.println(itr.next());  
12.}  
13.}  
14.}  

Output:

Ravi
Vijay
Ravi
Ajay

Vector
Vector uses a dynamic array to store the data elements. It is similar to ArrayList.
However, It is synchronized and contains many methods that are not the part of
Collection framework.

Consider the following example.

1. import java.util.*;  
2. public class TestJavaCollection3{  
3. public static void main(String args[]){  
4. Vector<String> v=new Vector<String>();  
5. v.add("Ayush");  
6. v.add("Amit");  
7. v.add("Ashish");  
8. v.add("Garima");  
9. Iterator<String> itr=v.iterator();  
10.while(itr.hasNext()){  
11.System.out.println(itr.next());  
12.}  
13.}  
14.}  

Output:

Ayush
Amit
Ashish
Garima

Stack
The stack is the subclass of Vector. It implements the last-in-first-out data structure,
i.e., Stack. The stack contains all of the methods of Vector class and also provides its
methods like boolean push(), boolean peek(), boolean push(object o), which defines its
properties.

Consider the following example.

1. import java.util.*;  
2. public class TestJavaCollection4{  
3. public static void main(String args[]){  
4. Stack<String> stack = new Stack<String>();  
5. stack.push("Ayush");  
6. stack.push("Garvit");  
7. stack.push("Amit");  
8. stack.push("Ashish");  
9. stack.push("Garima");  
10.stack.pop();  
11.Iterator<String> itr=stack.iterator();  
12.while(itr.hasNext()){  
13.System.out.println(itr.next());  
14.}  
15.}  
16.}  

Output:

Ayush
Garvit
Amit
Ashish

Queue Interface
Queue interface maintains the first-in-first-out order. It can be defined as an ordered
list that is used to hold the elements which are about to be processed. There are
various classes like PriorityQueue, Deque, and ArrayDeque which implements the
Queue interface.

Queue interface can be instantiated as:

1. Queue<String> q1 = new PriorityQueue();  
2. Queue<String> q2 = new ArrayDeque();  

There are various classes that implement the Queue interface, some of them are given
below.

PriorityQueue
The PriorityQueue class implements the Queue interface. It holds the elements or
objects which are to be processed by their priorities. PriorityQueue doesn't allow null
values to be stored in the queue.

Consider the following example.

1. import java.util.*;  
2. public class TestJavaCollection5{  
3. public static void main(String args[]){  
4. PriorityQueue<String> queue=new PriorityQueue<String>();  
5. queue.add("Amit Sharma");  
6. queue.add("Vijay Raj");  
7. queue.add("JaiShankar");  
8. queue.add("Raj");  
9. System.out.println("head:"+queue.element());  
10.System.out.println("head:"+queue.peek());  
11.System.out.println("iterating the queue elements:");  
12.Iterator itr=queue.iterator();  
13.while(itr.hasNext()){  
14.System.out.println(itr.next());  
15.}  
16.queue.remove();  
17.queue.poll();  
18.System.out.println("after removing two elements:");  
19.Iterator<String> itr2=queue.iterator();  
20.while(itr2.hasNext()){  
21.System.out.println(itr2.next());  
22.}  
23.}  
24.}  

Output:

head:Amit Sharma
head:Amit Sharma
iterating the queue elements:
Amit Sharma
Raj
JaiShankar
Vijay Raj
after removing two elements:
Raj
Vijay Raj

Deque Interface
Deque interface extends the Queue interface. In Deque, we can remove and add the
elements from both the side. Deque stands for a double-ended queue which enables us
to perform the operations at both the ends.

Deque can be instantiated as:

1. Deque d = new ArrayDeque();  
ArrayDeque
ArrayDeque class implements the Deque interface. It facilitates us to use the Deque.
Unlike queue, we can add or delete the elements from both the ends.

ArrayDeque is faster than ArrayList and Stack and has no capacity restrictions.

Consider the following example.

1. import java.util.*;  
2. public class TestJavaCollection6{  
3. public static void main(String[] args) {  
4. //Creating Deque and adding elements  
5. Deque<String> deque = new ArrayDeque<String>();  
6. deque.add("Gautam");  
7. deque.add("Karan");  
8. deque.add("Ajay");  
9. //Traversing elements  
10.for (String str : deque) {  
11.System.out.println(str);  
12.}  
13.}  
14.}  

Output:

Gautam
Karan
Ajay

Set Interface
Set Interface in Java is present in java.util package. It extends the Collection interface.
It represents the unordered set of elements which doesn't allow us to store the
duplicate items. We can store at most one null value in Set. Set is implemented by
HashSet, LinkedHashSet, and TreeSet.

Set can be instantiated as:

1. Set<data-type> s1 = new HashSet<data-type>();  
2. Set<data-type> s2 = new LinkedHashSet<data-type>();  
3. Set<data-type> s3 = new TreeSet<data-type>();  

HashSet
HashSet class implements Set Interface. It represents the collection that uses a hash
table for storage. Hashing is used to store the elements in the HashSet. It contains
unique items.

Consider the following example.

1. import java.util.*;  
2. public class TestJavaCollection7{  
3. public static void main(String args[]){  
4. //Creating HashSet and adding elements  
5. HashSet<String> set=new HashSet<String>();  
6. set.add("Ravi");  
7. set.add("Vijay");  
8. set.add("Ravi");  
9. set.add("Ajay");  
10.//Traversing elements  
11.Iterator<String> itr=set.iterator();  
12.while(itr.hasNext()){  
13.System.out.println(itr.next());  
14.}  
15.}  
16.}  

Output:

Vijay
Ravi
Ajay

LinkedHashSet
LinkedHashSet class represents the LinkedList implementation of Set Interface. It
extends the HashSet class and implements Set interface. Like HashSet, It also contains
unique elements. It maintains the insertion order and permits null elements.
Consider the following example.

1. import java.util.*;  
2. public class TestJavaCollection8{  
3. public static void main(String args[]){  
4. LinkedHashSet<String> set=new LinkedHashSet<String>();  
5. set.add("Ravi");  
6. set.add("Vijay");  
7. set.add("Ravi");  
8. set.add("Ajay");  
9. Iterator<String> itr=set.iterator();  
10.while(itr.hasNext()){  
11.System.out.println(itr.next());  
12.}  
13.}  
14.}  

Output:

Ravi
Vijay
Ajay

SortedSet Interface
SortedSet is the alternate of Set interface that provides a total ordering on its
elements. The elements of the SortedSet are arranged in the increasing (ascending)
order. The SortedSet provides the additional methods that inhibit the natural ordering
of the elements.

The SortedSet can be instantiated as:

1. SortedSet<data-type> set = new TreeSet();  

TreeSet
Java TreeSet class implements the Set interface that uses a tree for storage. Like
HashSet, TreeSet also contains unique elements. However, the access and retrieval
time of TreeSet is quite fast. The elements in TreeSet stored in ascending order.

Consider the following example:


1. import java.util.*;  
2. public class TestJavaCollection9{  
3. public static void main(String args[]){  
4. //Creating and adding elements  
5. TreeSet<String> set=new TreeSet<String>();  
6. set.add("Ravi");  
7. set.add("Vijay");  
8. set.add("Ravi");  
9. set.add("Ajay");  
10.//traversing elements  
11.Iterator<String> itr=set.iterator();  
12.while(itr.hasNext()){  
13.System.out.println(itr.next());  
14.}  
15.}  
16.}  

Output:

Ajay
Ravi
Vijay

57. What is meant by Exception?


An exception is an event, which occurs during the execution of a program, that disrupts the normal flow
of the program's instructions.

When an error occurs within a method, the method creates an object and hands it off to the runtime system.
The object, called an exception object, contains information about the error, including its type and the state of
the program when the error occurred. Creating an exception object and handing it to the runtime system is
called throwing an exception.

After a method throws an exception, the runtime system attempts to find something to handle it. The set of
possible "somethings" to handle the exception is the ordered list of methods that had been called to get to the
method where the error occurred. The list of methods is known as the call stack (see the next figure).
The call stack.

The runtime system searches the call stack for a method that contains a block of code that can handle the
exception. This block of code is called an exception handler. The search begins with the method in which the
error occurred and proceeds through the call stack in the reverse order in which the methods were called.
When an appropriate handler is found, the runtime system passes the exception to the handler. An exception
handler is considered appropriate if the type of the exception object thrown matches the type that can be
handled by the handler.

The exception handler chosen is said to catch the exception. If the runtime system exhaustively searches all
the methods on the call stack without finding an appropriate exception handler, as shown in the next figure,
the runtime system (and, consequently, the program) terminates.

Searching the call stack for the exception handler.

58. What are the types of Exceptions?


Java defines several types of exceptions that relate to its various class libraries.
Java also allows users to define their own exceptions. 
 
Built-in Exceptions
 

 
Built-in exceptions are the exceptions which are available in Java libraries.
These exceptions are suitable to explain certain error situations. Below is the
list of important built-in exceptions in Java. 
 
1. ArithmeticException 
It is thrown when an exceptional condition has occurred in an arithmetic
operation.
2. ArrayIndexOutOfBoundsException 
It is thrown to indicate that an array has been accessed with an illegal index.
The index is either negative or greater than or equal to the size of the array.
3. ClassNotFoundException 
This Exception is raised when we try to access a class whose definition is
not found
4. FileNotFoundException 
This Exception is raised when a file is not accessible or does not open.
5. IOException 
It is thrown when an input-output operation failed or interrupted
6. InterruptedException 
It is thrown when a thread is waiting , sleeping , or doing some processing ,
and it is interrupted.
7. NoSuchFieldException 
It is thrown when a class does not contain the field (or variable) specified
8. NoSuchMethodException 
It is thrown when accessing a method which is not found.
9. NullPointerException 
This exception is raised when referring to the members of a null object. Null
represents nothing
10. NumberFormatException 
This exception is raised when a method could not convert a string into a
numeric format.
11. RuntimeException 
This represents any exception which occurs during runtime.
12. StringIndexOutOfBoundsException 
It is thrown by String class methods to indicate that an index is either
negative or greater than the size of the string
 
Examples of Built-in Exception:
 
 Arithmetic exception
 
 Java

// Java program to demonstrate ArithmeticException

class ArithmeticException_Demo

    public static void main(String args[])

    {

        try {

            int a = 30, b = 0;

            int c = a/b;  // cannot divide by zero

            System.out.println ("Result = " + c);


        }

        catch(ArithmeticException e) {

            System.out.println ("Can't divide a number by 0");

        }

    }

 Output: 
 
Can't divide a number by 0
  
 NullPointer Exception
 
 Java

//Java program to demonstrate NullPointerException

class NullPointer_Demo

    public static void main(String args[])

    {

        try {

            String a = null; //null value

            System.out.println(a.charAt(0));
        } catch(NullPointerException e) {

            System.out.println("NullPointerException..");

        }

    }

 Output:
NullPointerException..
  
 StringIndexOutOfBound Exception
 
 Java

// Java program to demonstrate StringIndexOutOfBoundsException

class StringIndexOutOfBound_Demo

    public static void main(String args[])

    {

        try {

            String a = "This is like chipping "; // length is 22

            char c = a.charAt(24); // accessing 25th element

            System.out.println(c);

        }
        catch(StringIndexOutOfBoundsException e) {

            System.out.println("StringIndexOutOfBoundsException");

        }

    }

 Output: 
 
StringIndexOutOfBoundsException
  
 FileNotFound Exception
 
 Java

//Java program to demonstrate FileNotFoundException

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

 class File_notFound_Demo {

    public static void main(String args[])  {

        try {

 
            // Following file does not exist

            File file = new File("E://file.txt");

            FileReader fr = new FileReader(file);

        } catch (FileNotFoundException e) {

           System.out.println("File does not exist");

        }

    }

 Output:
File does not exist
  
 NumberFormat Exception
 
 Java

// Java program to demonstrate NumberFormatException

class  NumberFormat_Demo

    public static void main(String args[])

    {

        try {
            // "akki" is not a number

            int num = Integer.parseInt ("akki") ;

            System.out.println(num);

        } catch(NumberFormatException e) {

            System.out.println("Number format exception");

        }

    }

 Output:
Number format exception
  
 ArrayIndexOutOfBounds Exception
 
 Java

// Java program to demonstrate ArrayIndexOutOfBoundException

class ArrayIndexOutOfBound_Demo

    public static void main(String args[])

    {

        try{
            int a[] = new int[5];

            a[6] = 9; // accessing 7th element in an array of

                      // size 5

        }

        catch(ArrayIndexOutOfBoundsException e){

            System.out.println ("Array Index is Out Of Bounds");

        }

    }

 Output: 
 
Array Index is Out Of Bounds
  
 
User-Defined Exceptions
Sometimes, the built-in exceptions in Java are not able to describe a certain
situation. In such cases, user can also create exceptions which are called ‘user-
defined Exceptions’. 
Following steps are followed for the creation of user-defined Exception. 
 
 The user should create an exception class as a subclass of Exception class.
Since all the exceptions are subclasses of Exception class, the user should
also make his class a subclass of it. This is done as: 
 
class MyException extends Exception
  
 We can write a default constructor in his own exception class. 
 
MyException(){}
  
 We can also create a parameterized constructor with a string as a
parameter. 
We can use this to store exception details. We can call super
class(Exception) constructor from this and send the string there. 
 
MyException(String str)
{
super(str);
}
  
 To raise exception of user-defined type, we need to create an object to his
exception class and throw it using throw clause, as: 
 
MyException me = new MyException(“Exception details”);
throw me;
  
 
 The following program illustrates how to create own exception class
MyException.
 Details of account numbers, customer names, and balance amounts are
taken in the form of three arrays.
 In main() method, the details are displayed using a for-loop. At this time,
check is done if in any account the balance amount is less than the minimum
balance amount to be apt in the account.
 If it is so, then MyException is raised and a message is displayed “Balance
amount is less”.
 

 Java

// Java program to demonstrate user defined exception

// This program throws an exception whenever balance


// amount is below Rs 1000

class MyException extends Exception

    //store account information

    private static int accno[] = {1001, 1002, 1003, 1004};

    private static String name[] =

                 {"Nish", "Shubh", "Sush", "Abhi", "Akash"};

    private static double bal[] =

         {10000.00, 12000.00, 5600.0, 999.00, 1100.55};

    // default constructor

    MyException() {    }

    // parametrized constructor

    MyException(String str) { super(str); }

    // write main()


    public static void main(String[] args)

    {

        try  {

            // display the heading for the table

            System.out.println("ACCNO" + "\t" + "CUSTOMER" +

                                           "\t" + "BALANCE");

            // display the actual account information

            for (int i = 0; i < 5 ; i++)

            {

                System.out.println(accno[i] + "\t" + name[i] +

                                               "\t" + bal[i]);

                // display own exception if balance < 1000

                if (bal[i] < 1000)

                {

                    MyException me =

                       new MyException("Balance is less than 1000");

                    throw me;
                }

            }

        } //end of try

        catch (MyException e) {

            e.printStackTrace();

        }

    }

RunTime Error 
 
MyException: Balance is less than 1000
at MyException.main(fileProperty.java:36)
Output: 
 
ACCNO CUSTOMER BALANCE
1001 Nish 10000.0
1002 Shubh 12000.0
1003 Sush 5600.0
1004 Abhi 999.0

59. What are the different ways to handle exceptions?


1. Clean Up Resources in a Finally Block or
Use a Try-With-Resource Statement
It happens quite often that you use a resource in your try block, like an InputStream, which you need
to close afterward. A common mistake in these situations is to close the resource at the end of the
try block.

1
public void doNotCloseResourceInTry() {
2
FileInputStream inputStream = null;
3
try {
4
File file = new File("./tmp.txt");
5
inputStream = new FileInputStream(file);
6

7
// use the inputStream to read a file
8

9
// do NOT do this
10
inputStream.close();
11
} catch (FileNotFoundException e) {
12
log.error(e);
13
} catch (IOException e) {
14
log.error(e);
15
}
16
}

The problem is that this approach seems to work perfectly fine as long as no exception gets thrown.
All statements within the try block will get executed, and the resource gets closed.

But you added the try block for a reason. You call one or more methods which might throw an
exception, or maybe you throw the exception yourself. That means you might not reach the end of
the try block. And as a result, you will not close the resources.

You should, therefore, put all your clean up code into the finally block or use a try-with-resource
statement.
Use a Finally Block
In contrast to the last few lines of your try block, the finally block gets always executed. That
happens either after the successful execution of the try block or after you handled an exception in a
catch block. Due to this, you can be sure that you clean up all the opened resources.

1
public void closeResourceInFinally() {
2
FileInputStream inputStream = null;
3
try {
4
File file = new File("./tmp.txt");
5
inputStream = new FileInputStream(file);
6

7
// use the inputStream to read a file
8

9
} catch (FileNotFoundException e) {
10
log.error(e);
11
} finally {
12
if (inputStream != null) {
13
try {
14
inputStream.close();
15
} catch (IOException e) {
16
log.error(e);
17
}
18
}
19
}
20
}
Java 7’s Try-With-Resource Statement
Another option is the try-with-resource statement which I explained in more detail in my introduction
to Java exception handling.

You can use it if your resource implements the AutoCloseable interface. That’s what most Java
standard resources do. When you open the resource in the try clause, it will get automatically closed
after the try block got executed, or an exception handled.

1
public void automaticallyCloseResource() {
2
File file = new File("./tmp.txt");
3
try (FileInputStream inputStream = new FileInputStream(file);) {
4
// use the inputStream to read a file
5

6
} catch (FileNotFoundException e) {
7
log.error(e);
8
} catch (IOException e) {
9
log.error(e);
10
}
11
}

2. Prefer Specific Exceptions


The more specific the exception is that you throw, the better. Always keep in mind that a co-worker
who doesn’t know your code, or maybe you in a few months, need to call your method and handle
the exception.

Therefore make sure to provide them as many information as possible. That makes your API easier
to understand. And as a result, the caller of your method will be able to handle the exception better
or avoid it with an additional check.

So, always try to find the class that fits best to your exceptional event, e.g. throw
a NumberFormatException instead of an IllegalArgumentException. And avoid throwing an
unspecific Exception.

1
public void doNotDoThis() throws Exception {
2
...
3
}
4

5
public void doThis() throws NumberFormatException {
6
...
7
}

3. Document the Exceptions You Specify


Whenever you specify an exception in your method signature, you should also document it in your
Javadoc. That has the same goal as the previous best practice: Provide the caller as many
information as possible so that he can avoid or handle the exception.

So, make sure to add a @throws declaration to your Javadoc and to describe the situations that can
cause the exception.

1
/**
2
* This method does something extremely useful ...
3
*
4
* @param input
5
* @throws MyBusinessException if ... happens
6
*/
7
public void doSomething(String input) throws MyBusinessException {
8
...
9
}

4. Throw Exceptions With Descriptive


Messages
The idea behind this best practice is similar to the two previous ones. But this time, you don’t provide
the information to the caller of your method. The exception’s message gets read by everyone who
has to understand what had happened when the exception was reported in the log file or your
monitoring tool.
It should, therefore, describe the problem as precisely as possible and provide the most relevant
information to understand the exceptional event.

Don’t get me wrong; you shouldn’t write a paragraph of text. But you should explain the reason for
the exception in 1-2 short sentences. That helps your operations team to understand the severity of
the problem, and it also makes it easier for you to analyze any service incidents.

If you throw a specific exception, its class name will most likely already describe the kind of error.
So, you don’t need to provide a lot of additional information. A good example for that is
the NumberFormatException. It gets thrown by the constructor of the class java.lang.Long when you
provide a String in a wrong format.

1
try {
2
new Long("xyz");
3
} catch (NumberFormatException e) {
4
log.error(e);
5
}

The name of the NumberFormatException class already tells you the kind of problem. Its message
only needs to provide the input string that caused the problem. If the name of the exception class
isn’t that expressive, you need to provide the required information in the message.

1
17:17:26,386 ERROR TestExceptionHandling:52 - java.lang.NumberFormatException:
For input string: "xyz"

5. Catch the Most Specific Exception First


Most IDEs help you with this best practice. They report an unreachable code block when you try to
catch the less specific exception first.

The problem is that only the first catch block that matches the exception gets executed. So, if you
catch an IllegalArgumentException first, you will never reach the catch block that should handle the
more specific NumberFormatException because it’s a subclass of the IllegalArgumentException.

Always catch the most specific exception class first and add the less specific catch blocks to the end
of your list.

You can see an example of such a try-catch statement in the following code snippet. The first catch
block handles all NumberFormatExceptions and the second one all IllegalArgumentExceptions
which are not a NumberFormatException.

1
public void catchMostSpecificExceptionFirst() {
2
try {
3
doSomething("A message");
4
} catch (NumberFormatException e) {
5
log.error(e);
6
} catch (IllegalArgumentException e) {
7
log.error(e)
8
}
9
}

6. Don’t Catch Throwable


Throwable is the superclass of all exceptions and errors. You can use it in a catch clause, but you
should never do it!

If you use Throwable in a catch clause, it will not only catch all exceptions; it will also catch all errors.
Errors are thrown by the JVM to indicate serious problems that are not intended to be handled by an
application. Typical examples for that are the OutOfMemoryError or the StackOverflowError. Both
are caused by situations that are outside of the control of the application and can’t be handled.

So, better don’t catch a Throwable unless you’re absolutely sure that you’re in an exceptional
situation in which you’re able or required to handle an error.

1
public void doNotCatchThrowable() {
2
try {
3
// do something
4
} catch (Throwable t) {
5
// don't do this!
6
}
7
}

7. Don’t Ignore Exceptions


Have you ever analyzed a bug report where only the first part of your use case got executed?
That’s often caused by an ignored exception. The developer was probably pretty sure that it would
never be thrown and added a catch block that doesn’t handle or logs it. And when you find this
block, you most likely even find one of the famous “This will never happen” comments.

1
public void doNotIgnoreExceptions() {
2
try {
3
// do something
4
} catch (NumberFormatException e) {
5
// this will never happen
6
}
7
}

Well, you might be analyzing a problem in which the impossible happened.

So, please, never ignore an exception. You don’t know how the code will change in the future.
Someone might remove the validation that prevented the exceptional event without recognizing that
this creates a problem. Or the code that throws the exception gets changed and now throws multiple
exceptions of the same class, and the calling code doesn’t prevent all of them.

You should at least write a log message telling everyone that the unthinkable just had happened and
that someone needs to check it.

1
public void logAnException() {
2
try {
3
// do something
4
} catch (NumberFormatException e) {
5
log.error("This should never happen: " + e);
6
}
7
}

8. Don’t Log and Throw


That is probably the most often ignored best practice in this list. You can find lots of code snippets
and even libraries in which an exception gets caught, logged and rethrown.
1
try {
2
new Long("xyz");
3
} catch (NumberFormatException e) {
4
log.error(e);
5
throw e;
6
}

It might feel intuitive to log an exception when it occurred and then rethrow it so that the caller can
handle it appropriately. But it will write multiple error messages for the same exception.

1
17:44:28,945 ERROR TestExceptionHandling:65 - java.lang.NumberFormatException:
For input string: "xyz"
2
Exception in thread "main" java.lang.NumberFormatException: For input string:
"xyz"
3
at
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
4
at java.lang.Long.parseLong(Long.java:589)
5
at java.lang.Long.(Long.java:965)
6
at
com.stackify.example.TestExceptionHandling.logAndThrowException(TestExceptionH
andling.java:63)
7
at
com.stackify.example.TestExceptionHandling.main(TestExceptionHandling.java:58)

The additional messages also don’t add any information. As explained in best practice #4, the
exception message should describe the exceptional event. And the stack trace tells you in which
class, method, and line the exception was thrown.

If you need to add additional information, you should catch the exception and wrap it in a custom
one. But make sure to follow best practice number 9.

1
public void wrapException(String input) throws MyBusinessException {
2
try {
3
// do something
4
} catch (NumberFormatException e) {
5
throw new MyBusinessException("A message that describes the error.",
e);
6
}
7
}

So, only catch an exception if you want to handle it. Otherwise, specify it in the method signature
and let the caller take care of it.

9. Wrap the Exception Without Consuming it


It’s sometimes better to catch a standard exception and to wrap it into a custom one. A typical
example for such an exception is an application or framework specific business exception. That
allows you to add additional information and you can also implement a special handling for your
exception class.

When you do that, make sure to set the original exception as the cause. The Exception class
provides specific constructor methods that accept a Throwable as a parameter. Otherwise, you lose
the stack trace and message of the original exception which will make it difficult to analyze the
exceptional event that caused your exception.

1
public void wrapException(String input) throws MyBusinessException {
2
try {
3
// do something
4
} catch (NumberFormatException e) {
5
throw new MyBusinessException("A message that describes the error.",
e);
6
}
7
}

Summary
As you’ve seen, there are lots of different things you should consider when you throw or catch an
exception. Most of them have the goal to improve the readability of your code or the usability of your
API.
Exceptions are most often an error handling mechanism and a communication medium at the same
time. You should, therefore, make sure to discuss the best practices and rules you want to apply
with your coworkers so that everyone understands the general concepts and uses them in the same
way.

60. What are the advantages of Exception handling?

Advantage 1: Separating Error-Handling Code from "Regular" Code

Exceptions provide the means to separate the details of what to do when something out of the ordinary
happens from the main logic of a program. In traditional programming, error detection, reporting, and handling
often lead to confusing spaghetti code. For example, consider the pseudocode method here that reads an
entire file into memory.

readFile {
open the file;
determine its size;
allocate that much memory;
read the file into memory;
close the file;
}

At first glance, this function seems simple enough, but it ignores all the following potential errors.

 What happens if the file can't be opened?


 What happens if the length of the file can't be determined?
 What happens if enough memory can't be allocated?
 What happens if the read fails?
 What happens if the file can't be closed?

To handle such cases, the readFile function must have more code to do error detection, reporting, and
handling. Here is an example of what the function might look like.

errorCodeType readFile {
initialize errorCode = 0;

open the file;


if (theFileIsOpen) {
determine the length of the file;
if (gotTheFileLength) {
allocate that much memory;
if (gotEnoughMemory) {
read the file into memory;
if (readFailed) {
errorCode = -1;
}
} else {
errorCode = -2;
}
} else {
errorCode = -3;
}
close the file;
if (theFileDidntClose && errorCode == 0) {
errorCode = -4;
} else {
errorCode = errorCode and -4;
}
} else {
errorCode = -5;
}
return errorCode;
}

There's so much error detection, reporting, and returning here that the original seven lines of code are lost in
the clutter. Worse yet, the logical flow of the code has also been lost, thus making it difficult to tell whether the
code is doing the right thing: Is the file really being closed if the function fails to allocate enough memory? It's
even more difficult to ensure that the code continues to do the right thing when you modify the method three
months after writing it. Many programmers solve this problem by simply ignoring it — errors are reported when
their programs crash.

Exceptions enable you to write the main flow of your code and to deal with the exceptional cases elsewhere. If
the readFile function used exceptions instead of traditional error-management techniques, it would look more
like the following.

readFile {
try {
open the file;
determine its size;
allocate that much memory;
read the file into memory;
close the file;
} catch (fileOpenFailed) {
doSomething;
} catch (sizeDeterminationFailed) {
doSomething;
} catch (memoryAllocationFailed) {
doSomething;
} catch (readFailed) {
doSomething;
} catch (fileCloseFailed) {
doSomething;
}
}

Note that exceptions don't spare you the effort of doing the work of detecting, reporting, and handling errors,
but they do help you organize the work more effectively.

Advantage 2: Propagating Errors Up the Call Stack

A second advantage of exceptions is the ability to propagate error reporting up the call stack of methods.
Suppose that the readFile method is the fourth method in a series of nested method calls made by the main
program: method1 calls method2, which calls method3, which finally calls readFile.

method1 {
call method2;
}

method2 {
call method3;
}

method3 {
call readFile;
}

Suppose also that method1 is the only method interested in the errors that might occur within readFile.
Traditional error-notification techniques force method2 and method3 to propagate the error codes returned
by readFile up the call stack until the error codes finally reach method1—the only method that is interested in
them.

method1 {
errorCodeType error;
error = call method2;
if (error)
doErrorProcessing;
else
proceed;
}

errorCodeType method2 {
errorCodeType error;
error = call method3;
if (error)
return error;
else
proceed;
}

errorCodeType method3 {
errorCodeType error;
error = call readFile;
if (error)
return error;
else
proceed;
}

Recall that the Java runtime environment searches backward through the call stack to find any methods that
are interested in handling a particular exception. A method can duck any exceptions thrown within it, thereby
allowing a method farther up the call stack to catch it. Hence, only the methods that care about errors have to
worry about detecting errors.

method1 {
try {
call method2;
} catch (exception e) {
doErrorProcessing;
}
}
method2 throws exception {
call method3;
}

method3 throws exception {


call readFile;
}

However, as the pseudocode shows, ducking an exception requires some effort on the part of the middleman
methods. Any checked exceptions that can be thrown within a method must be specified in its throws clause.

Advantage 3: Grouping and Differentiating Error Types

Because all exceptions thrown within a program are objects, the grouping or categorizing of exceptions is a
natural outcome of the class hierarchy. An example of a group of related exception classes in the Java platform
are those defined in java.io — IOException and its descendants. IOException is the most general and
represents any type of error that can occur when performing I/O. Its descendants represent more specific
errors. For example, FileNotFoundException means that a file could not be located on disk.

A method can write specific handlers that can handle a very specific exception.
The FileNotFoundException class has no descendants, so the following handler can handle only one type of
exception.

catch (FileNotFoundException e) {
...
}

A method can catch an exception based on its group or general type by specifying any of the exception's
superclasses in the catch statement. For example, to catch all I/O exceptions, regardless of their specific type,
an exception handler specifies an IOException argument.

catch (IOException e) {
...
}

This handler will be able to catch all I/O exceptions, including FileNotFoundException, EOFException, and so
on. You can find details about what occurred by querying the argument passed to the exception handler. For
example, use the following to print the stack trace.

catch (IOException e) {
// Output goes to System.err.
e.printStackTrace();
// Send trace to stdout.
e.printStackTrace(System.out);
}

You could even set up an exception handler that handles any Exception with the handler here.

// A (too) general exception handler


catch (Exception e) {
...
}
The Exception class is close to the top of the Throwable class hierarchy. Therefore, this handler will catch many
other exceptions in addition to those that the handler is intended to catch. You may want to handle exceptions
this way if all you want your program to do, for example, is print out an error message for the user and then
exit.

In most situations, however, you want exception handlers to be as specific as possible. The reason is that the
first thing a handler must do is determine what type of exception occurred before it can decide on the best
recovery strategy. In effect, by not catching specific errors, the handler must accommodate any possibility.
Exception handlers that are too general can make code more error-prone by catching and handling exceptions
that weren't anticipated by the programmer and for which the handler was not intended.

61. What is the Java instanceOf operator?

instanceof is a binary operator used to test if an object is of a given type. The
result of the operation is either true or false. It's also known as type comparison
operator because it compares the instance with type.
Before casting an unknown object, the instanceof check should always be used. Doing
this helps in avoiding ClassCastException at runtime.
The instanceof operator's basic syntax is:
(object) instanceof (type)
Let's see a basic example for the instanceof operator. First, let's create a class Round:
public class Round {
// implementation details
}
Next, let's create a class Ring that extends Round:
public class Ring extends Round {
// implementation details
}
We can use instanceof to check if an instance of Ring is of Round type:
@Test
public void givenWhenInstanceIsCorrect_thenReturnTrue() {
Ring ring = new Ring();
Assert.assertTrue(ring instanceof Round);
}

3. How Does the instanceof Operator Work?


The instanceof operator works on the principle of the is-a relationship. The
concept of an is-a relationship is based on class inheritance or interface
implementation.
To demonstrate this, let's create a Shape interface:
public interface Shape {
// implementation details
}
Let's also create a class Circle that implements the Shape interface and also extends
the Round class:
public class Circle extends Round implements Shape {
// implementation details
}
The instanceof result will be true if the object is an instance of the type:
@Test
public void givenWhenObjectIsInstanceOfType_thenReturnTrue() {
Circle circle = new Circle();
Assert.assertTrue(circle instanceof Circle);
}
It will also be true if the object is an instance of a subclass of the type:
@Test
public void giveWhenInstanceIsOfSubtype_thenReturnTrue() {
Circle circle = new Circle();
Assert.assertTrue(circle instanceof Round);
}
If the type is an interface, it will return true if the object implements the
interface:
@Test
public void givenWhenTypeIsInterface_thenReturnTrue() {
Circle circle = new Circle();
Assert.assertTrue(circle instanceof Shape);
}
The instanceof operator cannot be used if there is no relationship between the
object that is being compared and the type it is being compared with.
Let's create a new class Triangle that implements Shape but has no relationship
with Circle:
public class Triangle implements Shape {
// implementation details
}
Now, if we use instanceof to check if a Circle is an instance of Triangle:
@Test
public void givenWhenComparingClassInDiffHierarchy_thenCompilationError() {
Circle circle = new Circle();
Assert.assertFalse(circle instanceof Triangle);
}
We'll get a compilation error because there's no relationship between the Circle and
the Triangle classes:
java.lang.Error: Unresolved compilation problem:
Incompatible conditional operand types Circle and Triangle
4. Using instanceof with the Object Type
In Java, every class implicitly inherits from the Object class. Therefore, using
the instanceof operator with the Object type will always evaluate to true:
@Test
public void givenWhenTypeIsOfObjectType_thenReturnTrue() {
Thread thread = new Thread();
Assert.assertTrue(thread instanceof Object);
}

5. Using the instanceof Operator When an Object


Is null

If we use the instanceof operator on any object that is null, it returns false. Also, no


null check is needed when using an instanceof operator.
@Test
public void givenWhenInstanceValueIsNull_thenReturnFalse() {
Circle circle = null;
Assert.assertFalse(circle instanceof Round);
}

6. instanceof and Generics
Instance tests and casts depend on inspecting the type information at runtime.
Therefore, we can't use instanceof along with erased generic types.
For instance, if we try to compile the following snippet:
public static <T> void sort(List<T> collection) {
if (collection instanceof List<String>) {
// sort strings differently
}

// omitted
}
Then we get this compilation error:
error: illegal generic type for instanceof
if (collection instanceof List<String>) {
^
Technically speaking, we are only allowed to use instanceof along with
reified types in Java. A type is reified if its type information is present at runtime.
The reified types in Java are as follows:

 Primitive types like int


 Non-generic classes and interfaces like String or Random
 Generic types in which all types are unbounded wildcards like Set<?
> or Map<?, ?>
 Raw types like List or HashMap
 Arrays of other reifiable types like String[], List[], or Map<?, ?>[]

Because generic type parameters aren't reified, we can't use them either:
public static <T> boolean isOfType(Object input) {
return input instanceof T; // won't compile
}
However, it's possible to test against something like List<?>:
if (collection instanceof List<?>) {
// do something
}

62. What is the difference between abstraction and encapsulation?


// Java program to demonstrate encapsulation

  

public class Encapsulate {

  

    // private variables declared

    // these can only be accessed by

    // public methods of class


    private String geekName;

    private int geekRoll;

    private int geekAge;

  

    // get method for age to access

    // private variable geekAge

    public int getAge()

    {

        return geekAge;

    }

  

    // get method for name to access

    // private variable geekName

    public String getName()

    {

        return geekName;

    }

  

    // get method for roll to access


    // private variable geekRoll

    public int getRoll()

    {

        return geekRoll;

    }

  

    // set method for age to access

    // private variable geekage

    public void setAge(int newAge)

    {

        geekAge = newAge;

    }

  

    // set method for name to access

    // private variable geekName

    public void setName(String newName)

    {

        geekName = newName;

    }
  

    // set method for roll to access

    // private variable geekRoll

    public void setRoll(int newRoll)

    {

        geekRoll = newRoll;

    }

  

// Class to access variables

// of the class Encapsulate

class TestEncapsulation {

    public static void main(String[] args)

    {

        Encapsulate obj = new Encapsulate();

  

        // setting values of the variables

        obj.setName("Harsh");

        obj.setAge(19);
        obj.setRoll(51);

  

        // Displaying values of the variables

        System.out.println("Geek's name: " + obj.getName());

        System.out.println("Geek's age: " + obj.getAge());

        System.out.println("Geek's roll: " + obj.getRoll());

  

        // Direct access of geekRoll is not possible

        // due to encapsulation

        // System.out.println("Geek's roll: " + obj.geekName);

    }

Output:

Geek's name: Harsh


Geek's age: 19
Geek's roll: 51
Abstraction in Java
Data Abstraction is the property by virtue of which only the essential details are
displayed to the user. The trivial or the non-essentials units are not displayed to
the user. Ex: A car is viewed as a car rather than its individual components.
Data Abstraction may also be defined as the process of identifying only the
required characteristics of an object ignoring the irrelevant details. The
properties and behaviours of an object differentiate it from other objects of
similar type and also help in classifying/grouping the objects.
// Java program to illustrate the concept of Abstraction

  

abstract class Shape {

    String color;

  

    // these are abstract methods

    abstract double area();

    public abstract String toString();

  

    // abstract class can have a constructor

    public Shape(String color)

    {

        System.out.println("Shape constructor called");

        this.color = color;

    }

  

    // this is a concrete method

    public String getColor()

    {
        return color;

    }

class Circle extends Shape {

    double radius;

  

    public Circle(String color, double radius)

    {

  

        // calling Shape constructor

        super(color);

        System.out.println("Circle constructor called");

        this.radius = radius;

    }

  

    @Override

    double area()

    {

        return Math.PI * Math.pow(radius, 2);


    }

  

    @Override

    public String toString()

    {

        return "Circle color is "

            + super.color

            + "and area is : "

            + area();

    }

  

class Rectangle extends Shape {

  

    double length;

    double width;

  

    public Rectangle(String color,

                     double length,
                     double width)

    {

  

        // calling Shape constructor

        super(color);

        System.out.println("Rectangle constructor called");

        this.length = length;

        this.width = width;

    }

  

    @Override

    double area()

    {

        return length * width;

    }

  

    @Override

    public String toString()

    {
        return "Rectangle color is "

            + super.color

            + "and area is : "

            + area();

    }

  

public class Test {

    public static void main(String[] args)

    {

        Shape s1 = new Circle("Red", 2.2);

        Shape s2 = new Rectangle("Yellow", 2, 4);

  

        System.out.println(s1.toString());

        System.out.println(s2.toString());

    }

Output:
Shape constructor called
Circle constructor called
Shape constructor called
Rectangle constructor called
Circle color is Redand area is : 15.205308443374602
Rectangle color is Yellowand area is : 8.0

63. How to make a read-only class in Java?

We can make a class read-only by making all of the data members private.

Please note:

 If we make a class read-only, then we can’t modify the properties or data members
value of the class.
 If we make a class read-only, then we can only read the properties or data members
value of the class.
 The read-only class will contain only getter methods which return the value of the
private properties to the main() function.
 The read-only class can contain setter methods if we want to modify the value of the
private properties after reading because there is our choice to keep setter method in
the class but as per based on the concepts we should not contain.

Now, we will see the objective of the getter method, why it is required?

Few points need to remember about getter methods are given below:

 As we know that "private" data member of the class is accessible in the same class
only.
 Let suppose we want to access "private" data member of the class in outside class.
So, in that case, we need to declare public "getter" methods.
 The objective of the getter method is used to view the private variable values.

Syntax:

public returntype getDataMember_Name();

In the Getter method, it is not mandatory the same data member name after get, but it is
convenient for our understanding that we should consider the same name as the data
member after get.

There are few advantages of getter methods are given below:


 Hiding the internal representation of the private data member.
 Getter methods provide access level hierarchy.
 This method adds additional functionality easily later.
 This class allows getter methods to be passed around as lambda expressions rather
than values.
 The private data member is accessible from outside the class with the help of getter
methods.

Example:

// Java program to demonstrate the example of


// making Read-only class in Java

public class Weeks {


// Private Data Member Declaration
private String days = "7 days";

// Defining Getter method to return the value of


// private properties.
public String getDays() {
return days;
}

public static void main(String[] args) {


// Weeks object instanstiation
Weeks w = new Weeks();

// Get the value of the private member


String result = w.getDays();
// Display the value of the private properties
System.out.println("Days in a Week:" + " " + result);

}
}
Output:Days in a Week: 7 days

64. How to make a write-only class in Java?

We can make a class write-only by making all of the data members private.

Please note:

 If we made a class write-only then we can modify the properties or data member
value of the class.
 If we made a class write-only then we can only write the properties or data members
value of the class.
 The write-only class will contain setter methods which write the value of the private
properties because there is a setter method available in the class.
 The write-only class can contain getter method if we want to read the value of the
private properties after writing.

Now, we will see the objective of the setter method, why it is required?

Few points need to remember about setter methods are given below:

 As we know that "private" data member of the class is accessible in the same class
only.
 Let suppose we want to access "private" data member of the class in outside class.
So, in that case, we need to declare public "setter" methods.
 The objective of the set method is used to update or set the private variable values.

Syntax:

public void setDataMember_Name(Type var_name);

In the setter method, it is not mandatory the same data member name after set, but it is
convenient for our understanding that we should consider the same name as the data
member after set.

There are few advantages of Setter Methods are given below:

 Hiding the internal representation of the private data member.


 Setter methods provide access level hierarchy.
 This method adds additional functionality easily later.
 This class allows getter methods to be protected properties from unexpected
changes by outside class.
 The private data member is updateable from outside the class with the help of setter
methods.

Example:

// Java program to demonstrate the example of


// making write-only class in Java

public class WriteWeeks {


// Private Data Member Declaration
private String days;

// Defining Setter method to write the value of


// private properties and this method takes an argument
// and assign it in the private member.
public void setDays(String days) {
this.days = days;
}

// Defining Getter method to retrive the value of


//private variable

public String getDays() {


return days;
}

public static void main(String[] args) {


// Weeks object instanstiation
WriteWeeks w = new WriteWeeks();

// Set the value of the private member


w.setDays("7 Days");

String result = w.getDays();


// Display the value of the private properties
System.out.println("Days in a Week :" + " " + result);

}
}

Output

Days in a Week : 7 Days

65. Explain the hierarchy of Java Exception classes?

The java.lang.Throwable class is the root class of Java Exception hierarchy which is
inherited by two subclasses: Exception and Error. A hierarchy of Java Exception classes
are given below:
Basically, there are two types of exceptions in java API. They are:
1. Predefined Exceptions (Built-in-Exceptions)
2. Custom Exceptions
Predefined Exceptions:
Predefined exceptions are those exceptions that are already defined by Java
system. These exceptions are also called built-in-exceptions.
Java API supports exception handling by providing the number of predefined
exceptions. These predefined exceptions are represented by classes in java.
When a predefined exception occurs, JVM (Java runtime system) creates an
object of predefined exception class. All exceptions are derived from
java.lang.Throwable class but not all exception classes are defined in the same
package.
All the predefined exceptions supported by java are organized as subclasses in a
hierarchy under the Throwable class.
The Throwable class is the root of exception hierarchy and is an immediate
subclass of Object class. Let’s take a look at the java exception hierarchy, as
shown in the below figure.

1. Throwable class: As shown in the above figure, Throwable class which is


derived from Object class, is a top of exception hierarchy from which all
exception classes are derived directly or indirectly. It is the root of all exception
classes. It is present in java.lang package.

Throwable class is the superclass of all exceptions in java. This class has two
subclasses: Error and Exception. Errors or exceptions occurring in java programs
are objects of these classes. Using Throwable class, you can also create your
own custom exceptions.
2. Error: Error class is the subclass of Throwable class and a superclass of all
the runtime error classes. It terminates the program if there is problem-related to
a system or resources (JVM).
An error generally represents an unusual problem or situation from which it is
difficult to recover. It does not occur by programmer mistakes. It generally occurs
if the system is not working properly or resource is not allocated properly.
VirtualMachineError, StackOverFlowError, AssertionError, LinkageError,
OutOfMmeoryError, etc are examples of error. We will learn more detail in further
tutorials.
3. Exception: It is represented by an Exception class that represents errors
caused by the program and by external factors. Exception class is a subclass of
Throwable class and a superclass of all the exception classes. All the exception
classes are derived directly or indirectly from the Exception class. They generally
originate from within the application.
The exception class provides two constructors:
 public Exception() (Default constructor)
 public Exception(String message) (It takes a message string as argument)
Each of the exception classes provides two constructors: one with no argument
and another with a String type argument. Exception class does not provide its
own method. It inherits all methods provided by Throwable class.

Exception Class Hierarchy in Java

The hierarchy of exception class in Java has been shown in the below figure that
is very important for an interview purpose.
Custom Exceptions:
Custom exceptions are those exceptions that are created by users or
programmers according to their own needs. The custom exceptions are also
called user-defined exceptions that are created by extending the exception class.
So, Java provides the liberty to programmers to throw and handle exceptions
while dealing with functional requirements of problems they are solving. We will
discuss in more detail about custom exceptions in further tutorials.
Let’s see the brief description of each subclass of the Exception class.

RuntimeException class (Unchecked Exceptions)

RuntimeException class is a subclass of the Exception class. It is thrown by JVM


or programmatically when an arithmetic operation performed in the program is
incorrect or defect/bug occurs in the program’s code.
RuntimeException and all its exception subclasses are not checked by Java
compiler because they occur during runtime of a program. That’s why these
exceptions are also called unchecked exceptions.
RuntimeException class consists of many other exception subclasses that are
used to handle a specific type of exception. Apart from these exception
subclasses of RuntimeException class shown in the above figure, there are also
other subclasses of RuntimeException class which has not been shown in the
hierarchy structure diagram to avoid complexity.
Let’s see a brief description of them.
1. ArithmeticException: This exception is thrown when arithmetic problems,
such as a number is divided by zero, is occurred. That is, it is caused by maths
error.
2. ClassCastException: The ClassCastException is a runtime exception that is
thrown by JVM when we attempt to invalid typecasting in the program. That is, it
is thrown when we cast an object to a subclass of which an object is not an
instance.
3. IllegalArgumentException: This runtime exception is thrown by
programmatically when an illegal or appropriate argument is passed to call a
method. This exception class has further two subclasses:
 NumberFormatException
 IllegalThreadStateException
NumericFormatException: NumberFormatException is thrown by
programmatically when we try to convert a string into the numeric type and the
process of illegal conversion fails. That is, it occurs due to the illegal conversion
of a string to a numeric format.
IllegalThreadStateException: IllegalThreadStateException exception is a
runtime exception that is thrown by programmatically when we attempt to
perform any operation on a thread but it is incompatible with the current thread
state.
4. IndexOutOfBoundsException: This exception class is thrown by JVM when
an array or string is going out of the specified index. It has two further
subclasses:
 ArrayIndexOutOfBoundsException
 StringIndexOutOfBoundsException
ArrayIndexOutOfBoundsException: ArrayIndexOutOfBoundsException
exception is thrown when an array element is accessed out of the index.
StringIndexOutOfBoundsException: StringIndexOutOfBoundsException
exception is thrown when a String or StringBuffer element is accessed out of the
index.
5. NullPointerException: NullPointerException is a runtime exception that is
thrown by JVM when we attempt to use null instead of an object. That is, it is
thrown when the reference is null.
6. ArrayStoreException: This exception occurs when we attempt to store any
value in an array which is not of array type. For example, suppose, an array is of
integer type but we are trying to store a value of an element of another type.
7. IllegalStateException: The IllegalStateException exception is thrown by
programmatically when the runtime environment is not in an appropriate state for
calling any method.
8. IllegalMonitorStateException: This exception is thrown when a thread does
not have the right to monitor an object and tries to access wait(), notify(), and
notifyAll() methods of the object.
9. NegativeArraySizeException: The NegativeArraySizeException exception is
thrown when an array is created with a negative size.

List of Checked Exceptions in Java

Now, we have listed checked exceptions in a brief description.


1. ClassNotFoundException: The ClassNotFoundException is a kind of
checked exception that is thrown when we attempt to use a class that does not
exist.
Checked exceptions are those exceptions that are checked by the Java compiler
itself.
2. FileNotFoundException: The FileNotFoundException is a checked exception
that is thrown when we attempt to access a non-existing file.
3. InterruptedException: InterruptedException is a checked exception that is
thrown when a thread is in sleeping or waiting state and another thread attempt
to interrupt it.
4. InstantiationException: This exception is also a checked exception that is
thrown when we try to create an object of abstract class or interface. That is,
InstantiationException exception occurs when an abstract class or interface is
instantiated.
5. IllegalAccessException: The IllegalAccessException is a checked exception
and it is thrown when a method is called in another method or class but the
calling method or class does not have permission to access that method.
6. CloneNotSupportedException: This checked exception is thrown when we
try to clone an object without implementing the cloneable interface.
7. NoSuchFieldException: This is a checked exception that is thrown when an
unknown variable is used in a program.
8. NoSuchMethodException: This checked exception is thrown when the
undefined method is used in a program.
Hope that this tutorial has covered almost all the basic points related to the
exception hierarchy in java. I hope that you will have understood the basic points
of Throwable class and its subclasses: Exception and Error.
Key Points to Remember:
 Two types of exceptions in Java: Predefined and Custom exceptions.
 The root class for all the exceptions in the hierarchy of exception classes is
java.lang.Throwable.
 Exception classes are mainly divided into three types: system errors,
exceptions, and runtime exceptions.
 System errors are represented by Error class and thrown by JVM.
 Exceptions are represented by Exception classes that describe errors in
your program.

66. What is the difference between Checked Exception and Unchecked


Exception?
The main difference between checked and unchecked exception is that the
checked exceptions are checked at compile-time while unchecked exceptions
are checked at runtime.

What are checked exceptions?


Checked exceptions are checked at compile-time. It means if a method is
throwing a checked exception then it should handle the exception using try-catch
block or it should declare the exception using throws keyword, otherwise the
program will give a compilation error.

Lets understand this with the help of an example:

Checked Exception Example


In this example we are reading the file myfile.txt and displaying its content on the
screen. In this program there are three places where a checked exception is
thrown as mentioned in the comments below. FileInputStream which is used for
specifying the file path and name, throws FileNotFoundException. The read() method
which reads the file content throws IOException and the close() method which
closes the file input stream also throws IOException.

import java.io.*;
class Example {
public static void main(String args[])
{
FileInputStream fis = null;
/*This constructor FileInputStream(File filename)
* throws FileNotFoundException which is a checked
* exception
*/
fis = new FileInputStream("B:/myfile.txt");
int k;

/* Method read() of FileInputStream class also throws


* a checked exception: IOException
*/
while(( k = fis.read() ) != -1)
{
System.out.print((char)k);
}

/*The method close() closes the file input stream


* It throws IOException*/
fis.close();
}
}
Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:


Unhandled exception type FileNotFoundException
Unhandled exception type IOException
Unhandled exception type IOException
Why this compilation error? As I mentioned in the beginning that checked
exceptions gets checked during compile time. Since we didn’t handled/declared
the exceptions, our program gave the compilation error.
How to resolve the error? There are two ways to avoid this error. We will see
both the ways one by one.

Method 1: Declare the exception using throws keyword.


As we know that all three occurrences of checked exceptions are inside main()
method so one way to avoid the compilation error is: Declare the exception in the
method using throws keyword. You may be thinking that our code is throwing
FileNotFoundException and IOException both then why we are declaring the
IOException alone. The reason is that IOException is a parent class of
FileNotFoundException so it by default covers that. If you want you can declare
them like this public static void main(String args[]) throws IOException, FileNotFoundException .

import java.io.*;
class Example {
public static void main(String args[]) throws IOException
{
FileInputStream fis = null;
fis = new FileInputStream("B:/myfile.txt");
int k;

while(( k = fis.read() ) != -1)


{
System.out.print((char)k);
}
fis.close();
}
}
Output:
File content is displayed on the screen.

Method 2: Handle them using try-catch blocks.


The approach we have used above is not good at all. It is not the best exception
handling practice. You should give meaningful message for each exception type
so that it would be easy for someone to understand the error. The code should
be like this:

import java.io.*;
class Example {
public static void main(String args[])
{
FileInputStream fis = null;
try{
fis = new FileInputStream("B:/myfile.txt");
}catch(FileNotFoundException fnfe){
System.out.println("The specified file is not " +
"present at the given path");
}
int k;
try{
while(( k = fis.read() ) != -1)
{
System.out.print((char)k);
}
fis.close();
}catch(IOException ioe){
System.out.println("I/O error occurred: "+ioe);
}
}
}
This code will run fine and will display the file content.

Here are the few other Checked Exceptions –

 SQLException
 IOException
 ClassNotFoundException
 InvocationTargetException

What are Unchecked exceptions?


Unchecked exceptions are not checked at compile time. It means if your program
is throwing an unchecked exception and even if you didn’t handle/declare that
exception, the program won’t give a compilation error. Most of the times these
exception occurs due to the bad data provided by user during the user-program
interaction. It is up to the programmer to judge the conditions in advance, that
can cause such exceptions and handle them appropriately. All Unchecked
exceptions are direct sub classes of RuntimeException class.

Lets understand this with an example:

Unchecked Exception Example


class Example {
public static void main(String args[])
{
int num1=10;
int num2=0;
/*Since I'm dividing an integer with 0
* it should throw ArithmeticException
*/
int res=num1/num2;
System.out.println(res);
}
}
If you compile this code, it would compile successfully however when you will run
it, it would throw ArithmeticException. That clearly shows that unchecked exceptions
are not checked at compile-time, they occurs at runtime. Lets see another
example.

class Example {
public static void main(String args[])
{
int arr[] ={1,2,3,4,5};
/* My array has only 5 elements but we are trying to
* display the value of 8th element. It should throw
* ArrayIndexOutOfBoundsException
*/
System.out.println(arr[7]);
}
}
This code would also compile successfully since ArrayIndexOutOfBoundsException is
also an unchecked exception.
Note: It doesn’t mean that compiler is not checking these exceptions so we
shouldn’t handle them. In fact we should handle them more carefully. For e.g. In
the above example there should be a exception message to user that they are
trying to display a value which doesn’t exist in array so that user would be able to
correct the issue.

class Example {
public static void main(String args[]) {
try{
int arr[] ={1,2,3,4,5};
System.out.println(arr[7]);
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("The specified index does not exist " +
"in array. Please correct the error.");
}
}
}
Output:

The specified index does not exist in array. Please correct the error.

67. Write a regular expression to validate a password. A password must


start with an alphabet and followed by alphanumeric characters; Its
length must be in between 8 to 20.

The regular expression for the above criteria will be: ^[a-zA-Z][a-zA-Z0-9]


{8,19} where ^ represents the start of the regex, [a-zA-Z] represents that the first
character must be an alphabet, [a-zA-Z0-9] represents the alphanumeric character,
{8,19} represents that the length of the password must be in between 8 and 20.

 ^ represents starting character of the string.


 (?=.*[0-9]) represents a digit must occur at least once.
 (?=.*[a-z]) represents a lower case alphabet must occur at least once.
 (?=.*[A-Z]) represents an upper case alphabet that must occur at least once.
 (?=.*[@#$%^&-+=()] represents a special character that must occur at least
once.
 (?=\\S+$) white spaces don’t allowed in the entire string.
 .{8, 20} represents at least 8 characters and at most 20 characters.
 $ represents the end of the strin

// Java program to validate


// the password using ReGex
  
import java.util.regex.*;
class GFG {
  
    // Function to validate the password.
    public static boolean
    isValidPassword(String password)
    {
  
        // Regex to check valid password.
        String regex = "^(?=.*[0-9])"
                       + "(?=.*[a-z])(?=.*[A-Z])"
                       + "(?=.*[@#$%^&+=])"
                       + "(?=\\S+$).{8,20}$";
  
        // Compile the ReGex
        Pattern p = Pattern.compile(regex);
  
        // If the password is empty
        // return false
        if (password == null) {
            return false;
        }
  
        // Pattern class contains matcher() method
        // to find matching between given password
        // and regular expression.
        Matcher m = p.matcher(password);
  
        // Return if the password
        // matched the ReGex
        return m.matches();
    }
  
    // Driver Code.
    public static void main(String args[])
    {
  
        // Test Case 1:
        String str1 = "Geeks@portal20";
        System.out.println(isValidPassword(str1));
  
        // Test Case 2:
        String str2 = "Geeksforgeeks";
        System.out.println(isValidPassword(str2));
  
        // Test Case 3:
        String str3 = "Geeks@ portal9";
        System.out.println(isValidPassword(str3));
  
        // Test Case 4:
        String str4 = "1234";
        System.out.println(isValidPassword(str4));
  
        // Test Case 5:
        String str5 = "Gfg@20";
        System.out.println(isValidPassword(str5));
  
        // Test Case 6:
        String str6 = "geeks@portal20";
        System.out.println(isValidPassword(str6));
    }
}

68. In Java, How many ways you know to take input from the console?

In Java, there are three ways by using which, we can take input from the console.

o Using BufferedReader class: we can take input from the console by wrapping
System.in into an InputStreamReader and passing it into the BufferedReader. It
provides an efficient reading as the input gets buffered. Consider the following
example.
1. import java.io.BufferedReader;   
2. import java.io.IOException;   
3. import java.io.InputStreamReader;   
4. public class Person   
5. {   
6.     public static void main(String[] args) throws IOException    
7.     {   
8.       System.out.println("Enter the name of the person");  
9.         BufferedReader reader = new BufferedReader(new InputStreamReader(Sy
stem.in));   
10.        String name = reader.readLine();   
11.        System.out.println(name);           
12.    }   
13.}   

o Using Scanner class: The Java Scanner class breaks the input into tokens
using a delimiter that is whitespace by default. It provides many methods to
read and parse various primitive values. Java Scanner class is widely used to
parse text for string and primitive types using a regular expression. Java
Scanner class extends Object class and implements Iterator and Closeable
interfaces. Consider the following example.

1. import java.util.*;    
2. public class ScannerClassExample2 {      
3.       public static void main(String args[]){                         
4.           String str = "Hello/This is JavaTpoint/My name is Abhishek.";    
5.           //Create scanner with the specified String Object    
6.           Scanner scanner = new Scanner(str);    
7.           System.out.println("Boolean Result: "+scanner.hasNextBoolean());           
   
8.           //Change the delimiter of this scanner    
9.           scanner.useDelimiter("/");    
10.          //Printing the tokenized Strings    
11.          System.out.println("---Tokenizes String---");     
12.        while(scanner.hasNext()){    
13.            System.out.println(scanner.next());    
14.        }    
15.          //Display the new delimiter    
16.          System.out.println("Delimiter used: " +scanner.delimiter());              
17.          scanner.close();    
18.          }      
19.}    
20.    
o Using Console class: The Java Console class is used to get input from the
console. It provides methods to read texts and passwords. If you read the
password using the Console class, it will not be displayed to the user. The
java.io.Console class is attached to the system console internally. The Console
class is introduced since 1.5. Consider the following example.

1. import java.io.Console;    
2. class ReadStringTest{      
3. public static void main(String args[]){      
4. Console c=System.console();      
5. System.out.println("Enter your name: ");      
6. String n=c.readLine();      
7. System.out.println("Welcome "+n);      
8. }      
9. }    

69. What is JDBC?


JDBC is a Java API that is used to connect and execute the query to the database. JDBC
API uses JDBC drivers to connect to the database. JDBC API can be used to access
tabular data stored into any relational database.

The current version of JDBC is 4.3. It is the stable release since 21st September, 2017.
It is based on the X/Open SQL Call Level Interface. The java.sql package contains
classes and interfaces for JDBC API. A list of popular interfaces of JDBC API are given
below:

o Driver interface

o Connection interface

o Statement interface

o PreparedStatement interface

o CallableStatement interface

o ResultSet interface

o ResultSetMetaData interface

o DatabaseMetaData interface

o RowSet interface

A list of popular classes of JDBC API are given below:

o DriverManager class

o Blob class

o Clob class

o Types class
Why Should We Use JDBC
Before JDBC, ODBC API was the database API to connect and execute the query with
the database. But, ODBC API uses ODBC driver which is written in C language (i.e.
platform dependent and unsecured). That is why Java has defined its own API (JDBC
API) that uses JDBC drivers (written in Java language).

We can use JDBC API to handle database using Java program and can perform the
following activities:

1. Connect to the database

2. Execute queries and update statements to the database

3. Retrieve the result received from the database.

What is API
API (Application programming interface) is a document that contains a description of all
the features of a product or software. It represents classes and interfaces that software
programs can follow to communicate with each other. An API can be created for
applications, libraries, operating systems, etc

70. What are the steps to connect to the database in java?


There are 5 steps to connect any java application with the database using JDBC. These steps are as

o Register the Driver class

o Create connection

o Create statement

o Execute queries

o Close connection
1) Register the driver class
The forName() method of Class class is used to register the driver class. This method is used to d

Syntax of forName() method


1. public static void forName(String className)throws ClassNotFoundException  

Note: Since JDBC 4.0, explicitly registering the driver is optional. We just need to put
vender's Jar in the classpath, and then JDBC driver manager can detect and load the
driver automatically.

Example to register the OracleDriver class


Here, Java program is loading oracle driver to esteblish database connection.

1. Class.forName("oracle.jdbc.driver.OracleDriver");  
2) Create the connection object
The getConnection() method of DriverManager class is used to establish connection with the data

Syntax of getConnection() method


1. 1) public static Connection getConnection(String url)throws SQLException  
2. 2) public static Connection getConnection(String url,String name,String password)  
3. throws SQLException  

Example to establish connection with the Oracle


database
1. Connection con=DriverManager.getConnection(  
2. "jdbc:oracle:thin:@localhost:1521:xe","system","password");  

3) Create the Statement object


The createStatement() method of Connection interface is used to create statement. The object of s
queries with the database.

Syntax of createStatement() method


1. public Statement createStatement()throws SQLException  

Example to create the statement object


1. Statement stmt=con.createStatement();  

4) Execute the query


The executeQuery() method of Statement interface is used to execute queries to the database. This
ResultSet that can be used to get all the records of a table.

Syntax of executeQuery() method


1. public ResultSet executeQuery(String sql)throws SQLException  
Example to execute query
1. ResultSet rs=stmt.executeQuery("select * from emp");  
2.   
3. while(rs.next()){  
4. System.out.println(rs.getInt(1)+" "+rs.getString(2));  
5. }  

5) Close the connection object


By closing connection object statement and ResultSet will be closed automatically. The close() meth
to close the connection.

Syntax of close() method


1. public void close()throws SQLException  

Example to close connection


1. con.close();  

Note: Since Java 7, JDBC has ability to use try-with-resources statement to


automatically close resources of type Connection, ResultSet, and Statement.

It avoids explicit connection closing step.

71. Design principle: Single-responsibility principle


A class should have only one reason to change *
▪ This means that class should have only one job (responsible for one thing)
▪ There is a place for everything, and everything should be in its own place (Clean
room example)
▪ Expectations are often exaggerated
▪ By following the Single Responsibility Principle, you make sure that your class or
module has high cohesion, which means that your class does not do more than it
should
You need to feel the balance. Use SOLID as design principles but not as rules!
▪ Use common sense when you want to apply SOLID
▪ Avoid over-fragmenting when you apply Singular Responsibility Principle
▪ You need to understand where to stop
▪ DON`T try to obtain SOLID, since it is the thing that helps you to obtain
maintainability
▪ The goals are easier maintainability and extensibility
▪ For a man with a hammer, everything looks like a nail

72. Design principle: Open–closed principle


You should be able to extend a class without modifying it *
▪ Objects or entities should be open for extension, but closed for modification
▪ Extend functionality by adding new code, instead of changing existing one
▪ Separate the behaviors, therefore, the system will not be broken and easily
extended
▪ The same as you`ve used packages and libraries. One can use its functionalities but
never change.
You need to feel the balance. Use SOLID as design principles but not as rules!
▪ Use common sense when you want to apply SOLID
▪ Avoid over-fragmenting when you apply Singular Responsibility Principle
▪ You need to understand where to stop
▪ DON`T try to obtain SOLID, since it is the thing that helps you to obtain
maintainability
▪ The goals are easier maintainability and extensibility
▪ For a man with a hammer, everything looks like a nail

73. Design principle: Liskov substitution principle


Derived classes must be substitutable for their base classes without consumer
knowing it *
▪ Let 𝜑(𝑥) be a property provable about objects of 𝑥 of type T. Then 𝜑(𝑦) should be
provable for objects 𝑦 of type S where S is a subtype of T.
▪ Every part of code should get the expected result whatever class instance is sent
(knowing that this class implements the same interface)
▪ In short, this principle says that to build software systems from interchangeable
parts, those parts must adhere to a contract that allows those parts to be substituted
one for another
You need to feel the balance. Use SOLID as design principles but not as rules!
▪ Use common sense when you want to apply SOLID
▪ Avoid over-fragmenting when you apply Singular Responsibility Principle
▪ You need to understand where to stop
▪ DON`T try to obtain SOLID, since it is the thing that helps you to obtain
maintainability
▪ The goals are easier maintainability and extensibility
▪ For a man with a hammer, everything looks like a nail

74. Design principle: Interface segregation principle


Make fine-grained interfaces with specific methods *
▪ A client should never be forced to implement an interface that it doesn't use, or
clients shouldn't be forced to depend on methods they do not use.
▪ A client should never depend on anything more that the method which is used
▪ Changing a method should not affect on unrelated class
▪ Fat interface should be changed to several small and specific ones
You need to feel the balance. Use SOLID as design principles but not as rules!
▪ Use common sense when you want to apply SOLID
▪ Avoid over-fragmenting when you apply Singular Responsibility Principle
▪ You need to understand where to stop
▪ DON`T try to obtain SOLID, since it is the thing that helps you to obtain
maintainability
▪ The goals are easier maintainability and extensibility
▪ For a man with a hammer, everything looks like a nail

75. Design principle: Dependency inversion principle.


▪ Depend on abstractions, not on concretions *
▪ It states that the high-level module must not depend on the low-level module, but
they should depend on abstractions
▪ It should be possible to change an implementation easily without altering highlevel code
▪ The code that implements high-level policy should not depend on the code that
implements low-level details. Rather, details should depend on policies
You need to feel the balance. Use SOLID as design principles but not as rules!
▪ Use common sense when you want to apply SOLID
▪ Avoid over-fragmenting when you apply Singular Responsibility Principle
▪ You need to understand where to stop
▪ DON`T try to obtain SOLID, since it is the thing that helps you to obtain
maintainability
▪ The goals are easier maintainability and extensibility
▪ For a man with a hammer, everything looks like a nail
76. Java language features: lambda, RTTI, reflection, etc.(lecture 7)
RTTI syntax
Java performs its RTTI using the Class object, even if you’re doing
something like a cast. The class Class also has a number of other ways
you can use RTTI.

First, you must get a reference to the appropriate Class object. One way to
do this, is to use a string and the Class.forName( ) method. This is
convenient because you don’t need an object of that type in order to get the
Class reference. However, if you do already have an object of the type
you’re interested in, you can fetch the Class reference by calling a method
that’s part of the Object root class: getClass( ). This returns the Class
reference representing the actual type of the object.
The Class.getInterfaces( ) method returns an array of Class objects representing the
interfaces that are contained in the Class object of interest.

If you have a Class object, you can also ask it for its direct base class
using getSuperclass( ). This, of course, returns a Class reference that you can
further query. This means that at run time, you can discover an object’s entire class
hierarchy.

The newInstance( ) method of Class can, at first, seem like just another way


to clone( ) an object. However, you can create a new object
with newInstance( ) without an existing object, as seen here, because there is
no Toy object—only cy, which is a reference to y’s Class object. This is a way to
implement a “virtual constructor,” which allows you to say “I don’t know exactly what
type you are, but create yourself properly anyway.” In the preceding example, cy is
just a Class reference with no further type information known at compile time. And
when you create a new instance, you get back an Object reference. But that
reference is pointing to a Toy object. Of course, before you can send any messages
other than those accepted by Object, you have to investigate it a bit and do some
casting. In addition, the class that’s being created with newInstance( ) must have a
default constructor. In the next section, you’ll see how to dynamically create objects of
classes using any constructor, with the Java reflection API (Application Programmer
Interface).

The final method in the listing is printInfo( ), which takes a Class reference and gets
its name with getName( ), and finds out whether it’s an interface with isInterface( ).
Thus, with the Class object you can find out just about everything you want to know
about an object.
Generics are a facility of generic programming that were added to the Java
programming language in 2004 within version J2SE 5.0. They were
designed to extend Java's type system to allow "a type or method to
operate on objects of various types while providing compile-time type
safety".[1] The aspect compile-time type safety was not fully achieved,
since it was shown in 2016 that it is not guaranteed in all cases.[2]

The Java collections framework supports generics to specify the type of


objects stored in a collection instance.

In 1998, Gilad Bracha, Martin Odersky, David Stoutamire and Philip Wadler
created Generic Java, an extension to the Java language to support
generic types.[3] Generic Java was incorporated in Java with the addition
of wildcards.
77. Class diagrams: give and example, how to read it.
A class represent a concept which encapsulates state (attributes) and behavior
(operations). Each attribute has a type. Each operation has a signature. The
class name is the only mandatory information.
Class Name:
 The name of the class appears in the first partition.

Class Attributes:
 Attributes are shown in the second partition.
 The attribute type is shown after the colon.
 Attributes map onto member variables (data members) in code.

Class Operations (Methods):


 Operations are shown in the third partition. They are services the class
provides.
 The return type of a method is shown after the colon at the end of the method
signature.
 The return type of method parameters are shown after the colon following the
parameter name. Operations map onto class methods in code

Class Visibility

The +, - and # symbols before an attribute and operation name in a class


denote the visibility of the attribute and operation.
 + denotes public attributes or operations
 - denotes private attributes or operations
 # denotes protected attributes or operations

Parameter Directionality

Each parameter in an operation (method) may be denoted as


in, out or inout which specifies its direction with respect to the caller.
This directionality is shown before the parameter name.

Perspectives of Class Diagram


The choice of perspective depends on how far along you are in the development
process. During the formulation of a domain model, for example, you would
seldom move past the conceptual perspective. Analysis models will typically
feature a mix of conceptual and specification perspectives. Design
model development will typically start with heavy emphasis on
the specification perspective, and evolve into the implementation
perspective.
A diagram can be interpreted from various perspectives:
 Conceptual: represents the concepts in the domain
 Specification: focus is on the interfaces of Abstract Data Type (ADTs) in the
software
 Implementation: describes how classes will implement their interfaces

The perspective affects the amount of detail to be supplied and the kinds of
relationships worth presenting. As we mentioned above, the class name is the
only mandatory information.

Relationships between classes


UML is not just about pretty pictures. If used correctly, UML precisely
conveys how code should be implemented from diagrams. If precisely
interpreted, the implemented code will correctly reflect the intent of the
designer. Can you describe what each of the relationships mean relative to
your target programming language shown in the Figure below?
If you can't yet recognize them, no problem this section is meant to help you
to understand UML class relationships. A class may be involved in one or more
relationships with other classes. A relationship can be one of the following
types:
Inheritance (or Generalization):

A generalization is a taxonomic relationship between a more general classifier


and a more specific classifier. Each instance of the specific classifier is
also an indirect instance of the general classifier. Thus, the specific
classifier inherits the features of the more general classifier.
 Represents an "is-a" relationship.
 An abstract class name is shown in italics.
 SubClass1 and SubClass2 are specializations of SuperClass.

The figure below shows an example of inheritance hierarchy. SubClass1 and


SubClass2 are derived from SuperClass. The relationship is displayed as a
solid line with a hollow arrowhead that points from the child element to the
parent element.
Inheritance Example - Shapes

The figure below shows an inheritance example with two styles. Although the
connectors are drawn differently, they are semantically equivalent.

Association

Associations are relationships between classes in a UML Class Diagram. They


are represented by a solid line between classes. Associations are typically
named using a verb or verb phrase which reflects the real world problem
domain.

Simple Association
 A structural link between two peer classes.
 There is an association between Class1 and Class2

The figure below shows an example of simple association. There is an


association that connects the <<control>> class Class1 and <<boundary>> class
Class2. The relationship is displayed as a solid line connecting the two
classes.
Cardinality

Cardinality is expressed in terms of:


 one to one
 one to many
 many to many

Aggregation

A special type of association.


 It represents a "part of" relationship.
 Class2 is part of Class1.
 Many instances (denoted by the *) of Class2 can be associated with Class1.
 Objects of Class1 and Class2 have separate lifetimes.

The figure below shows an example of aggregation. The relationship is


displayed as a solid line with a unfilled diamond at the association end,
which is connected to the class that represents the aggregate.
Composition
 A special type of aggregation where parts are destroyed when the whole is
destroyed.
 Objects of Class2 live and die with Class1.
 Class2 cannot stand by itself.

The figure below shows an example of composition. The relationship is


displayed as a solid line with a filled diamond at the association end, which
is connected to the class that represents the whole or composite.

Dependency

An object of one class might use an object of another class in the code of a
method. If the object is not stored in any field, then this is modeled as a
dependency relationship.
 A special type of association.
 Exists between two classes if changes to the definition of one may cause
changes to the other (but not the other way around).
 Class1 depends on Class2

The figure below shows an example of dependency. The relationship is displayed


as a dashed line with an open arrow.

The figure below shows another example of dependency. The Person class might
have a hasRead method with a Book parameter that returns true if the person
has read the book (perhaps by checking some database).
Realization

Realization is a relationship between the blueprint class and the object


containing its respective implementation level details. This object is said to
realize the blueprint class. In other words, you can understand this as the
relationship between the interface and the implementing class.
For example, the Owner interface might specify methods for acquiring property
and disposing of property. The Person and Corporation classes need to
implement these methods, possibly in very different ways.
Class Diagram Example: Order System

Class Diagram Example: GUI


A class diagram may also have notes attached to classes or relationships.

78. Write a code to build Stack data structure (LIFO)


79. Java Generics (C++ analogue: templates)
While building large-scale projects, we need the code to be compatible with any
kind of data which is provided to it. That is the place where your written code
stands above than that of others. Here what we meant is to make the code you
write be generic to any kind of data provided to the program regardless of its
data type. This is where Generics in Java and the similar in C++ named
Template comes in handy. While both have similar functionalities, but they differ
in a few places.
Template in C++
Writing Generic programs in C++ is called Templates.
1. One of the major features of the template in C++ is the usage of
metaprogramming. It Let the template signature of the provided code be
different, were C++ provides the ability to implement them.
2. Template arguments can be both classes and in functions.
3. C++ requires template sources to be added in their headers.
4. Template specialization could be achieved i.e, Specific type and method of
template can be implemented.

// CPP program to illustrate Templates

#include <iostream>

#include <string.h>

  

using namespace std;

  

template <class T>

class TempClass {

  

    T value;

  

public:
    TempClass(T item)

    {

        value = item;

    }

  

    T getValue()

    {

        return value;

    }

};

  

int main()

    class TempClass<string>* String = 

      new TempClass<string>("Generics vs Templates");

  

    cout << "Output Values: " << String->getValue() 

         << "\n";

  
    class TempClass<int>* integer = new TempClass<int>(9);

    cout << "Output Values: " << integer->getValue();

Output:
Output Values: Generics vs Templates
Output Values: 9
Generics in Java

1. One of the major features of Java Generics is that It handles type checking
during instantiation and generates byte-code equivalent to non-generic code.
The compiler of Java checks type before instantiation, that in turn makes the
implementation of Generic type-safe. Meanwhile, in C++, templates know
nothing about types.
2. If Generics is applied in a class, then it gets Applied to classes and methods
within classes.
3. Another major factor that leads to the use of generics in Java is because it
allows you to eliminate downcasts.
4. Instantiating a generic class has no runtime overhead over using an
equivalent class that uses as specific object rather than a generic type of T.

// Java program to illustrate

// Generics

public class GenericClass<T> {

private T value;

  

    public GenericClass(T value)


    {

        this.value = value;

    }

  

    public void showType()

    {

        System.out.println("Type:" + 

            value.getClass().getSimpleName());

        System.out.println("Value: " + value);

    }

  

    public static void main(String[] args)

    {

        GenericClass<String> Str = 

           new GenericClass<String>("Generics vs Templates");

  

        GenericClass<Integer> integer = 

                         new GenericClass<Integer>(9);

  
        Str.showType();

        integer.showType();

    }

Output:
Type:String
Value: Generics vs Templates
Type:Integer
Value: 9
C++ Templates vs Generics in Java
Though both of the methods to create a generic type is similar, but they vary at
some places, while the implementation property that they possess is the same.
1. Type erasure : Type erasure ensures tighter type check during compile
time. Java generics simply offer compile-time safety and eliminate the need
for casts. This is directly implemented in the Java compiler front-end and
make sure type erasure is done.
2. In C++ when you use a template the compiler will emit the template code
again after replacing the generic parameter in it with the type you used. This
is more powerful in several ways but can lead to bloated executables.
3. Wrapper class : In Java, Even if we have to specifically specify the datatype
within which the function call using any object, we don’t need to cast it
similar to that of C++ with actual data types, rather we use wrapper classes
to do the required.
4. Type Checking : Java Generics handles type checking during instantiation
and generates byte-code equivalent to non-generic code C++ has “latent
typing” and template metaprogramming and generates a new class for each
instantiation

80. Introduction to classes: getters, setters, methods


81. Classes and objects
Classes are structures to create your own compound data types.
▪ They consist of three parts:
▪ Constructors
▪ Methods
▪ Fields
▪ With help of classes you can create structures of any complexity, that
can be used
to simplify the logic of solution.
▪ Class is key part of Object Oriented Programming.
▪ Classes can be created using class keyword and its access modifier
(use public for
classes, if it is not an inner class)
▪ In class each element can have four different access modifiers:
▪ public – accessible everywhere
▪ protected – accessible in the same package and by subclasses
▪ private – accessible only inside the same class
▪ no modifiers – accessible in the same package

82. Java basics: (Data types, variables, conditions, loops, arrays)


83. Java basics: (Loops, arrays)
84. Polymorphism: upcasting, downcasting
Upcasting: Upcasting is the typecasting of a child object to a parent object.
Upcasting can be done implicitly. Upcasting gives us the flexibility to access
the parent class members but it is not possible to access all the child class
members using this feature. Instead of all the members, we can access some
specified members of the child class. For instance, we can access the
overridden methods.
Downcasting: Similarly, downcasting means the typecasting of a parent
object to a child object. Downcasting cannot be implicitly.
 Let there be a parent class. There can be many children of a parent. Let’s
take one of the children into consideration. The child inherits the properties of
the parent. Therefore, there is an “is-a” relationship between the child and
parent. Therefore, the child can be implicitly upcasted to the parent. However,
a parent may or may not inherits the child’s properties. However, we can
forcefully cast a child to a parent which is known as downcasting. After we
define this type of casting explicitly, the compiler checks in the background if
this type of casting is possible or not. If it’s not possible, the compiler throws
a ClassCastException.

85. What is your teacher’s name OOP (firstname, last name, patronymic
name), position :)
Beibut Amirgaliev, Associate Professor of the Information and
Communication Technologies Program, Director of the Department of
Science and Innovation of Astana IT University, is the best teacher of
the university in 2019.

You might also like