Virtual Destructor in C++

Aman Kumar
Updated on 27-May-2025 16:32:51

2K+ Views

A virtual destructor is a destructor declared within the base class with a virtual keyword. In C++, destructors are special members of a class that frees memory occupied by an object when a memory leak occurs. Deleting a derived class object using a pointer to a base class, the base class should be defined with a virtual destructor. When to Use Virtual Destructor? Virtual destructorsare needed in scenarios where polymorphism and inheritance are involved, and instances of derived classes are managed by pointers to base classes. If our class has one or more virtual functions that are inherited from child ... Read More

Virtual Constructor in C++

Aman Kumar
Updated on 27-May-2025 16:31:50

23K+ Views

In C++, we cannot create a virtual constructor, this is because C++ is a statically typed language and the constructor is responsible for creating an object. So, the compiler needs to know the exact type of object at compile time. The virtual mechanism works only when we have a base class pointer to a derived class object. The constructor cannot be virtual, because when a constructor of a class is executed there is no virtual table in the memory, means no virtual pointer defined yet. So, the constructor should always be non-virtual. Implementation of Constructor & Destructor In the following ... Read More

Create a Thread Using Anonymous Class in Java

Vivek Verma
Updated on 27-May-2025 16:16:05

4K+ Views

This article will use an anonymous class to create a Thread in Java. An Anonymous class is a class that does not have a name. Thread in Java In Java, a Thread is a part of a program that can be executed independently. All Java programs have at least one thread, known as the main thread, which is created by the Java Virtual Machine (JVM), when the main() method is invoked. Creating a Thread by Using Anonymous Class In Java, the basic way to create a thread is to either extend the Thread class or implement the Runnable interface. However, we ... Read More

Use of Object Cloning in Java

Vivek Verma
Updated on 27-May-2025 15:54:32

2K+ Views

The article will explain the use of object cloning in Java and provide an appropriate example to clarify the problem. What is the Object Cloning in Java? In Java, object cloning is a way to create an exact copy of an object. We can use the clone() method provided by the Object class to create a clone of an object. When an object is cloned, a new instance of the same class is created, and the fields of the original object are copied to the new object. The Cloneable interface must be implemented by a class whose object is ... Read More

Ways to Call Garbage Collector (GC) in Java

Vivek Verma
Updated on 27-May-2025 15:23:58

1K+ Views

This article explains the different ways of calling the garbage collector (GC) in Java. It also includes a brief introduction to garbage collection, various calling approaches, and relevant examples. Garbage Collection (GC) in Java In Java, the garbage collection is carried out by a daemon thread called the Garbage Collector (GC). Instead of waiting until the Java Virtual Machine (JVM) runs a garbage collector, we can request the JVM to run the garbage collector. There is no guarantee that the JVM will accept our request. In Java, we can call the garbage collector (GC) manually in "two ways", which ... Read More

Purpose of Overriding a finalize Method in Java

Vivek Verma
Updated on 27-May-2025 15:07:58

1K+ Views

In Java, overriding is a technique used to provide a specific implementation of a method that is already defined in a superclass (i.e., parent class), allowing a subclass (i.e., child class) to modify or extend the behavior of that method. Purpose of Overriding the finalize() Method The purpose of a finalize() method can be overridden for an object to include the cleanup code or to dispose of the system resources that can be done before the object is garbage collected. If we are overriding this method, then we need to call the finalize() method explicitly. The finalize() method can be ... Read More

Convert Between List and Array in Java

Vivek Verma
Updated on 27-May-2025 14:31:46

1K+ Views

In Java, a List is an interface that represents an ordered collection of elements of the same type. You cannot directly create an object of the List interface, instead, you need to instantiate an ArrayList class or another class that implements the List interface.An array is a container that holds a fixed size of similar types of data. As the size is fixed, it can not be changed once created.The conversion between List and array is important because array provides faster access to the elements than List interface (faster in case of searching elements), and provides a fixed size of ... Read More

Convert Array to List in Java

Vivek Verma
Updated on 27-May-2025 14:23:13

663 Views

In Java, an array is a container object that holds a similar type of data (elements), but has a fixed size. You can declare it by assigning values in curly braces or creating it using the new keyword by specifying the size. A List in Java is an interface that represents a sequence of elements of the same type (duplicates are allowed) and has a dynamic size. To create an object of the List interface, you can use one of its classes, such as ArrayList, Stack, Vector, etc.Converting an Array to a List in JavaSometimes we need to insert more data ... Read More

Check If a Triangle Is Valid Based on Given Sides in Java

Vivek Verma
Updated on 27-May-2025 13:37:32

9K+ Views

A Triangle is a polygon that has 3 sides, and it consists of three sides and three vertices. The sum of the three internal angles is up to 180 degrees. Below is the diagram of a triangle having three sides (a, b, and c): In a valid triangle, if you add any two sides, then it will be greater than the third side. As per our problem statement, we have to check if a triangle is valid or not, if three sides are given, using the Java programming language. Suppose a, b and c are the three sides of ... Read More

Catch LookupError Exception in Python

Sarika Singh
Updated on 27-May-2025 12:11:54

2K+ Views

LookupError in Python is the base class for errors that occur when a lookup using a key or index fails to find the expected value. This includes exceptions like IndexError and KeyError. If any lookup operation fails, a LookupError or one of its subclasses is raised. In this article, you will learn how to catch LookupError exceptions in Python and handle them to prevent your program from crashing abruptly. LookupError is commonly raised when - You access an invalid index in a list or a tuple. You use a missing key ... Read More

Advertisements