Posts

OO concepts - Java

Object 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 Collection of objects  is called class. It is a logical entity. Inheritance When one object acquires all the properties and behaviours of parent 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. For example: to convense the customer differently, to draw something e.g. shape or rectangle etc. 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 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 a...

Deep copy vs Shallow copy - Java

Image
Shallow:     Deep:     ------------------------------------------------------------------------------------------ 1) The clone() method is used to create a copy of an object in Java. In order to use  clone()  method, class must implement  java.lang.Cloneable   interface  and o verride protected  clone()  method from  java.lang.Object . A call to  clone()  method will result in  CloneNotSupportedException  if that class doesn't implement  Cloneable  interface. 2) No constructor is called during cloning of Object in Java. 3) Default implementation of  clone()  method in Java provides " shallow copy"  of object, because it creates copy of Object by creating new instance and then copying content by assignment, which means if your class contains a mutable field, then both original object and clone will refer to same internal object. This can be dangerous becau...

Composition vs Aggregation - Java

Composition final class Car { private final Engine engine ; Car ( EngineSpecs specs ) { engine = new Engine ( specs ); } void move () { engine . work (); } } Aggregation final class Car { private Engine engine ; void setEngine ( Engine engine ) { this . engine = engine ; } void move () { if ( engine != null ) engine . work (); } } In the case of composition, the Engine is completely encapsulated by the Car. There is no way for the outside world to get a reference to the Engine. The Engine lives and dies with the car. With aggregation, the Car also performs its functions through an Engine, but the Engine is not always an internal part of the Car. Engines may be swapped, or even completely removed. Not only that, but the outside world can still have a reference to the Engine, and tinker with it regardless of whether it's in the Car.

Throw vs throws - Java

Throws clause in used to declare an exception and throw keyword is used to throw an exception explicitly. If we see syntax wise than throw is followed by an instance variable and throws is followed by exception class names. The keyword throw is used inside method body to invoke an exception and throws clause is used in method declaration (signature). for Example Throw throw new Exception ( "You have some exception" ) throw new IOException ( "Connection failed!!" ) Throws throws IOException , ArithmeticException , NullPointerException

Final keyword - Java

Reference can not be reassigned. But you can change its internals (it is mutable, if it was originally). So this works : final A ob = new A (); ob . setI ( 6 ) but this doesn't : final A ob = new A (); ob = new A (); ------------------------------------------------------------------------------------------------------------------------ 1.  Final keyword  can be applied to member variable, local variable, method or  class in Java . 2.  Final member variable  must be initialized at the time of declaration or inside constructor, failure to do so will result in compilation error. 3. You can not reassign value to  final variable in Java . 4.  Local final variable  must be initializing during declaration. 5. Only final variable is accessible inside anonymous class in Java. 6.  Final method  can not be  overridden in Java . 7.  Final class  can not be inheritable in Java. 8....

Transient keyword - Java

Java transient  keyword is used in serialization. If you define any data member as transient, it will not be serialized. Let's take an example, I have declared a class as Student, it has three data members id, name and age. If you serialize the object, all the values will be serialized but I don't want to serialize one value, e.g. age then we can declare the age data member as transient. Example of Java Transient Keyword In this example, we have created the two classes Student and PersistExample. The age data member of the Student class is declared as transient, its value will not be serialized. If you deserialize the object, you will get the default value for transient variable. Let's create a class with transient variable. import  java.io.Serializable;   public   class  Student  implements  Serializable{     int  id;    String name;     transient   int  age;...

TreeMap internal working - Java

TreeMap is a Red-Black tree based NavigableMap implementation.In other words , it sorts the TreeMap object keys using Red-Black tree algorithm. -------------------------------------------------------------------------------------------------- TreeMap in Java  is a SortedMap and it maintains Sorting order when you insert object on it. You can specify Sorting order while Creating TreeMap by providing an explicit Comparator to TreeMap. Basically you can create TreeMap in Java by different ways, a TreeMap with natural sorting order, and TreeMap with custom Sorting Order by providing Comparator, Copying Sorting order from other SortedMap etc. TreeMap has specific constructor for these conditions. We will see these in section of creating instance of TreeMap in Java.  Here are few ways to create TreeMap in Java: TreeMap in Java using natural ordering of keys TreeMap naturalOrderMap = new TreeMap(); TreeMap with custom sorting order TreeMap customSort...