Programming Articles - Page 2564 of 3366

Can we declare a constructor as private in Java?

Alshifa Hasnain
Updated on 05-Jun-2025 13:00:43

11K+ Views

Java constructor is a special method used to initialize objects. It has the same name as the class and is automatically called when an object is created. You can also declare a constructor as private to restrict object creation from outside the class. What is a Private Constructor? A private constructor is a constructor that is declared using the private access modifier. It restricts object creation from outside the class i.e., the class having a private constructor cannot be subclassed. Private constructors are used in design patterns like Singleton, or to prevent subclassing or instantiation. Purpose of a Private ... Read More

What will happen when we try to override final method of the superclass in Java?

Alshifa Hasnain
Updated on 09-Jun-2025 14:49:19

2K+ Views

Any method that is declared as final in the superclass cannot be overridden by a subclass. If we try to override the final method of the super class, we will get a compile-time error.  Rules for Implementing Method Overriding The method declaration should be the same as that of the method that is to be overridden. The class (subclass) should extend another class (superclass), prior to even try overriding. The Sub Class can never override final methods of the Super Class. Final method A method is declared final when ... Read More

What is a ClassCastException and when it will be thrown in Java?

Alshifa Hasnain
Updated on 18-Apr-2025 16:12:07

2K+ Views

In this article, we will be learning about ClassCastException in Java. But before we jump into this, we'll understand what an exception is in Java. After that, we'll learn why and when ClassCastException occurs, look at real-world code examples that throw this exception, and finally learn how to avoid or resolve it. What is an Exception in Java? An exception in Java is an event that disrupts the normal flow of the program. Java provides a way to handle these errors gracefully using try-catch blocks. There are 2 types of exceptions: Checked Exceptions – ... Read More

How does Python\'s super() work with multiple inheritance?

SaiKrishna Tavva
Updated on 27-Feb-2025 19:36:45

1K+ Views

Python supports a feature known as multiple inheritance, where a class (child class) can inherit attributes and methods from more than one parent class. This allows for a flexible and powerful way to design class hierarchies. To manage this, Python provides the super() function, which facilitates the calling of methods from a parent class without explicitly naming it. Understanding Multiple Inheritance In multiple inheritance, a child class can derive attributes and methods from multiple parent classes. This can be particularly useful in scenarios where you want to compose behavior from different sources. Example In the following example, the Child class ... Read More

What does double question mark (??) operator mean in PHP ?

Alok Prasad
Updated on 29-Jun-2020 11:37:56

5K+ Views

PHP 7 has added a new operator double question mark (??) operator. In PHP 7, the double question mark(??) operator known as Null Coalescing Operator.It returns its first operand if it exists and is not NULL; otherwise, it returns its second operand. It evaluates from left to right. Null Coalescing operator also can be used in a chain format.Let's take the below example to demonstrate the double question mark (??) operator.ExampleOutput9ExampleOutput34

What are the different types of nested classes are defined in Java?

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

1K+ Views

In Java, it is possible to define a class inside another class, such classes are called Nested classes. We can use the access modifiers like private, public, protected or default for inner classes and default or public access modifiers for outer class.There are two types of nested classes are defined in Java.Static Nested ClassNon-Static Nested ClassStatic Nested ClassWe Can define an inner class as static, so such type of classes is called a static nested class.The nested class is defined with the static keyword, so this type of nested classes doesn’t share any relationship with the instance of an outer class.A static ... Read More

What is the difference between del, remove and pop on lists in python ?

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

555 Views

It does't matter how many lines of code you write in a program. When you want to remove or delete any elements from the Python list, you have to think about the difference between remove, del and pop in Python List and which one to useremove : remove() removes the first matching value or object, not a specific indexing. lets say list.remove(value)Examplelist=[10, 20, 30, 40] list.remove(30) print(list)Output[10, 20, 40]del : del removes the item at a specific index. lets say del list[index]Examplelist = [10, 20, 30, 40, 55] del list[1] print(list)Output[10, 30, 40, 55]pop : pop removes the item at a specific index and returns ... Read More

How are arguments passed by value or by reference in Python?

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

13K+ Views

Python uses a mechanism, which is known as "Call-by-Object", sometimes also called "Call by Object Reference" or "Call by Sharing"If you pass immutable arguments like integers, strings or tuples to a function, the passing acts like Call-by-value. It's different, if we pass mutable arguments.All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function.Examplestudent={'Archana':28, 'krishna':25, 'Ramesh':32, 'vineeth':25} def test(student):    new={'alok':30, 'Nevadan':28}    student.update(new)    print("Inside the function", student)    return test(student) print("outside the function:", student)OutputInside the function ... Read More

How to convert an array to SimpleXML in PHP?

Alok Prasad
Updated on 29-Jun-2020 11:20:10

4K+ Views

We can solved the above problem using array_walk_recursive() function.array_walk_recursive()   is an inbuilt PHP function. This function converts array to XML document where keys of the array are converted into values and values of the array are converted into the element of XML.Let' demonstrate with a simple example.ExampleOutput alex account michigan NoteIf errors message display like PHP Fatal error: Uncaught Error: Class 'SimpleXMLElement' not found in then simply install php-xml, php-simplexml packages.

What are the differences between length and length () in Java?

Shriansh Kumar
Updated on 21-May-2025 10:23:36

4K+ Views

In Java, both the length property and the length() method are used to determine the size of data, or we can say they help us to find the number of elements an object contains. However, they are used with different Java objects. The length is an instance variable used for arrays, whereas length() is a method used with String objects. In this article, we are going to discuss the difference between length and length() in Java. The length Property An array is an object that holds a fixed number of values of the same type. Its length variable is used to find the number ... Read More

Advertisements