Skip to main content

Posts

Java Cloning and Types of Cloning (Shallow and Deep) in Details with Example

In my previous article 5 Different ways to create objects in Java with Example , I have discussed 5 different ways (new keyword, Class.newInstance() method, Constructor.newInstance() method, clone() method and deserialization) a developer can use to create objects, if you haven't read it please go ahead. Cloning is also a way of creating an object but in general, cloning is not just about creating a new object. Cloning means creating a new object from an already present object and copying all data of given object to that new object. In order to create a clone of an object we generally design our class in such way that Our class should implement Cloneable interface otherwise, JVM will throw CloneNotSupportedException if we will call clone() on our object. Cloneable interface is a marker interface which JVM uses to analyse whether this object is allowed for cloning or not. According to JVM if yourObject instanceof Cloneable then create a copy of the object otherwise throw ...

Why an outer Java class can’t be static

In a previous blog , I talked about why we can not define an outer class using private or protected keywords. If you have not read it, please go ahead and give it a look. I this article I will talk what is the use of the static keyword, why an outer Java class can’t be static, why it is not allowed in Java to define a static outer class. In order to understand that first, we need to understand what is the static keyword used for, what purpose it solves and how does it works. What does static keyword do Every Java programmer knows that if we need to define some behavior (method) or state (field) which will be common to all objects we define it as static. Because static content (behavior or state) does not belong to any particular instance or object, it will common to all objects and all objects are free to change any static field and every change will be visible to every object. We do not need to create any object of the class to access a static field or method, we can directly...

Why an outer Java class can’t be private or protected

As soon as we try to use private or protected keyword while declaring an outer class compiler gives a compilation error saying “Illegal modifier for the class your_class_name; only public, abstract & final are permitted”. Here in this article, we are going to study why we are not allowed to use these keywords while declaring outer classes. But before understating the reason behind this we need to understand Java access specifiers and their use cases. There is total 4 access specifier in Java mentioned below in the order of their accessibility. private:  anything (field, class, method, interface etc.) defined using private keyword is only accessible inside the entity (class or package or interface) in which it is defined.  default:  only accessible inside the same package and it is also known as package-private (No modifiers needed). protected:  only accessible inside the same package plus outside the package within child classes through inheritance only....

Everything About Object Oriented JavaScript

Complete explanation of Object Oriented JavaScript 01:50  JavaScript Objects 02:36  Objects in Objects 04:12  Constructor Functions 05:58  instanceof 06:28  Passing Objects to Functions 08:09  Prototypes 09:34  Adding Properties to Objects 10:44  List Properties in Objects 11:38  hasOwnProperty 12:42  Add Properties to Built in Objects 14:31  Private Properties 18:01  Getters / Setters 21:20  defineGetter / defineSetter 24:38  defineProperty 27:07  Constructor Function Getters / Setters 29:40  Inheritance 37:13  Intermediate Function Inheritance 39:14  Call Parent Functions 41:51  ECMAScript 6 47:31  Singleton Pattern 49:32  Factory Pattern 52:53  Decorator Pattern 54:52  Observer Pattern

Why Java is Purely Object Oriented Language Or Why Not

Some years back when I have started learning Java I get to know that Java follows Object Oriented Programming paradigm and everything in Java is an object either it is a String (which was a char array in C) or an array itself. But later on I found on Internet people saying that Java is actually not purely Object Oriented because everything in Java is not an object, for example: All primitive types ( char, boolean, byte, short, int, long, float, double ) are not objects because we are not able to do any object like operation (using . and calling methods) on them. I have also found some people some saying that all static content (variables and methods) does not belong to any object so they are non object things. Due to my little bit knowledge and less experience I easily accepted these reasons and started to believe that Java is not a purely Object Oriented Programming Language. But later on I found that for every object JVM creates two objects The object itself. And one Class level ob...