Core Java FAQ

What is Dynamic Binding and Static Binding in java ?

Dynamic Binding or Late Binding:

Dynamic Binding refers to the case where compiler is not able to resolve the call and the binding is done at runtime only. Let's try to understand this. Suppose we have a class named 'SuperClass' and another class named 'SubClass' extends it. Now a 'SuperClass' reference can be assigned to an object of the type 'SubClass' as well. If we have a method (say 'someMethod()') in the 'SuperClass' which we override in the 'SubClass' then a call of that method on a 'SuperClass' reference can only be resolved at runtime as the compiler can't be sure of what type of object this reference would be pointing to at runtime.

...
SuperClass superClass1 = new SuperClass();
SuperClass superClass2 = new SubClass();
...

superClass1.someMethod(); // SuperClass version is called
superClass2.someMethod(); // SubClass version is called


Here, we see that even though both the object references superClass1 andsuperClass2 are of type 'SuperClass' only, but at run time they refer to the objects of types 'SuperClass' and 'SubClass' respectively.

Hence, at compile time the compiler can't be sure if the call to the method 'someMethod()' on these references actually refer to which version of the method - the super class version or the sub class version.

Thus, we see that dynamic binding in Java simply binds the method calls (inherited methods only as they can be overriden in a sub class and hence compiler may not be sure of which version of the method to call) based on the actual object type and not on the declared type of the object reference.



Static Binding or Early Binding

If the compiler can resolve the binding at the compile time only then such a binding is called Static Binding or Early Binding. All the instance method calls are always resolved at runtime, but all the static method calls are resolved at compile time itself and hence we have static binding for static method calls. Because static methods are class methods and hence they can be accessed using the class name itself (in fact they are encourgaed to be used using their corresponding class names only and not by using the object references) and therefore access to them is required to be resolved during compile time only using the compile time type information. That's the reason why static methods can not actually be overriden.

Similarly, access to all the member variables in Java follows static binding as Java doesn't support (in fact, it discourages) polymorphic behavior of member variables. For example:-

class SuperClass{
...
public String someVariable = "Some Variable in SuperClass";
...
}

class SubClass extends SuperClass{
...
public String someVariable = "Some Variable in SubClass";
...
}
...
...

SuperClass superClass1 = new SuperClass();
SuperClass superClass2 = new SubClass();

System.out.println(superClass1.someVariable);
System.out.println(superClass2.someVariable);
...

Output:-
Some Variable in SuperClass
Some Variable in SuperClass

We can observe that in both the cases, the member variable is resolved based on the declared type of the object reference only, which the compiler is capable of finding as early as at the compile time only and hence a static binding in this case. Anotherexample of static binding is that of 'private' methods as they are never inherited and the compile can resolve calls to any private method at compile time only.

—————

OOPS Concepts?

Abstraction          :  Hiding the features of the object and show the essential features of an object . simply v can tell like outside view of object

Polymorphism    : one name multiple form is polymorphism , compile time polymorphism and runtime polymorphism ‘

Inheritance          : inherit the parent class property as well as have some own property

Encapusalation  : separate the objects state and behaviour and hides the state of the object so the external components will not change the state of it. Hiding the object              data

—————

What is interface?

An interface is a named collection of method definitions with out implementations.An interface can also include constant declarations.

—————

What is Abstract class?

 

  • Abstract class must be extended or subclassed
  • It servers as a template (Common methods declarations)
  • A class that is abstract may not be instantiated i.e you may not call its constructor
  • A may be declared abstract even if it has no abstract methods
  • abstract class may contain static data

—————

How to remove the duplicates elements in the Arraylist?

put arraylist values in a HashSet.

—————

HashMap and HashTable Difference?

HashMap:


  • Not synchronized  so not thread safe
  • permits null values as KEY
  • iterator in a HashMap is fail-safe( fail-safe means while iterating the elements in the hashmap some other thread can change the values of the hashmap it will leads to con-current modification exception)

HashTable:

  • Synchronized so Thread Safe
  • HashTable doesn't allow null values as key 
  • Enumeration used to get the all values in the HashTable, Enumeration has a read only access , so other threads can't modify the content.

—————

Difference between ArrayList and LinkedList?

ArrayList is implementing RandomAccess while LinkedList implementing Queue. You could already tell something about the usage of the two classes.

ArrayListNow for some implementation notes. The ArrayList is actually encapsulating an actualy Array, an Object[]. When you instanciate ArrayList, an array is created, and when you add values into it, the array changes its size accordingly. This gives you strengths and weaknesses:



  • Fast Random Access
  • You can perform random access without fearing for performence. Calling get(int) will just access the underlying array.

  • Adding values might be slow When you don’t know the amount of values the array will contain when you create it, a lot of shifting is going to be done in the memory space when the ArrayList manipulates its internal array.
  • Slow manipulation When you’ll want to add a value randomly inside the array, between two already existing values, the array will have to start moving all the values one spot to the right in order to let that happen.

LinkedListThe LinkedList is implemented using nodes linked to each other. Each node contains aprevious node link, next node link, and value, which contains the actual data. When new data is inserted, a node is inserted and the links of the surrounding nodes are updated accordingly. When one is removed, the same happens – The surrounding nodes are changing their links and the deleted node is garbage collected. This, as well, gives strengths and weaknesses:
 

  • Fast manipulation As you’d expect, adding and removing new data anywhere in the list is instantanious. Change two links, and you have a new value anywhere you want it.
  • No random access Even though the get(int) is still there, it now just iterates the list until it reaches the index you specified. It has some optimizations in order to do that, but that’s basically it.

Some ConclusionsArrayList is very useful when a well defined set of data is needed in a List interface as opposed to an array. It can be dynamically changed, but try not to do so frequently throughout the life of the application. LinkedList is there for you to do just that:Manipulating it is very easy, and as long as its used for iteration purposes only and not for random accessing, it’s the best solution. Further, if you need random accessing from time to time, I suggest toArray for that specific moment.

Another point I didn’t raise here is the Queue issue. LinkedList implements extended abilities to the normal List interface which allows it to add and remove elements from its beginning and end. This makes the LinkedList perfect for Queue and Stack purposes – Although in Java 5 they already added a Stack class.

—————

Difference between ArrayList and Vector?

Vectors are synchronized. Any method that touches the Vector's contents is thread safe. 
ArrayList, on the other hand, is unsynchronized, making them, therefore, not thread safe. 
With that difference in mind, using synchronization will incur a performance hit. 
So if you don't need a thread-safe collection, use the ArrayList don't use Vector. 

A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.
Depending on how you use these classes, you could end up taking a large performance hit while adding new elements. 
If you don't know how much data you'll have, but you do know the rate at which it grows, Vector does possess a slight advantage since you can set the increment value. 

Both the ArrayList and Vector are good for retrieving elements from a specific position in the container or for adding and removing elements from the end of the container. 
All of these operations can be performed in constant time -- O(1). 
However, adding and removing elements from any other position proves more expensive -- linear to be exact: O(n-i), where n is the number of elements and i is the index of the element added or removed. 
For Example : ArrayList Has 5 elements and you want to add an element on 2nd position. 
Then arrayList add the element on 2nd position and shifted existing 2nd position element to 3rd , 3rd position element to 4th etc.. 
For the case of remove just opposite. 
These operations are more expensive because you have to shift all elements at index i and higher over by one element

—————

Difference between Abstract and interface?

 

  • interface contains methods that must be abstract; abstract class may contain concrete methods
  • interface contains variable must be static and final; abstract class may contain non-final and final variables
  • members in an interface are public by default; abstract class may contain non-public methods
  • interface is used to "implements" ; where abstract class is used to "extends"
  • interface can be used to achieve multiple inheritance ; abstract class can be used as a single inheritance
  • interface extend another interface; abstract class can "extends" another class and implements multiple interfaces.

—————

When u go for abstract and when u go for interface?

 

  • When Sub-Type behavior is totally different then go for interface and to achieve the multiple inheritance 
  • When Sub-Type behavior is partially common means i will go for an abstract class and to achieve the single inheritance

—————


Topic: Core Java FAQ

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————