Java Interview Preparation (conceptual)
-------------------------------------------------------------
***Java is partially Object-oriented as it has primitive types.
_-_____-----___--___--____---
Oops Concept
_-_____-----___--___--____---
abstraction,
encapsulation,
inheritance,
polymorphism.
Inheritance :
Real-World Example :
Children have some of the qualities of their parents and grandparents or their ancestors.
Java Example:
There is a method in the parent class and you want the same method to create in a child class. Why create a new one if it already exists. As a child, you can inherit the method.
There are a few variables in a class and you need them .So why to create new variables. We can extend that class (means we can be a child of that class so that the qualities of that class such as variables, methods, and all available in the child class).
Encapsulation :
Real-World Example :
Have you ever seen the tablets you take while you are ill? The contain of the tablet is encapsulated with a cover, which is a thin plastic or something. i.e. wrapped.
Java Example :
Wrapping of data is called Encapsulation.The example is that the use of getter and setter i.e. get() method and set() method to set a value to a variable that is private.
E.g. there is a variable in the class i.e. salary. So that salary if made public, will be accessible for everyone which is not secure. So I made that variable salary as private and now told people to set their salary by using a method only i.e. setSalary() because as I have made the variable salary private, they can't simply access that and now my variable is secure.
Same if someone wants to get his salary detail, as I have made salary private, he or she can't access. I will give him a method so that he can access his salary only by the method getSalary().
PolyMorphism :
Real-World Example :
Abstract class | Interface |
---|---|
1) Abstract class can have abstract and non-abstract methods. | Interface can have only abstract methods. Since Java 8, it can have default and static methods also. |
2) Abstract class doesn't support multiple inheritance. | Interface supports multiple inheritance. |
3) Abstract class can have final, non-final, static and non-static variables. | Interface has only static and final variables. |
4) Abstract class can provide the implementation of interface. | Interface can't provide the implementation of abstract class. |
5) The abstract keyword is used to declare abstract class. | The interface keyword is used to declare interface. |
6) An abstract class can extend another Java class and implement multiple Java interfaces. | An interface can extend another Java interface only. |
7) An abstract class can be extended using keyword "extends". | An interface can be implemented using keyword "implements". |
8) A Java abstract class can have class members like private, protected, etc. | Members of a Java interface are public by default. |
9)Example: public abstract class Shape{ public abstract void draw(); } | Example: public interface Drawable{ void draw(); } |
Convert List to Array :
Set Interface
Sr.No. | Method & Description |
---|---|
1 | add( ) Adds an object to the collection. |
2 | clear( ) Removes all objects from the collection. |
3 | contains( ) Returns true if a specified object is an element within the collection. |
4 | isEmpty( ) Returns true if the collection has no elements. |
5 | iterator( ) Returns an Iterator object for the collection, which may be used to retrieve an object. |
6 | remove( ) Removes a specified object from the collection. |
7 | size( ) Returns the number of elements in the collection. |
Method | Description |
---|---|
V put(Object key, Object value) | It is used to insert an entry in the map. |
void putAll(Map map) | It is used to insert the specified map in the map. |
V putIfAbsent(K key, V value) | It inserts the specified value with the specified key in the map only if it is not already specified. |
V remove(Object key) | It is used to delete an entry for the specified key. |
boolean remove(Object key, Object value) | It removes the specified values with the associated specified keys from the map. |
Set keySet() | It returns the Set view containing all the keys. |
Set<Map.Entry<K,V>> entrySet() | It returns the Set view containing all the keys and values. |
void clear() | It is used to reset the map. |
V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) | It is used to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). |
V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction) | It is used to compute its value using the given mapping function, if the specified key is not already associated with a value (or is mapped to null), and enters it into this map unless null. |
V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) | It is used to compute a new mapping given the key and its current mapped value if the value for the specified key is present and non-null. |
boolean containsValue(Object value) | This method returns true if some value equal to the value exists within the map, else return false. |
boolean containsKey(Object key) | This method returns true if some key equal to the key exists within the map, else return false. |
boolean equals(Object o) | It is used to compare the specified Object with the Map. |
void forEach(BiConsumer<? super K,? super V> action) | It performs the given action for each entry in the map until all entries have been processed or the action throws an exception. |
V get(Object key) | This method returns the object that contains the value associated with the key. |
V getOrDefault(Object key, V defaultValue) | It returns the value to which the specified key is mapped, or defaultValue if the map contains no mapping for the key. |
int hashCode() | It returns the hash code value for the Map |
boolean isEmpty() | This method returns true if the map is empty; returns false if it contains at least one key. |
V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction) | If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. |
V replace(K key, V value) | It replaces the specified value for a specified key. |
boolean replace(K key, V oldValue, V newValue) | It replaces the old value with the new value for a specified key. |
void replaceAll(BiFunction<? super K,? super V,? extends V> function) | It replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception. |
Collection | It returns a collection view of the values contained in the map. |
int size() | This method returns the number of entries in the map. |
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
Note :
**
Do you want to iterate the entries (key+value) or only keys or only values? That's why you obtain the specific collection and obtain an iterator.
Since keys/entries are unique (no duplicate elements present), you obtain a set through entrySet()
and keySet()
. And since values can have duplicates, you obtain the collection through values()
and then get an iterator from these.
**
It depends on what value the programmer is seeking to iterate. Either it's just keys or values or both. based on that, we have three different objects that can be iterated. Example: mapObject.keySet().iterator()
; or mapObject.values().iterator()
; or mapObject.entrySet().iterator()
. The only thing to keep in mind is that, when we iterate over just values()
collection, we won't get respective keys by any means.
***It works similar to java.util.Arrays.sort() method but it is better then as it can sort the elements of Array as well as linked list, queue, and many more present in it.
Comments
Post a Comment