Monday 23 January 2017

Will this code give error if i try to add two heterogeneous elements in the Arraylist. ? and Why ?

public class Check {

       public static void main(String[] args) {

              List list = new ArrayList<>();
//If we don't declare the list to be of specific type, it treats it as list of objects.
              list.add(2);
              list.add("2");
              System.out.println(list);
// int 2 is auto boxed to Integer and "2" is String and hence both are objects.
              Iterator iter = list.iterator();

              while (iter.hasNext()) {

                     Object object = (Object) iter.next();

                     System.out.println(object);
                     //output is 2 and 2

              }

       }




 output  will be 
[2, 2]
2
2

Saturday 21 January 2017

Immutable Class in Java

Immutable

Immutable objects are those objects whose state can not be changed once created

Ex- String, Boolean, Byte, Short, Integer, Long, Float, Double etc. Means, all the wrapper classes and String class is immutable.

Please Be careful, there is difference between final and Immutable , people always do mistake here in interviewer ask question in details.


Difference between  final & immutable 
final means that you can't change the object's reference to point to another reference. Where immutable means that actual object's value can't be changed, but you can change its reference to another one.

                                          See Concepts in Image
   



Benefits of making a class immutable
  1. Immutable objects are thread-safe so you will not have any synchronization issues.
  2. Immutable objects are good Map keys and Set elements, since these typically do not change once created.
  3. Immutability makes it easier to write, use and reason about the code (class invariant is established once and then unchanged)
  4. Immutability makes it easier to parallelize your program as there are no conflicts among objects.
  5. The internal state of your program will be consistent even if you have exceptions.
  6. References to immutable objects can be cached as they are not going to change.
If any query then make a comment

Thanks
Neeraj Srivastava
Java Developer

Happy Learning :) :)
Keep Smiling :) :) :)






Wednesday 18 January 2017

Postmortem of Interface in Java



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

Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviours of an object. An interface contains behaviours that a class implements.


An interface is similar to a class in the following ways:

An interface can contain any number of methods.

An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.

The byte code of an interface appears in a .class file.

Interfaces appear in packages, and their corresponding byte code file must be in a directory structure that matches the package name.

However, an interface is different from a class in several ways, including:

You cannot instantiate an interface.

An interface does not contain any constructors.

All of the methods in an interface are abstract.

An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.

An interface is not extended by a class; it is implemented by a class.

An interface can extend multiple interfaces.

See all concepts in code




In this image , Multiple Inheritance


 Can we get an object of Java interface?
No, we cant, but in once case yes we can

It is anonymous class. Your check class is an interface. Anonymous class defines an implementation of given interface on the fly. So it saves you from creating a separate class for Interface's implementation. This approach is only useful when you know you will never require this implementation any where else in the code.It is anonymous class. Your check class is an interface. Anonymous class defines an implementation of given interface on the fly. So it saves you from creating a separate class for Interface's implementation. 
This approach is only useful when you know you will never require this implementation any where else in the code.




DIFFERENCE:--


Abstract Class Interface
One abstract class cannot extend more than one abstract class One interface can extend more than one interface
use extends use implements
Can contain concrete methods & abstract methods both or any one Must Contain only abstract methods
Access specifier of an abstract method can be any one except private only puclic
you need to specify specifier and abstract by default method is public abstract
Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.
can have final, non-final, static and non-static variables. only static and final variables.
Supports multilevel and hierarchical inheritance but "not multiple inheritance" Supports all types of inheritances – multilevel, hierarchical and multiple
Variables may be of any access specifier including private Variables must be public, static and final; and if omitted, taken by default
Can not extends more than one class can implements more than one interface

Can use Constructor Cannot use Constructor,because it is like method without return type,it have body

Why Interface methods cannot be “static” & “final”?

All variables are implicitly public static and final in interfaces.
Prior to Java 8, you can't create static methods in interfaces. All methods are instance methods.
A static method cannot be overridden


 Because static members and methods are compile time elements , and In interface we use Method Overriding and we cant override Static method because Overriding is Runtime

When we use interface

1. In general Java does not have multiple inheritance, some times we will face some design issues regarding multiple inheritance functionality during development in those cases it is very Good option to use Interface.

2. If we want to provide our services to public access then go for Interfaces.

3. Interfaces are really fit when we want to make functionality as standardized

4. In some design scenarios like if we want to implement some patterns interfaces are very good option , for example “Dependency Injection design pattern”.

5. Prefer Interfaces when ever you want to implement decoupling designs why because  interface doesn’t contain any implementation detail by default.


When we use Abstract class .

1.  If you want to provides a common base class implementation to derived classes then use Abstract class, for example if we have    100 derived classes want to use same functionality then we implement that functionality in abstract class and all the 100 derived classes can extend that abstract class , so that all the derived classes will use.

2. If we are  keep on doing functional changes (adding new methods) to the prefer Abstract class because the existing functionality will not break, but if you use interface in such kind of case the  existing functionality will break.

3.  If we want to declare non-public members prefer Abstract class In an interface, all methods must be public.

4. Prefer Interfaces when ever you want to implement coupling designs why because  Abstract class contain default implementation .


If any query then make a comment


Thanks


Happy Learning :) :)
Keep Smiling :) :) :)






Wednesday 11 January 2017