What Is Constructor and Destructor in Java - 1
What Is Constructor and Destructor in Java - 1
In Java, a constructor is a particular method that initializes an object when it is first formed. It
guarantees that the item begins its trip with predetermined values and configurations. Consider
it a blueprint for the object, outlining how it should be initialized.
In Java, constructors have the same name as the class they belong to. They have no return
type, not even void, which distinguishes them from conventional methods. When you build an
instance of a class using the 'new' keyword, the constructor is automatically invoked, setting the
stage for the object to perform its duties.
Constructors exist in a variety of types; parameterized constructors enable you to supply data
during object formation, whilst default constructors are used when there is no explicit
constructor declared.
Unlike several programming languages, Java does not have explicit destructors. Instead, it
depends on the garbage collector to automatically reclaim memory used by things that are no
longer in use. This technique, referred to as garbage collection, relieves the programmer of
manual memory management duties.
Java doesn't offer a traditional destructor, developers can implement the finalize() method.
However, it's essential to note that relying solely on finalize() for cleanup is not recommended
due to its unpredictable nature.
To summarise, constructors give life to Java objects by purposefully initialising them, whereas
destructors, or their substitutes, provide a graceful farewell when an object's trip is over. The
ability to generate and deconstruct objects is critical for developing strong and efficient Java
programs.
Learn more about Constructor in Java.
Types of Constructor
Constructors are important in Java programming because they initialise objects. Constructors
are special methods that are responsible for initialising an object's state when it is first formed.
In this lecture, we'll look at three types of constructors that Java developers commonly
encounter: default constructors, parametrized constructors, and copy constructors.
Default Constructor
Let's kick things off with the humble Default Constructor. This type of constructor is
automatically provided by Java if a class doesn't explicitly define any constructor. Its major job is
to set up the object with default settings. Consider it the default constructor for objects created
without any starting arguments.
There are two types of modifiers in Java: access modifiers and non-access modifiers.
The access modifiers in Java specifies the accessibility or scope of a field, method, constructor,
or class. We can change the access level of fields, constructors, methods, and class by
applying the access modifier on it.
Private: The access level of a private modifier is only within the class. It cannot be accessed
from outside the class.
Default: The access level of a default modifier is only within the package. It cannot be accessed
from outside the package. If you do not specify any access level, it will be the default.
Protected: The access level of a protected modifier is within the package and outside the
package through child class. If you do not make the child class, it cannot be accessed from
outside the package.
Public: The access level of a public modifier is everywhere. It can be accessed from within the
class, outside the class, within the package and outside the package.
Q.COMMAND LINE ARGUMENT!?
Java command-line argument is an argument i.e. passed at the time of running the Java
program. In Java, the command line arguments passed from the console can be received in the
Java program and they can be used as input. The users can pass the arguments during the
execution bypassing the command-line arguments inside the main() method.
We need to pass the arguments as space-separated values. We can pass both strings and
primitive data types(int, double, float, char, etc) as command-line arguments. These arguments
convert into a string array and are provided to the main() function as a string array argument.
When command-line arguments are supplied to JVM, JVM wraps these and supplies them to
args[]. It can be confirmed that they are wrapped up in an args array by checking the length of
args using args.length.
Internally, JVM wraps up these command-line arguments into the args[ ] array that we pass into
the main() function. We can check these arguments using args.length method. JVM stores the
first command-line argument at args[0], the second at args[1], the third at args[2], and so on.
In other words, If a subclass provides the specific implementation of the method that has been
declared by one of its parent class, it is known as method overriding.
Method overriding is used to provide the specific implementation of a method which is already
provided by its superclass.
Method overriding is used for runtime polymorphism
Preventing naming conflicts. For example there can be two classes with name Employee in two
packages, college.staff.cse.Employee and college.staff.ee.Employee
Making searching/locating and usage of classes, interfaces, enumerations and annotations
easier
Providing controlled access: protected and default have package level access control. A
protected member is accessible by classes in the same package and its subclasses. A default
member (without any access specifier) is accessible by classes in the same package only.
Packages can be considered as data encapsulation (or data-hiding).
All we need to do is put related classes into packages. After that, we can simply write an import
class from existing packages and use it in our program. A package is a container of a group of
related classes where some of the classes are accessible are exposed and others are kept for
internal purpose.
We can reuse existing classes from the packages as many time as we need it in our program.
Understanding Multiple Inheritance A class's capacity to inherit traits from several classes is
referred to as multiple inheritances. This notion may be quite helpful when a class needs
features from many sources. Multiple inheritances, however, can result in issues like the
diamond problem, which occurs when two superclasses share the same method or field and
causes conflicts. Java uses interfaces to implement multiple inheritances in order to prevent
these conflicts.
Q:-Java Threads?
Typically, we can define threads as a subprocess with lightweight with the smallest unit of
processes and also has separate paths of execution. The main advantage of multiple threads is
efficiency (allowing multiple things at the same time. For example, in MS Word. one thread
automatically formats the document while another thread is taking user input. Another
advantage is quick response, if we use multiple threads in a process and if a thread gets stuck
due to lack of resources or an exception, the other threads can continue to execution, allowing
the process (which represents an application) to continue to be responsive.
In this AWT tutorial, you will learn the basics of the AWT, including how to create windows,
buttons, labels, and text fields. We will also learn how to add event listeners to components so
that they can respond to user input.
By the end of this tutorial, you will have a good understanding of the AWT and be able to create
simple GUIs in Java.
String Declaration
There are two ways to declare strings in C:
The following example will create a string as "Scaler" where the last character must always be a
null character. The size mentioned within the brackets is the maximum number of characters a
string could hold, and it is mandatory to give the size of a string if we are not initializing it at the
time of declaration.
2 In this method, we do not need to put the null character at the end of the string constant. The
compiler automatically inserts the null character at the end of the string.
Java abstract class is a class that can not be initiated by itself, it needs to be subclassed by
another class to use its properties. An abstract class is declared using the “abstract” keyword in
its class definition.
Interface in Java
An interface in Java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract
methods in the Java interface, not method body. It is used to achieve abstraction and multiple
inheritance in Java.
In other words, you can say that interfaces can have abstract methods and variables. It cannot
have a method body.
Java Interface also represents the IS-A relationship. It cannot be instantiated just like the
abstract class.
Since Java 8, we can have default and static methods in an interface.