0% found this document useful (0 votes)
546 views

Realtime Java Interview Question PDF

The document discusses core Java interview questions and answers related to object-oriented programming concepts like class, object, inheritance, polymorphism, abstraction, encapsulation, interface, abstract class, and differences between HashMap vs HashTable and HashMap vs LinkedHashMap. It provides definitions and examples for each concept. It also discusses how to implement your own HashMap and ArrayList classes.

Uploaded by

sonam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
546 views

Realtime Java Interview Question PDF

The document discusses core Java interview questions and answers related to object-oriented programming concepts like class, object, inheritance, polymorphism, abstraction, encapsulation, interface, abstract class, and differences between HashMap vs HashTable and HashMap vs LinkedHashMap. It provides definitions and examples for each concept. It also discusses how to implement your own HashMap and ArrayList classes.

Uploaded by

sonam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 52

#Core #Java #interview #questions #Answers

Q.What is oops concept?

Ans-> Object-Oriented Programming is a methodology or paradigm to design a program using classes


and objects.

-> It simplifies the software development and maintainance by providing some concepts:

1. Object

2. Class

3. Inheritance

4. Polymorphism

5. Abstraction

6. Encapsulation

Object:

=======

-> Instance of a class is known as an Object whereas instance is nothing but allocating sufficient amount
of memory

space for the data members of a class.

-> Any entity that has state and behavior is known as an object.

-> For example: chair, pen, table, keyboard, bike etc.

-> It can be physical and logical.

Class:

======

-> The process of binding data-members and associated methods in a single unit is called class.

-> Collection of objects is nothing but class. It is logical entity.

Inheritance:

===========

-> When an object acquires all the properties and behaviors of parent class object.
i.e., known as inheritance. It provides code-reusability. It is used to achieve runtime polymorphism.

Polymorphism:

==============

-> When one task is performed by different ways. i.e., known as polymorphism.

-> The process of representing "one form in multiple forms" whereas One form represents

original method and it always resides in Base class. Multiple forms represent overriden

methods and they always resides in derived class.

Ex: void sum()------>BC void sum()----->DC1 void sum()--------->DC2

{ { {

a=10; b=20; f1=10.5f; f2=12.5f; c1='A'; c2='B';

S.o.p(a+b); S.o.p(f1+f2); S.o.p(c1+c2);

} } }

Ex: To convince the customer differently.

To draw something eg: shape or rectangle.

-> In java, we use method overloading and method overriding to achieve polymorphism.

-> Another example can be to speak something e.g. cat speaks meaw, dog barks woof etc.

Abstraction: (Security Purpose)

===========

-> Highlighting set of services what we are offering and hiding implementation details is nothing but
Abstraction. Example: ATM.

-> Hiding internal details and showing functionality is known as abstraction.

-> For example: phone call, we don't know the internal processing.

-> In java, we use abstract class and interface to achieve abstraction.

Encapsulation:

=============

-> Binding (or wrapping) code and data together into a single unit is known as encapsulation.
-> For example: capsule, it is wrapped with different medicines.

-> A java class is the example of encapsulation. Java bean is the fully encapsulated class

-> because all the data members are private here.

Q.What is Interface:

===================

-> 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.

-> Java Interface also represents IS-A relationship.

Why use Java interface?:

1. It is used to achieve abstraction.

2. By interface, we can support the functionality of multiple inheritance.

3. It can be used to achieve loose coupling.

Abstraction: (Security Purpose)

===========

-> Highlighting set of services what we are offering and hiding implementation details is nothing but
Abstraction. Example: ATM.

-> Hiding internal details and showing functionality is known as abstraction.

-> For example: phone call, we don't know the internal processing.

-> In java, we use abstract class and interface to achieve abstraction.

Interface

=================

1. If we don't know anything about implementation just we have requirement specification

then we should go for Interface.

2. Every methods inside Interface is always public and abstract.

3. We can't declare Interface methods with the following modifiers: private, protected,

static, final, synchronized, native and strictfp.

4. Every variable present inside Interface must be public, static, final whether we are declaring
or not.

5. We can't declare Interface variables with the following modifiers private, protected, transient

and volatile.

6. For Interface variables compulsory we should perform initialization at the time of declaration,

otherwise we will get compile time error.

7. Inside Interface we can't declare static and instance blocks.

8. Inside Interface we can't take constructor.

=====================================================================================
=====

Abstract Class

=============

1. If we are talking about implementation but not completely (partial implementation)

then we should go for Abstract class.

2. Every method present inside Abstract Class need not be public & abstract, we can

take concrete methods also.

3. There are no restrictions on abstract class method modifiers.

4. Every variable present inside Abstract Class need not be public static and final.

5. There are no restrictions on Abstract class variable modifiers.

6. For Abstract Class variables which is not required to perform initialization at the time of declaration.

7. Inside Abstract Class we can declare static and instance blocks.

8. Inside Abstract Class we can take constructor.

Q.What is difference between HashMap and HashTable?

================================================================================

Ans- HashMap Hashtable

HashMap

Hashtable

1. No method present inside HashMap is synchronized.


1. Every methods inside Hashtable is synchronized.

2. At a time multiple threads are allowed to operate on HashMap object simultaneously and hence it is
not a Threadsafe.

2. At a time only one thread is allowed to operate onHashtable object and hence it is Threadsafe.

3. Relatively Performance is high.

3. Relatively Performance is low.

4. Null is allowed for both keys and values.

4. Null is not allowed for both keys and values. Otherwisewe will get NullPointerException.

============================================================================

Q.What is difference between HashMap and LinkedHashMap?

HashMap LinkedHashMap

=====================================================================================

HashMap

LinkedHashMap

1. Underlying data structure is Hashtable.

1. Underlying data structure is combination of Hashtable Underlying data structure is combination. of


Hashtable

2. Insertion order is not preserved.

2. Insertion order is preserved.

3. Introduced in 1.2 version.

3. Introduced in 1.4 version.

IdentityHashMap

==================
-> It is exactly same as HashMap except the following difference.

-> In case of HashMap, JVM will use .equals(-) method to identify duplicate keys, which is meant for
content comparison.

-> But in case of IdentityHashMap, JVM will use == operator to identify duplicate keys which is meant for
reference comparison.

WeakHashMap

==================

-> It is exactly same as HashMap except the following difference.

-> In case of HashMap, if an object associated with HashMap then it is not eligible for garbage collection,

eventhough it doesn't contain any external references. i.e., HashMap dominates Garbage Collector.

-> But in case of WeakHashMap, if an object doesn't contain any references then it is always eligible for

Garbage Collector eventhough it is associated with WeakHashMap. ie., Garbage Collector dominates
WeakHashMap.

Q.How to implement your own HashMap?

***********

Ans- Hash Map is a implementaion of Map Interface.

HashMap works on the principal of hashing.

-> Map.Entry interface - This interface gives a map entry (key-value pair).

HashMap in Java stores both key and value object, in bucket,

as an object of Entry class which implements this nested interface Map.Entry.

-> hashCode() -HashMap provides put(key, value) for storing and get(key) method

for retrieving Values from HashMap. When put() method is used to store (Key, Value) pair,

HashMap implementation calls hashcode on Key object to calculate a hash

that is used to find a bucket where Entry object will be stored.

When get() method is used to retrieve value, again key object is used to calculate a hash

which is used then to find a bucket where that particular key is stored.

-> equals() - equals() method is used to compare objects for equality.


In case of HashMap key object is used for comparison, also using equals() method

Map knows how to handle hashing collision (hashing collision means more than

one key having the same hash value, thus assigned to the same bucket.

In that case objects are stored in a linked list,

Where hashCode method helps in finding the bucket where that key is stored,

equals method helps in finding the right key as there may be more than one key-value pair

stored in a single bucket.

** Bucket term used here is actually an index of array, that array is called table in HashMap

implementation. Thus table[0] is referred as bucket0, table[1] as bucket1 and so on.

So it is implementaion of HashMap

QHow to implement your own ArrayList?

************

Ans-> ArrayList is a Resizable-array of an Objects.

In Array we don't have the capability of dynamically increasing the size of the array

but in ArrayList we can, We know that ArrayList has initial capacity of the element

to be added in a list, so first things we need is default initial capacity.

second things we need an Array of Objects to store the element of the ArrayList.

->Define default initial capacity of the CustomArrayList.

-private static final int DEFAULT_INITIAL_CAPACITY = 5;

->Now declare an empty CustomArrayList which will return while creating empty CustomArrayList

-private static final Object[] EMPTY_ELEMENT_DATA = {};

->Define the size for the CustomeArrayList

-private int size;

->Now we need to define an Array of an Object to store the elements view sourceprint?

-private transient Object[] customArrayListElementData;


So it is implementaion of ArrayList.

Q. Synchronized keyword

=====================================================================================
====================================

Ans--> Synchronized modifier is applicable only for methods and blocks but not for classes and variables.

-> If multiple threads are operating simultaneously on same java object then there may be a chance of
data inconsistency problem.

-> To overcome this problem we should go for synchronized.

-> If a method or block declared as synchronized then at a time only one thread is allowed to operate
method or block on the given object.

-> So that we can resolve data inconsistency problem.

-> The main advantage of synchronized keyword is we can overcome data inconsistency problems.

-> But the main disadvantage is it increases waiting time of threads and creates performance problems.

-> Hence if there is no specific requirement then it is never recommanded to use synchronized keyword.

-> Synchronized method should compulsory contain implementation whereas abstract method should
not contain implementation.

-> Hence [abstract synchronized] combination is illegal for methods.

Q.4. Diff_Serialization&Externalization.

Ans-*****************************************

The process of converting system representation to network representation is called an marshalling.

The process of converting data from network representation to sysytem representation is called un
marshalling.

****************************************
Serialization:

================

the process of seprating data from an object is called serialization.

or

-> The process of writing state of an object to a file is called Serialization.

-> But strictly speaking, it is a process of converting an object from java supported form to either file
supprted form

or network supported form or database supported form is called Serialization.

-> By using FileOutputStream and ObjectOutputStream, we can achieve Serialization.

It is meant for default Serialization.

Here everything takes care by JVM and Programmer doesn't have any control.

In Serialization, total object will be serialized always & it is not possible to serialize part of the object.

Relatively performance is low.

Serialization is best choice if we want to save total objects to the file.

Serialization Interface doesn't contain any methods.

So it is called as Marker Interface.**

transient keyword will play role in Serialization.

Step to create the serialization:

-----------------------------

step1 create the serialized class and its object.

ex:- class employee inplements serializable{

int emp id=101;

String emp name=ram;

----

-----

step2 create fileOutput stream.


FileOutputStream fos=new fileoutputStream("abc.txt");

Step3 create object outputStream

for that we have to use the following constructor.

public objectoutputstream(FileOutPutStream fos)

ex: objectoutputStream oos=new objectoutputStream(fos);

step4 write serializable object in object outputstream:

we have to use this method for that.

public void writeobject(object obj)

ex: Employee emp=new employee();

oos.writeobject(emp);

Desirialization:

==================

The process of regenerating the object on the basis of data is called the deserialization.

Step to perform the des:

-------------------------------

step1 create fileInputStream.

ex: fileInputStream fis=new fileInputStream("abc.txt");

Step2 create objectInputStream:

to create objectInputStream we have to use the following constructor.

public objectInputStream(FileInputStream fis)

ex: objectInputStream ois=new objectInputStream(fis);


Step3 read des obj from objectInputStream:

for that we have to use the method.

public object readObject()

ex:

Employee emp=(Employee)ois.read object();

Externalization:

=================

It is meant for customized Serialization.

Here everything takes care by Programmer and JVM doesn't have any control.

In Externalization, based on our requirement we can save either total object or part of the object.

Relatively performance is high.

Externalization is best choice if we want to save part of the object to the file.

Externalization Interface contains 2 methods. 1. writeExternal(-) and 2. readExternal(-)

So it is not a Marker Interface.

transient keyword will not play any role in Externalization.

Marker Interface:

==================

-> Marker interface in Java is interfaces with no field or methods or in simple word empty interface in
java is called marker interface.

-> Example of marker interface is Serializable, Clonnable and Remote interface.

-> It looks they are used to indicate something to compiler or JVM.

-> So if JVM sees a class is Serializable it done some special operation on it,

similar way if JVM sees one class is implement Clonnable it performs some operation to support
cloning.

Same is true for RMI and Remote interface. So in short Marker interface indicate, signal or a
command to Compiler or JVM.
-> After introduction of Annotation on Java5, Annotation is better choice than marker interface and
JUnit is a perfect example of using Annotation.

-> In summary marker interface in Java is used to indicate something to compiler, JVM or any other tool
but Annotation is better way of doing same thing.

Q. equals&==operator.

Ans-1. What is difference between == operator and .equals() method?

== Opertor .equals() method

=====================================================================================

= = equals

.equlas

1. It is an operator, applicable for both premitives and Object types.

1. It is a method, applicable only for Object types but not premitives.

2. In general == operator meant for reference/address comparision.

2. in general .equals method is used for content comparision.

3. It is not possible to override == operator for content comparision.

3. It is possible to override equals(-) method for content comparision.

Note:**.equals method present in object class also meant for reference comparision only based on our
requirement we can override fro content comparision.

** In string class all wrapper class and all collection classes .equals() is overridden for content
comparision.

Q.Singleton Java class:

====================================

Ans-> The Java class that allows us to create only one object per JVM is called singleton java class.
-> Situations to take singleton java class:

a. If class is not having any state.

b. If class is having only sharable read only state (final variable).

c. If class is having huge amount of state and performing write operations on that state in
synchronized manner (1 Thread at a time).

Creation of our own Singleton class

=======================

-> We can create our own singleton class, by using

private constructor,

static variables,

static methods

we can implement singleton classes.

Example:

public class Test implements Cloneable

private static Test t;

private Test()

public static Test getInstance()

if(t==null)

t=new Test();

return t;

}
public Object clone()

return this;

Now ,

Test t1=Test.getInstance();

Test t2=Test.getInstance();

Test t3=Test.getInstance();

Test t100=Test.getInstance();

Like this, Only one object is created here with multiple references.

=====================================================================================
===

-> If Constructor is not private then outside person can create object directly by calling constructor.

In that case they can create multiple objects and also we will miss singleton behavior.

-> If 't' is not private then after creation of first object outside person can reassign 't' with null. In

that case a second new object will be created, whenever we call getInstance() method again.

Test t1=Test.getInstance();

Test.t=null;

Test t2=Test.getInstance();

-> If we are not overriding clone() method & Test class implements Cloneable then Object class

clone() method will be executed which provides always a separate new object.

Test t1=Test.getInstance();

Test t2=(Test)t1.clone();
=====================================================================================
===

Advantage of Singleton Class:

==========================

Instead of creating multiple objects we can run entire show with only one object but with multiple
references.

Hence the main advantage of singleton class is performance will be improved and we know object
creation is most precious.

Q.7. StringPool V.V.I

Ans--> There is string pool in java which is used for managing the memory efficiently and save the
money invested in the memory.

-> There are two types of declaring strings in java:

String Literal

String Object.

Example:

String s="ABC"; //String Literal

String s1="ABC"; //String Literal

String sq=new String("ABC"); //String Object

String sq1=new String("ABC"); //String Object

Both stm are use to create string object in java.

**

When we use String s="Abc" then string object will be created in the string constant pool SCP area. it is
one of memory in method area.

when String literal is created in java, JVM checks the string pool if the string content is already present
then again memory is not allocated.

So, It optimizes the use of memory.

String sq1=new String("ABC");

But this is not true of u create a string using Option 2. Then the String object is created on heap and
each time you create a string object,

a new object is created irrespective of its content.


So, same thing is not applies to String objects. String objects shown in example sq and sq1 occupy
different location in memory.

** Below are the hard requirements of an immutable object.

Make the class final

make all members final, set them explicitly, in a static block, or in the constructor

Make all members private

No Methods that modify state

Be extremely careful to limit access to mutable members(remember the field may be final but the object
can still be mutable.

ie private final Date imStillMutable). You should make defensive copies in these cases.

Q.8. Diff_yield,join&sleep.

Ans-

sleep()

========

-> If a thread doesn't want to perform any operation for a particular amount of time that is just

pausing is required then we should go for sleep() method.

-> sleep(n) says "I'm done with my timeslice, and please don't give me another one for at least n
milliseconds."

The OS doesn't even try to schedule the sleeping thread until requested time has passed.

-> It is static and final method.

-> It can be overloaded and throws InterruptedException also.

join()

========

-> If a thread wants to wait until completing other threads then we should go for join() method.

-> It is final method but not static method and native method.

-> It can be overloaded and throws InterruptedException.


yield()

=========

-> It causes to pause current executing thread to give the chance for remaining waiting threads of same
priority.

-> yield() says “I’m done with my timeslice, but I still have work to do.” The OS is free to immediately
give the

thread another timeslice, or to give some other thread or process the CPU the yielding thread just
gave up.

-> It is static method and native method but not final method.

-> It doesn't throw InterruptedException and it can't be overloaded.

wait()

-> wait() method releases the lock.

-> wait() is the method of Object class.

-> wait() is the non-static method.

-> wait() should be notified by notify() or notifyAll() methods.

Difference between wait and sleep method

===========================================

-> A wait can be "woken up" by another thread calling notify on the monitor which is being waited on

whereas a sleep cannot it can only be interrupted.

-> While sleeping a Thread does not release the locks it holds, while waiting releases the lock on the

object that wait() is called on.

-> sleep(n) says "I'm done with my timeslice, and please don't give me another one for at least n
milliseconds."

The OS doesn't even try to schedule the sleeping thread until requested time has passed.

.wait() says "I'm done my timeslice. Don't give me another timeslice until someone calls notify()."

-> we can normally use sleep() for time-synchronization and wait() for multi-thread-synchronization.

Q. Why we use super keyword?

=============================================================
Ans--> Whenever we inherit the base class features into derived class there is a possibility

that base class features are similar to derived class features.

-> Due to this, JVM get an ambiguity problem.

-> In order to differentiate between Base class features and Derived class features, for

Base class features must be always proceed by super keyword.

-> In other words, super is a keyword which is used for differentiate between Base class

features and Derived class features.

***

-> super keyword is playing an important role in three places. They are:

1. At variable level

2. At method level

3. At constructor level

Q.What is the difference between Checked and UnChecked Exception?

==============================================================

-> Checked Exception: The Exceptions which are checked by compiler for smooth execution of the
program at runtime are called Checked Exception.

-> Compiler will check whether we are handling checked exceptin or not. If we are not handling then we
will get compile time error.

For example: HallTicketMissingException, FileNotFoundException, IOException etc...

-> Unchecked Exception: The Exceptions which are not checked by compiler whether the programmer
handling or not are called Unchecked Exception.

RuntimeException and its child classes, Error and its child classes are unchecked except these the
remaining are checked exception.

For example: BombBlastException, ArithmeticException, NullPointerException etc...

-> Checked Exception is required to be handled by compile time while Unchecked Exception doesn't.

-> Checked Exception is direct sub-Class of Exception while Unchecked Exception are of
RuntimeException and Error.
Q.How can you create our own Exception?

===========================================================================

-> If you are creating our own Exception that is known as custom exception or user-defined exception.

-> Java custom exceptions are used to customize the exception according to user need.

-> By the help of custom exception, we can have our own exception and message.

-> Let's see a simple example of java custom exception.

class TooYoungException extends RuntimeException

TooYoungException(String s)

super(s);

class CustomExceptionTest

public static void main(String args[])

int age=17;

if(age<18)

throw new TooYoungException("You are too Young..Just wait for "+(18-age)+" years");

else

System.out.println("You are eligible to vote.");

Q. What is the difference between length and length()?

==========================================================================
-> length is a final variable, applicable only for arrays.

whereas length() is a method, applicable only for String object.

-> length variable represents the size of array.

whereas length() returns the number of characters present in String.

Q.13.ClassNotFound & ClassNoDefFound.

Ans-

-> It is an exception. It is of type java.lang.Exception.

where as It is an error. It is of type java.lang.Error.

-> It occurs when an application tries to load a class at run time which is not updated in the classpath.

where as It occurs when java runtime system doesn’t find a class definition,which is present at compile
time, but missing at run time.

-> It is thrown by the application itself. It is thrown by the methods like Class.forName(),

loadClass() and findSystemClass().

where as It is thrown by the Java Runtime System.

-> It occurs when classpath is not updated with required JAR files.

where as It occurs when required class definition is missing at runtime.

** ClassNotFoundException *

Ex.

public class MainClass

public static void main(String[] args)

try

Class.forName("oracle.jdbc.driver.OracleDriver");

}catch (ClassNotFoundException e)
{

e.printStackTrace();

*** NoClassDefFoundError ****

Ex....

class A

// some code

public class B

public static void main(String[] args)

A a = new A();

Q.** What is immutable class..??

=================================

-> Immutable class is a class which once created, it’s contents can not be changed.

-> Immutable objects are the objects whose state can not be changed once constructed.

e.g. String class.

-> All wrapper classes in java.lang are immutable –

String, Integer, Boolean, Character, Byte, Short, Long, Float, Double.


* Why use immutable class..??

=====================================

-> Because java uses the concept of string literal. Suppose there are 5 reference variables,all referes to
one object "sachin".

If one reference variable changes the value of the object, it will be affected to all the reference
variables.

That is why string objects are immutable in java.

-> In case of String object, just because of SCP a single object is referred by multiple references.

by using one reference if we are allowed to change the content then remaining refereces will be
impacted.

to overcome this problem some people made string object as immutable.

....

-> But in case of StringBuffer for every requirment a separate object will be created.

by using one reference if we are allowed to change the content then remaining refereces won't be
impacted.

hence immutability concept is not required for StringBuffer object.

***How to create an immutable class???

=============================================

-> Create a final class.

Set the values of properties using constructor only.

Make the properties of the class final and private.

Do not provide any setters for these properties.

If the instance fields include references to mutable objects, don't allow those objects to be changed:

Don't provide methods that modify the mutable objects.

Don't share references to the mutable objects.

Never store references to external, mutable objects passed to the constructor;


if necessary, create copies, and store references to the copies. Similarly,

create copies of your internal mutable objects when necessary to avoid returning the originals in your
methods.

-> Ex..

public final class FinalPersonClass {

private final String name;

private final int age;

public FinalPersonClass(final String name, final int age) {

this.name = name;

this.age = age;

public int getAge() {

return age;

public String getName() {

return name;

Q.String & StringBuffer & StringBuilder.

Ans-

** String **

-> String is immutable ( once created can not be changed )object.

The object created as a String is stored in the Constant String Pool .

Every immutable object in Java is thread safe ,that implies String is also thread safe .

String can not be used by two threads simultaneously.

String once assigned can not be changed.


** StringBuffer *

-> StringBuffer is mutable means one can change the value of the object .

The object created through StringBuffer is stored in the heap .

StringBuffer has the same methods as the StringBuilder ,

but each method in StringBuffer is synchronized that is StringBuffer is thread safe .

** StringBuilder **

-> StringBuilder is same as the StringBuffer ,

that is it stores the object in heap and it can also be modified .

The main difference between the StringBuffer and StringBuilder is that StringBuilder is also not thread
safe.

StringBuilder is fast as it is not thread safe .

Q.Reflection API:

=================================

-> Reflection API is used for reading a class at execution time, loading its byte code

and creating its object and further calling its datamembers and member methods.

-> Reflection API contains all information about our class that is what types of modifiers,

constructors, inner classes, methods etc...

-> Reflection acts as Mirror

Q.19 Why String class is final .

Ans-5 Reasons of Why String is final or Immutable in Java

Why String class is made Immutable or Final in JavaThough true reasons of why String class was made
final is best known to Java designers,

and apart from that hint on security by James Gosling, I think following reasons also suggest Why
String is Final or Immutable in Java.

1) String Pool
Java designer knows that String is going to be most used data type in all kind of Java applications and
that's

why they wanted to optimize from start. One of key step on that direction was idea of storing String
literals in String pool.

Goal was to reduce temporary String object by sharing them and in order to share, they must have to be
from Immutable class.

You can not share a mutable object with two parties which are unknown to each other.

Let's take an hypothetical example, where two reference variable is pointing to same String object:

String s1 = "Java";

String s2 = "Java";

Now if s1 changes the object from "Java" to "C++", reference variable also got value s2="C++", which it
doesn't even know about it.

By making String immutable, this sharing of String literal was possible. In short,

key idea of String pool can not be implemented without making String final or Immutable in Java.

2) Security

Java has clear goal in terms of providing a secure environment at every level of service and String is
critical in those whole security stuff.

String has been widely used as parameter for many Java classes, e.g. for opening network connection,

you can pass host and port as String, for reading files in Java you can pass path of files and directory as
String and for opening database connection,

you can pass database URL as String. If String was not immutable, a user might have granted to access a
particular file in system,

but after authentication he can change the PATH to something else, this could cause serious security
issues.

Similarly, while connecting to database or any other machine in network, mutating String value can pose
security threats.

Mutable strings could also cause security problem in Reflection as well, as the parameters are strings.

3) Use of String in Class Loading Mechanism

Another reason for making String final or Immutable was driven by the fact that it was heavily used in
class loading mechanism.

As String been not Immutable, an attacker can take advantage of this fact and a request to load standard
Java classes
e.g. java.io.Reader can be changed to malicious class com.unknown.DataStolenReader. By keeping String
final and immutable,

we can at least be sure that JVM is loading correct classes.

4) Multithreading Benefits

Since Concurrency and Multi-threading was Java's key offering, it made lot of sense to think about
thread-safety of String objects.

Since it was expected that String will be used widely, making it Immutable means no external
synchronization,

means much cleaner code involving sharing of String between multiple threads.

This single feature, makes already complicate, confusing and error prone concurrency coding much
easier.

Because String is immutable and we just share it between threads, it result in more readable code.

5) Optimization and Performance

Now when you make a class Immutable, you know in advance that, this class is not going to change once
created.

This guarantee open path for many performance optimization e.g. caching. String itself know that, I am
not going to change,

so String cache its hashcode. It even calculate hashcode lazily and once created, just cache it. In simple
world,

when you first call hashCode() method of any String object, it calculate hash code and all subsequent
call to hashCode() returns already calculated,

cached value. This results in good performance gain, given String is heavily used in hash based Maps e.g.

Hashtable and HashMap. Caching of hashcode was not possible without making it immutable and final,
as it depends upon content of String itself.

Q.Difference between Array and ArrayList ?

Throw

Throws

1. Java throw keyword is used to explicitly throw an exception.

1. Java throws keyword is used to declare an exception

2. Checked exception cannot be propagated using throw only.


2. Checked exception can be propagated with throws.

3. Throw is followed by an instance.

3. Throws is followed by class.

4. Throw is used within the method.

5. You cannot throw multiple exceptions.

4. Throws is used with the method signature.

5. You can declare multiple exceptions e.g.

public void method()throws IOException,SQLException.

Q.Difference between method overloading and method overriding in java ?

Method Overloading

Method Overriding

1) Method Overloading occurs with in the same Method Overriding occurs between

class two classes superclass and subclass

2) Since it involves with only one class inheritance

Since method overriding occurs between superclass and subclass inheritance is involved.

is not involved.

3)In overloading return type need not be the same

3) In overriding return type must be same.

4) Parameters must be different when we do

4) Parameters must be same.

overloading

5) Static polymorphism can be acheived using

5) Dynamic polymorphism can be acheived using method overriding.

method overloading

6) In overloading one method can’t hide the


6) In overriding subclass method hides that of the superclass method.

another
Pros and Cons of String being Immutable or Final in Java

---------------------------------------------------------------

Apart from above benefits, there is one more advantage that you can count due to String being final in
Java.

It's one of the most popular object to be used as key in hash based collections e.g. HashMap and
Hashtable.

Though immutability is not an absolute requirement for HashMap keys, its much more safe to use
Immutable object as key than mutable ones,

because if state of mutable object is changed during its stay inside HashMap, it would be impossible to
retrieve it back,

given it's equals() and hashCode() method depends upon the changed attribute. If a class is Immutable,
there is no risk of changing its state,

when it is stored inside hash based collections. Another significant benefits, which I have already
highlighted is its thread-safety.

Since String is immutable, you can safely share it between threads without worrying about external
synchronization.

It makes concurrent code more readable and less error prone.

Despite all these advantages, Immutability also has some disadvantages, e.g. it doesn't come without
cost.

Since String is immutable, it generates lots of temporary use and throw object, which creates pressure
for Garbage collector.

Java designer has already thought about it and storing String literals in pool is their solution to reduce
String garbage.

It does help, but you have to be careful to create String without using constructor e.g. new String() will
not pick object from String pool.

Also on average Java application generates too much garbage. Also storing Strings in pool has a hidden
risk associated with it.

String pool is located in PermGen Space of Java Heap, which is very limited as compared to Java Heap.

Having too many String literals will quickly fill this space, resulting in java.lang.OutOfMemoryError:
PermGen Space. Thankfully,

Java language programmers has realized this problem and from Java 7 onwards, they have moved String
pool to normal heap space,
which is much much larger than PermGen space. There is another disadvantage of making String final, as
it limits its extensibility.

Now, you just can not extend String to provide more functionality, though more general cases its hardly
needed,

still its limitation for those who wants to extend java.lang.String class.

Q.Final vs Finally vs Finalize.

Ans-Final--> it is modifier applicable for classes,methods,and variables.

Class----> if will declae the class as final then we cannot extend that class.or say we cant create child
class for that.

Variable--->That will be constant and we canot perform re-assignment for that variable.

Methods----> that method we cant overdide in child class.

Q. Difference between ArrayList and LinkedList ?

ArrayList

LinkedList

1.For retrieval operation ArrayList is

preferable

1.For Middle insertion or middle modification

LinkedList is preferable

2.Memory shifting or Memory management is

complicated

2.Memory shifting or Memory management is

not required

3. ArrayList internally uses dynamic

array to store the elements.

3. LinkedList internally uses doubly

linked list to store the elements.

Q.Difference between Array and ArrayList ?


Array

ArrayList

1. Array is not Grow able in nature.

1.ArrayList is grow able in nature

2.It allow only homogeneous object

2.It allow both homogeneous and

heterogeneous object

3.Inbuilt methods are not there in Array

3.Inbuilt methods are there provided by utile

class

4.Performance wise Array is best

5.Memory wise Array is not good

4.Performance wise ArrayList is preferable

5.Memory wise ArrayList is not good in comparison Array

Q. How can we take list into map?

Example: public Map<List<String>, List<Double>> getData() { Map<List<String>, List<Double>> dataMap


= new HashMap<>();

List<String> l1Key = new ArrayList<>();

l1Key.add("Basant");

l1Key.add("Babul");

List<String> l2Key = new ArrayList<>();

l2Key.add("Manoj"); l2Key.add("Amit"); l2Key.add("Saroj");

List<Double> l1Value = new ArrayList<>();

l1Value.add(50.000);
l1Value.add(40.000);

List<Double> l2Value = new ArrayList<>();

l2Value.add(90.000); l2Value.add(60.000); l2Value.add(30.000);

dataMap.put(l1Key, l1Value); dataMap.put(l2Key, l2Value); return dataMap;

Q. How can we take Map into List?

Example:

public List<Map<String, Integer>> setData() {

List<Map<String, Integer>> listMap = new ArrayList<>(); Map<String, Integer> map = new HashMap<>();

map.put("A", 34);

map.put("D", 90); map.put("B", 12); map.put("C", 91); listMap.add(map);

return listMap;

}
=====================================================================================
=================

Q.RequestDispatcherVsSendRedirect.

Ans-requestDispatcher - forward() method

====================================

-> When we use forward method, request is transfer to other resource within the same server for
further processing.

-> In case of forward, web container handle all process internally and client or browser is not involved.

-> When forward is called on requestdispatcher object we pass request and response objects so our old
request object

is present on new resource which is going to process our request.

-> Visually we are not able to see the forwarded address, it is transparent.

-> Using forward () method is faster than send redirect.

-> When we redirect using forward and we want to use same data in new resource we can use
request.setAttribute () as we have request object available.

SendRedirect

====================================

-> In case of sendRedirect, request is transfer to another resource to different domain or different server
for further processing.

-> When you use sendRedirect, container transfers the request to client or browser so URL given inside
the sendRedirect method

is visible as a new request to the client.

-> In case of sendRedirect call, old request and response objects are lost because it’s treated as new
request by the browser.

-> In address bar, we are able to see the new redirected address. It’s not transparent.

-> sendRedirect is slower because one extra round trip is required, because completely new request is
created and old request object is lost.

Two browser request required.

-> But in sendRedirect, if we want to use we have to store the data in session or pass along with the URL.

sWhich one is good?


=====================

-> Its depends upon the scenario that which method is more useful.

-> If you want control is transfer to new server or context and it is treated as completely new task then
we go for Send Redirect.

Generally, forward should be used if the operation can be safely repeated upon a browser reload of
the web page will not affect the result.

Q.What are the difference between ServletConfig and ServletContext Object?

=====================================================================================

-> The ServletConfig parameters are specified for a particular servlet and are unknown to other servlets.

-> It is used for initialising purpose.

-> The ServletContext parameters are specified for an entire application outside of any particular servlet

and are available to all the servlets within that application.

-> So we can say ServletConfig object is one per Servlet class.

-> ServletContext object is global to entire web application.

-> Object of ServletConfig will be created during initialization process of the servlet.

-> Object of ServletContext will be created at the time of web application deployment.

-> In web.xml, <init-param> tag will be appear under <servlet-class> tag.

-> In web.xml, <context-param> tag will be appear under <web-app> tag.

-> ServletConfig object will be destroyed once the servlet execution is completed.

-> ServletContext object will be destroyed once the application is removed from the server.

Q.WebServer&AppServer.

Ans- Web Server vs Application Server

=====================================================================================

-> I have web application and I have to run web application , Web Server is required.

-> WebServer provides environment to run only Web application.

-> Web Server can provide support for Servlet, JSP, Html, etc...

it support servlet and jsp only.

-> I have enterprise application and i have to run enterprise application, Application Server is required.

-> Application Server provides environment to run both web application and enterprise application.
-> Application Server is superior server. Every Application Server contain inbuilt Web server to provide

the support for Web application.

-> Any technology, we can use to develop enterprise application then we can go for Application Server.

For ex: Servlet, Jsp, Html, + EJB, JMS etc...

-> J2EE compatible server is Application Server.

it support distributed txn and EjB.

[7:03 pm, 22/11/2019] Jay: Hi Team,

#Microservices #project #interview #qustions #Answers

Q.#What Do You Mean by End-To-End Testing of Microservices?

Ans-End-to-end testing validates all the processes in the workflow to check if everything is working as
expected. It also ensures that the system works in a unified manner, thereby satisfying the business
requirement.

Q.#Why Do People Hesitate to Use Microservices?

I have seen many devs fumble over this question. After all, they're getting asked this question when
interviewing for a microservices architect role, so acknowledging its cons can be a little tricky. Here are
some good answers:

They require heavy investment – Microservices demand a great deal of collaboration. Since your teams
are working independently, they should be able to synchronize well at all times.

They need heavy architecture set up – The system is distributed, the architecture is heavily involved.

They need excessive planning for handling operations overhead – You need to be ready for operations
overhead if you are planning to use a microservices architecture.

They have autonomous staff selection – Skilled professionals are needed who can support microservices
that are distributed heterogeneously

Q.#Why Would You Need Reports and Dashboards in Microservices?


Reports and dashboards are mainly used to monitor and upkeep microservices. There are multiple tools
that help to serve this purpose. Reports and dashboards can be used to:

Find out which microservices expose what resources.

Find out the services which are impacted whenever changes in a component occur.

Provide an easy point which can be accessed whenever documentation is required.

Versions of the components which are deployed.

To obtain a sense of maturity and compliance from the components.

Q.#Why Would You Opt for Microservices Architecture?

This is a very common microservices interview question which you should be ready for! There are plenty
of pros that are offered by a microservices architecture. Here are a few of them:

Microservices can adapt easily to other frameworks or technologies.

Failure of a single process does not affect the entire system.

Provides support to big enterprises as well as small teams.

Can be deployed independently and in relatively less time.

Q.#How Can You Set Up Service Discovery?


There are multiple ways to set up service discovery. I’ll choose the one that I think to be most efficient,
Eureka by Netflix. It is a hassle free procedure that does not weigh much on the application. Plus, it
supports numerous types of web applications.

Eureka configuration involves two steps – client configuration and server configuration.

Client configuration can be done easily by using the property files. In the clas spath, Eureka searches for
a eureka-client.properties file. It also searches for overrides caused by the environment in property files
which are

environment specific.

For server configuration, you have to configure the client first. Once that is done, the server fires up a
client which is used to find other servers. The Eureka server, by default, uses the Client configuration to
find the peer server.

Q.#Role of Actuator in Spring Boot

It is one of the most important features, which helps you to access the current state of an application
that is running in a production environment. There are multiple metrics which can be used to check the
current state. They also provide endpoints for RESTful web services which can be simply used to check
the different metrics.

1. What Is Spring Cloud?

Spring Cloud, in microservices, is a system that provides integration with external systems. It is a short-
lived framework that builds an application, fast. Being associated with the finite amount of data
processing, it plays a very important role in microservice architectures.

For typical use cases, Spring Cloud provides the out of the box experiences and a sets of extensive
features mentioned below:

Versioned and distributed configuration.

Discovery of service registration.


Service to service calls.

Routing.

Circuit breakers and load balancing.

Cluster state and leadership election.

Global locks and distributed messaging.

2. What Is Spring Boot?

Spring boot is a major topic under the umbrella of microservices interview questions.

With the new functionalities that have been added, Spring keeps getting more complex. Whenever you
are starting a new project, it is mandatory to add a new build path or Maven dependencies. In short, you
will need to do everything from scratch. Spring Boot is the solution that will help you to avoid all the
code configurations.

3. How Do You Override a Spring Boot Project’s Default Properties?

This can be done by specifying the properties in the application.properties file.

For example, in Spring MVC applications, you have to specify the suffix and prefix. This can be done by
entering the properties mentioned below in the application.properties file.

For suffix – spring.mvc.view.suffix: .jsp

For prefix – spring.mvc.view.prefix: /WEB-INF/

4. Role of Actuator in Spring Boot


It is one of the most important features, which helps you to access the current state of an application
that is running in a production environment. There are multiple metrics which can be used to check the
current state. They also provide endpoints for RESTful web services which can be simply used to check
the different metrics.

Q. #How Is Spring Security Implemented In a Spring Boot Application?

Minimal configuration is needed for implementation. All you need to do is add thespring-boot-starter-
securitystarter in the pom.xml file. You will also need to create a Spring config class that will override
the required method while extending the WebSecurityConfigurerAdapter to achieve security in the
application. Here is some example code:

package com.gkatzioura.security.securityendpoints.config;

import org.springframework.context.annotation.Configuration;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;

import
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration

public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override

protected void configure(HttpSecurity http) throws Exception {

http.authorizeRequests() .antMatchers("/welcome").permitAll() .anyRequest().authenticated() .and()


.formLogin() .permitAll() .and() .logout() .permitAll();

Which Embedded Containers Are Supported by Spring Boot?


Whenever you are creating a Java application, deployment can be done via two methods:

Using an application container that is external.

Embedding the container inside your jar file.

Spring Boot contains Jetty, Tomcat, and Undertow servers, all of which are embedded.

Jetty – Used in a wide number of projects, Eclipse Jetty can be embedded in framework, application
servers, tools, and clusters.

Tomcat – Apache Tomcat is an open source JavaServer Pages implementation which works well with
embedded systems.

Undertow – A flexible and prominent web server that uses small single handlers to develop a web
server.

Q.#How Does PACT Work?

PACT is an open source tool. It helps in testing the interactions between consumers and service
providers. However, it is not included in the contract, increasing the reliability of the application. The
consumer service developer starts by writing a test which defines a mode of interaction with the service
provider. The test includes the provider’s state, the request body, and the response that is expected.
Based on this, PACT creates a stub against which the test is executed. The output is stored in a JSON file.

Q. #configure #Oracle #DataSource in #Tomcat 9.

Ans-1. #Oracle datasource setup

1. Download Oracle JDBC driver

Location to download ORACLE JDBC DRIVER (ojdbc6.jar)

Copy connector jar to either TOMCAT/LIB or application’s WEB-INF/lib

Note
In an enterprise application avoid adding it in application’s WEB-INF/lib. Mostly this connector will be
configured at server level so that same version will be shared among multiple applications. It helps in
upgrading jar at server without touching the application.

or use maven entry in pom.xml

Create C:\Users\window user\.m2\settings-security.xml

E:\programs\apache-maven-3.3.9\bin>mvn -emp password

Create C:\Users\window user\.m2\settings.xml

E:\programs\apache-maven-3.3.9\bin>mvn -ep password

Refere Oracle JDBC Driver Maven for details

<dependencies>

<dependency>

<groupId>com.oracle.jdbc</groupId>

<artifactId>ojdbc7</artifactId>

<version>12.1.0.2</version>

</dependency>

<dependency>

<groupId>com.oracle.jdbc</groupId>

<artifactId>ucp</artifactId>

<version>12.1.0.2</version>

</dependency>

</dependencies>

<repositories>

<repository>

<id>maven.oracle.com</id>

<name>oracle-maven-repo</name>

<url>https://maven.oracle.com</url>

<layout>default</layout>
<releases>

<enabled>true</enabled>

<updatePolicy>always</updatePolicy>

</releases>

</repository>

</repositories>

<pluginRepositories>

<pluginRepository>

<id>maven.oracle.com</id>

<name>oracle-maven-repo</name>

<url>https://maven.oracle.com</url>

<layout>default</layout>

<releases>

<enabled>true</enabled>

<updatePolicy>always</updatePolicy>

</releases>

</pluginRepository>

</pluginRepositories>

2. Change context.xml to create Oracle DB datasource in TOMCAT

a. Add Server to Eclipse. Open /Servers/Tomcat v9.0 Server at XXXXXX/context.xml

b. or if you are using direct tomcat without eclipse then change /tomcat_install_dir/context.xml

c. or create META-INF/context.xml in your application

Then add ‘Resource’ to context.xml

Tomcat is installed as same server as Oracle DB

Tomcat 8 and 9 uses org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory which uses maxTotal,


maxWaitMillis
Tomcat is installed as same server as Oracle DB

Tomcat 8 and 9 uses org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory which uses maxTotal,


maxWaitMillis

<Resource name="jdbc/javausecasedb" auth="Container" type="javax.sql.DataSource"

maxTotal="50" maxIdle="30" maxWaitMillis="10000"

username="javausecasedbuser" password="javausecasedbpassword"

driverClassName="oracle.jdbc.driver.OracleDriver"

url="jdbc:oracle:thin:@localhost:1521:xe"/>

Tomcat is not installed as same server as Oracle DB

<Resource name="jdbc/javausecasedb" auth="Container" type="javax.sql.DataSource"

maxTotal="50" maxIdle="30" maxWaitMillis="10000"

username="javausecasedbuser" password="javausecasedbpassword"

driverClassName="oracle.jdbc.driver.OracleDriver"

url="jdbc:oracle:thin:@DB_SERVER:1521:xe"/>

Note

maxTotal – Maximum number of database connections in pool. Make sure you configure your
max_connections large enough to handle all of your db connections. Set to -1 for no limit.

Connecting to shared DB in DEV environment in an organization do not use more than 1 or 2 total
connections.

maxIdle: Maximum number of idle database connections to retain in pool. Set to -1 for no limit. See also
the DBCP documentation on this nd the minEvictableIdleTimeMillis configuration parameter.

Connecting to shared DB in DEV environment in an organization do not use more than 1 or 2 idle
connections.

maxWaitMillis: Maximum time to wait for a database connection to become available in ms, in this
example 10 seconds. An Exception is thrown if this timeout is exceeded. Set to -1 to wait indefinitely.
Tomcat 7 and below uses org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory which uses maxActive,
maxWait

<Resource name="jdbc/javausecasedb" auth="Container" type="javax.sql.DataSource"

maxActive="50" maxIdle="30" maxWait="10000"

username="javausecasedbuser" password="javausecasedbpassword"

driverClassName="oracle.jdbc.driver.OracleDriver"

url="jdbc:oracle:thin:@localhost:1521:xe"/>

Note

maxActive – (int) The maximum number of active connections that can be allocated from this pool at the
same time. The default value is 100

Connecting to shared DB in DEV environment in an organization do not use more than 1 or 2 active
connections.

maxIdle – (int) The maximum number of connections that should be kept in the pool at all times. Default
value is maxActive:100 Idle connections are checked periodically (if enabled) and connections that been
idle for longer than minEvictableIdleTimeMillis will be released. (also see testWhileIdle)

Connecting to shared DB in DEV environment in an organization do not use more than 1 or 2 idle
connections.

initialSize – (int)The initial number of connections that are created when the pool is started. Default
value is 10

Connecting to shared DB in DEV environment in an organization do not use more than 1 or 2 initial
connections.

3. Change web.xml, This is optional step. Rather if you are going to deploy it to Websphere in Production
environment do not add this entry.

<resource-ref>

<description>Oracle Datasource example</description>

<res-ref-name>jdbc/javausecasedb</res-ref-name>

<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>

</resource-ref>

4. You can even create Oracle DB datasource through spring DBCP configuration. This is useful for
testing application without any server.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"

destroy-method="close">

<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>

<property name="url" value="jdbc:oracle:thin:@DB_SERVER_IP:1521:xe"/>

<property name="username" value="javausecasedbuser"/>

<property name="password" value="javausecasedbpassword"/>

</bean>

Create Oracle DB datasource through spring DBCP2

Apache Commons DBCP 2.1.1 for JDBC 4.1 (Java 7.0+)

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"

destroy-method="close">

<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>

<property name="url" value="jdbc:oracle:thin:@DB_SERVER_IP:1521:xe"/>

<property name="username" value="javausecasedbuser"/>

<property name="password" value="javausecasedbpassword"/>

</bean>

Create Oracle DB datasource through java code

BasicDataSource bds = new BasicDataSource();

bds.setDriverClassName("oracle.jdbc.driver.OracleDriver");

bds.setUrl("jdbc:oracle:thin:@localhost:1521:xe");

bds.setUsername("javausecasedbuser");

bds.setPassword("javausecasedbpassword");
bds.setInitialSize(5);

2. Use it in your application

1. Once datasource is configured on tomcat access it through Spring configuration

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/javausecasedb" />

2. To access Oracle DB through initial context

import javax.naming.Context;

import javax.naming.InitialContext;

..

Context ctx = new InitialContext();

DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/javausecasedb");

Notice java:comp/env

The InitialContext is configured as a web application is initially deployed, and is made available to web
application components (for read-only access). All configured entries and resources are placed in the
java:comp/env portion of the JNDI namespace.

3. To access Oracle DB through @Resource (javax.annotation.Resource)

package com.javausecase;

import java.io.IOException;

import java.sql.Connection;

import javax.annotation.Resource;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.sql.DataSource;

@WebServlet("/Servlet2")

public class Servlet2 extends HttpServlet {

private static final long serialVersionUID = 1L;

@Resource(name = "jdbc/javausecasedb")

private DataSource ds;

public Servlet2() {

super();

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

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

Connection conn = null;

try {

// check datasource

System.out.println("ds=" + ds);

conn = ds.getConnection();

System.out.println("ds.getConnection()=" + conn);

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

conn.close();

} catch (Exception e) {
e.printStackTrace();

response.getWriter().append("Served at: ").append(request.getContextPath());

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

[7:03 pm, 22/11/2019] Jay: Hey guys

I want to ask

How to Make a program that takes one number from user for Ex (a) and

-print " this number is little " when (a) is little than or equal 10

-print " this number is medium "

when (a) is greater than 10 but little than or equal 20

-print " this number is great "

when (a) is greater than 20

Ps : I’m still learning java level 2 using netbeans

Any help please😃

[7:03 pm, 22/11/2019] Jay:


https://www.facebook.com/groups/210711873141145/permalink/402629810616016/

[7:03 pm, 22/11/2019] Jay: Git cmnd

[7:03 pm, 22/11/2019] Jay: Why Default method is defined in Interface?

Up to java 7 , interface can only contain abstract methods i.e. we can only declare the method but can't
define the method. So the class that implements the interface must have to override every method of
that interface in the class .
Ex:

package com.sudhir.java8;

public interface SocialNetworkingApi {

void post();

void comment();

package com.sudhir.java8;

public class FaceBookApi implements SocialNetworkingApi{

public void post() {

System.out.println("provide your photo ");

public void comment() {

System.out.println("provide your comment");

package com.sudhir.java8;

public class WhatsappApi implements SocialNetworkingApi{

public void post() {


System.out.println("Please provide your photo");

public void comment() {

System.out.println("please provide your comment in short ");

suppose SocialNetworkingApi (interface) in future wants to implement new features named as


shareStatus().

Problem: Now the problem will be if SocialNetworkingApi will declare the new method shareStatus() ,
then FaceBookApi and WhatsappApi must have to override shareStatus() method because abstarct
method must have to be overriden in subclass otherwise it will throw error , which may result in
business loss because their customer will suffer.

So Java 8 introduced new keyword (default) which allows a method, present in an interface, can have
logic . Now subclass is not forced to define those methods inside the class. If they want any custom logic
they can define otherwise they will call the default method .So no business will suffer.

Java8 code:

package com.sudhir.java8;

public interface SocialNetworkingApi {

void post();

void comment();

default void shareStatus() {

System.out.println("please share your status");

};
}

package com.sudhir.java8;

public class FaceBookApi implements SocialNetworkingApi{

public void post() {

System.out.println("provide your photo ");

public void comment() {

System.out.println("zprovide your comment");

public static void main(String[] args) {

SocialNetworkingApi facebook = new FaceBookApi();

facebook.post();

facebook.comment();

//facebook.shareStatus(); Comment: here we can observe like if i am not implementing shareStaus()


method also no issues if i want i can call method directly.

o/p: provide your photo

provide your comment

package com.sudhir.java8;
public class WhatsappApi implements SocialNetworkingApi{

public void post() {

System.out.println("Please provide your photo");

public void comment() {

System.out.println("please provide your comment in short ");

// Override the method to give own logic

public void shareStatus() {

System.out.println("Please share your status in whatsapp");

public static void main(String[] args) {

SocialNetworkingApi whatsapp = new WhatsappApi();

whatsapp.post();

whatsapp.comment();

whatsapp.shareStatus();

o/p: Please provide your photo

please provide your comment in short

Please share your status in whatsapp

Conculsion: Interface can contain subclass child object also with method defintion.

You might also like