Articles on Trending Technologies

Technical articles with clear explanations and examples

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 311 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 877 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

Meta Strings (Check if two strings can become same after a swap in one string) in C++

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

In this section, we will see how to check whether two strings are meta string or not. The meta strings are those strings that are very much similar. If we swap two elements in one string, then it will be matched with other string. Suppose two strings are “HELLO” and “OELLH”, then they are meta strings.To check whether two strings are meta string or not, we have to follow these steps.Steps −If both strings are of different length, then return falseElse find a number of characters, that have not matched, also store the index of non-matched charactersIf the count is ...

Read More

What is the difference between getter/setter methods and constructor in Java?

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

ConstructorsA constructor in Java is similar to method and it is invoked at the time creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have the same name as their class and, have no return type.If you do not provide a constructor the compiler defines one on your behalf, which initializes the instance variables with default values.You can also accept parameters through constructors and initialize the instance variables of a class using the given values, these are known as parameterized constructors.ExampleThe following Java program has a class named student ...

Read More

Minimize Cost with Replacement with other allowed in C++

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

Suppose we have an array with N elements. We have to remove element from the array by following the given operations. The operation is like, we have to choose any two numbers of the array, and remove larger. Cost including in this operation is the same as the smaller number. We have to delete only one element at a time, based on this operation, and perform the task in minimum cost. Suppose the array has {4, 2, 5}. I take 4 and 2, remove 4 by paying cost 2, then we remove 5 again with cost 2.The approach is too ...

Read More

Why subclass doesn't inherit the private instance variables of superclass in Java?

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

When you declare the instance variables of a class private, you cannot access them in another class if you try to do so a compile-time error will be generated.But, if you inherit a class that has private fields, including all other members of the class the private variables are also inherited and available for the subclass.But, you cannot access them directly, if you do so a compile-time error will be generated.Exampleclass Person{    private String name;    public Person(String name){       this.name = name;    }    public void displayPerson() {       System.out.println("Data of the Person ...

Read More

Why should a blank final variable be explicitly initialized in all Java constructors?

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

A final variable which is left without initialization is known as blank final variable.Generally, we initialize instance variables in the constructor. If we miss out they will be initialized by the constructors by default values. But, the final blank variables will not be initialized with default values. So if you try to use a blank final variable without initializing in the constructor, a compile time error will be generated.Examplepublic class Student {    public final String name;    public void display() {       System.out.println("Name of the Student: "+this.name);    }    public static void main(String args[]) {   ...

Read More
Showing 28741–28750 of 61,299 articles
Advertisements