Programming Articles - Page 2575 of 3366

How to merge properties of two JavaScript Objects dynamically?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

325 Views

There are two methods to merge properties of javascript objects dynamically. They are1) Object.assign() The Object.assign() method is used to copy the values of all properties from one or more source objects to a target object. It will return the target object.Example-1 Live Demo var target = { a: "ram", b: "rahim" }; var source = { c: "akbar", d: "anthony" }; var returnedTarget = Object.assign(target, source); document.write(JSON.stringify(target)); document.write(JSON.stringify(returnedTarget)); output{"a":"ram", "b":"rahim", "c":"akbar", "d":"anthony"} {"a":"ram", "b":"rahim", "c":"akbar", "d":"anthony"}If the objects have the same keys, ... Read More

How to rethrow an exception in Java?

raja
Updated on 30-Jul-2019 22:30:26

19K+ Views

Sometimes we may need to rethrow an exception in Java. If a catch block cannot handle the particular exception it has caught, we can rethrow the exception. The rethrow expression causes the originally thrown object to be rethrown.Because the exception has already been caught at the scope in which the rethrow expression occurs, it is rethrown out to the next enclosing try block. Therefore, it cannot be handled by catch blocks at the scope in which the rethrow expression occurred. Any catch blocks for the enclosing try block have an opportunity to catch the exception.Syntaxcatch(Exception e) {    System.out.println("An exception ... Read More

What is the importance of the Throwable class and its methods in Java?

raja
Updated on 06-Feb-2020 11:30:26

533 Views

The Throwable class is a superclass of all errors and exceptions in Java. Objects that are instances of this class are thrown by the Java Virtual Machine or can be thrown by a throw statement. Similarly, this class or one of its subclasses can be the argument type in a catch clause.Instances of two subclasses Error and Exception are used to indicate that exceptional situations have occurred, these instances are created in the context of the exceptional situation to include relevant information.Commonly used exception methods of Throwable classpublic String getMessage(): returns the message string about the exception.public Throwable getCause(): returns the cause of the exception. ... Read More

What is non-enumerable property in JavaScript and how can it be created?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

3K+ Views

Non-enumerable propertyObjects can have properties that don't show up when iterated through the particular object using Object.keys() or for...in loop.Those type of properties are called as non-enumerable properties.Creating a non-enumerable propertyTo create a non-enumerable property we have to use Object.defineProperty() method. This a special method to create non-enumerable properties in an object. In the following example, three properties such as name, age and country were created normally and a property named "salary" was created using Object.defineProperty() method and key named enumerable was assigned with false. When the object "person" got iterated using Object.keys() the properties such as name, age and country ... Read More

What is "namespace" keyword in PHP?

Alok Prasad
Updated on 31-Dec-2019 08:53:49

369 Views

In this article, we are going to learn about namespace in PHP. In PHP when we are creating large applications or when integrating third-party applications/library then there may be chances of collision between class names, function names. So to avoid these problems PHP "Namespaces" provide a way in which to group related classes, interfaces, functions and constants.Let's see the syntax of declaring namespace below.SyntaxIn the PHP world, namespaces are intended to take care of two issues that creators of libraries and applications experience when making re-usable code components, Those are:1.Name impact between code you create, and internal PHP classes/functions/constants or ... Read More

How to prevent modification of object in JavaScript ?.

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

404 Views

ECMAScript 5 has introduced several methods to prevent modification of object. Those preventive measures ensures that no one, accidentally or otherwise change functionality of object.There are 3 levels of preventive methods1) Prevent ExtensionsIn this level, one cannot add any new properties or methods but can access existing properties or methods. Here there is an ability to delete the respective object. Object.preventExtensions() is the method used to accomplish this task. It prevents any new properties from ever being added to the object.ExampleLive Demo var object1 = { prop1: 1 ... Read More

When to use an abstract class and when to use an interface in Java?

Shriansh Kumar
Updated on 27-May-2025 10:43:00

47K+ Views

An interface can be used to define a contract for behavior that classes must implement, and it can also act as a contract between two systems to interact. An abstract class is mainly used to define default behavior for subclasses. All child classes inherited from the abstract class can override or extend its methods. If we compare an interface and an abstract class, both can contain abstract methods and cannot be instantiated directly. Also, both are used to achieve abstraction. We may often get confused about when to use an abstract class and when to use an interface in Java.  ... Read More

Can we create an object of an abstract class in Java?

Shriansh Kumar
Updated on 06-Jun-2025 13:59:55

15K+ Views

No, we can't create an object of an abstract class. But, we can create a reference variable of an abstract class. The reference variable is used to refer to the objects of derived classes (subclasses of abstract class). An abstract class hides the implementation and shows the function definition to the user. A Java abstract class can have instance methods that implement a default behavior and 0 or more abstract methods. A method which does not have body is known as abstract method. It contains only the method signature and an abstract keyword before it as shown below: public abstract ... Read More

What is method overloading in PHP?

Alok Prasad
Updated on 31-Dec-2019 08:29:38

11K+ Views

Method Overloading is a concept of Object Oriented Programming which helps in building the composite application in an easy way. Function overloading or method overloading is a feature that permits making creating several methods with a similar name that works differently from one another in the type of the input parameters it accepts as arguments.The above concept is fine for other programming languages and it is called static polymorphic i.e method overloading.ExampleLet's understand through an example.Output:ErrorExplanation:This will generate an error since php will say you have declared this method twice.But Other programming languages says , doTask($var1) and doTask($var1, $var2) are ... Read More

What is a constant and how to define constants in Java?

Shriansh Kumar
Updated on 06-Jun-2025 13:38:33

34K+ Views

Constants in Java A constant is a variable whose value cannot be changed once it has been initialized. Java doesn't have built-in support for constants. To define a variable as a constant, we just need to add the keyword "final" in front of the variable declaration. It is not mandatory that we assign values to constants during declaration. Syntax of Java Constant final float pi = 3.14f; The above statement declares the float variable "pi" as a constant with a value of 3.14f. We cannot change the value of "pi" at any point in time in the program. ... Read More

Advertisements