Programming Articles

Page 1477 of 2547

When overriding clone method, why do we need to declare it as public in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 2K+ Views

The clone() method belongs to the class named Object of the java.lang package, it accepts an object as a parameter creates and returns a copy of it.In order to use this method, you need to make sure that your class implements the Cloneable (marker) interface.Examplepublic class CloneExample implements Cloneable {    private String name;    private int age;    public CloneExample(String name, int age){       this.name = name;       this.age = age;    }    public void displayData(){       System.out.println("Name : "+this.name);       System.out.println("Age : "+this.age);    }    public static void ...

Read More

Difference between notify() and notifyAll() in Java

Nitin Sharma
Nitin Sharma
Updated on 11-Mar-2026 7K+ Views

Both notify and notifyAll are the methods of thread class and used to provide notification for the thread.But there are some significant differences between both of these methods which we would discuss below.Following are the important differences between notify and notifyAll.Sr. No.KeynotifynotifyAll1NotificationIn case of multiThreading notify() method sends the notification to only one thread among the multiple waiting threads which are waiting for lock.While notifyAll() methods in the same context sends the notification to all waiting threads instead of single one thread.2Thread identificationAs in case of notify the notification is sent to single thread among the multiple waiting threads so ...

Read More

Maximum positive integer divisible by C and is in the range [A, B] in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 198 Views

Here we will see one interesting problem. let us consider we have three integers A, B, and C. We have to find one minimum integer X, such that X mod C = 0, and X is not in the range [A, B]. If the values of A, B and C are 5, 10 and 4 respectively, then the value of X will be 4. We have to follow these steps to get the solution −Steps −If C is not in the range [A, B], then return C as a resultOtherwise get the first multiple of C, which is greater than ...

Read More

C++ program to concatenate a string given number of times?

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 689 Views

Here we will see how we can concatenate a string n number of times. The value of n is given by the user. This problem is very simple. In C++ we can use + operator for concatenation. Please go through the code to get the idea.AlgorithmconcatStrNTimes(str, n)begin    res := empty string    for i in range 1 to n, do       res := concatenate res and res    done    return res endExample#include using namespace std; main() {    string myStr, res = "";    int n;    cout > myStr;    cout > n;    for(int i= 0; i < n; i++) {       res += myStr;    }    cout

Read More

C++ Program to count Vowels in a string using Pointer?

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 662 Views

To get the vowels from a string, we have to iterate through each character of the string. Here we have to use pointers to move through the string. For this we need C style strings. If the string is pointed by str, then *str will hold the first character at the beginning. Then if str is increased, the *str will point next character and so on. If the character is in [a, e, i, o, u] or [A, E, I, O, U] then it is vowel. So we will increase the countAlgorithmcountVowels(str)begin    count := 0    for each character ...

Read More

What happens when a subclass object is assigned to a superclass object in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 6K+ Views

Converting one data type to others in Java is known as casting.If you convert a higher datatype to lower datatype, it is known as narrowing (assigning higher data type value to the lower data type variable).char ch = (char)5;If you convert a lower data type to a higher data type, it is known as widening (assigning lower data type value to the higher data type variable).Int i = 'c';Similarly, you can also cast/convert an object of one class type to others. But these two classes should be in an inheritance relation. Then, If you convert a Super class to subclass ...

Read More

C++ Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! + ...... n/n!

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Here we will see how we can get the sum of the given series. The value of n will be given by user. We can solve this problem by making a factorial function, and get factorial in each step in the loop. But factorial calculation is costlier task than normal addition. We will use the previous factorial term in the next one. Like 3! is (3 * 2 * 1), and 4! is 4 * 3!. So if we store 3! into some variable, we can use that and add the next number only to get the next factorial easily.Algorithmsum_series_fact(n)begin ...

Read More

Is it possible to override a Java method of one class in same?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 3K+ Views

When we have two classes where one extends another and if, these two classes have the same method including parameters and return type (say, sample) the method in the subclass overrides the method in the superclass.i.e. Since it is an inheritance. If we instantiate the subclass a copy of superclass’s members is created in the subclass object and, thus both methods are available to the object of the subclass.But if you call the method (sample), the sampling method of the subclass will be executed overriding the super class’s method.Exampleclass Super{    public static void sample(){       System.out.println("Method of ...

Read More

C++ program to Adding elements of an array until every element becomes greater than or equal to k

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 257 Views

We have array of unsorted element i.e. arr[] and have an integer K, and we have to find the minimum number of steps needed in which the elements of the array will be added to make all the elements greater than or equal to K. We can add two elements of array and make them one.Example, Input: arr[] = {1 10 12 9 2 3}, K = 6 Output: 2ExplanationFirst we can add (1 + 2), so the new array is 3 10 12 9 3, we can add (3 + 3), So the new array is 6 10 12 ...

Read More

How to access a derived class member variable by an interface object in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 821 Views

When you try to hold the super class’s reference variable with a sub class object, using this object you can access the members of the super class only, if you try to access the members of the derived class using this reference you will get a compile time error.Exampleinterface Sample {    void demoMethod1(); } public class InterfaceExample implements Sample {    public void display() {       System.out.println("This ia a method of the sub class");    }    public void demoMethod1() {       System.out.println("This is demo method-1");    }    public static void main(String args[]) { ...

Read More
Showing 14761–14770 of 25,466 articles
Advertisements