Unit1_COnstructor
Unit1_COnstructor
The java instanceof operator is used to test whether the object is an instance of the specified
type (class or subclass or interface).
The instanceof in java is also known as type comparison operator because it compares the
instance with type. It returns either true or false. If we apply the instanceof operator with any
variable that has null value, it returns false.
Let's see the simple example of instance operator where it tests the current class.
1. class Simple1{
2. public static void main(String args[]){
3. Simple1 s=new Simple1();
4. System.out.println(s instanceof Simple1);//true
5. }
6. }
Test it Now
Output:true
An object of subclass type is also a type of parent class. For example, if Dog extends Animal
then object of Dog can be referred by either Dog or Animal class
Constructors in Java
In Java, a constructor is a block of codes similar to the method. It is called when an instance
of the class
is created. At the time of calling constructor, memory for the object is allocated in the memory.
Every time an object is created using the new() keyword, at least one constructor is called.
It calls a default constructor if there is no constructor available in the class. In such case, Java
compiler provides a default constructor by default.
There are two types of constructors in Java: no-arg/Default constructor, and parameterized
constructor.
Note: It is called constructor because it constructs the values at the time of object creation. It is
not necessary to write a constructor for a class. It is because java compiler creates a default
constructor if your class doesn't have any.
while declaring a constructor. It controls the object creation. In other words, we can have
private, protected, public or default constructor in Java.
1. <class_name>(){}
Example of default constructor
In this example, we are creating the no-arg constructor in the Bike class. It will be
invoked at the time of object creation.
Output:
Bike is created
Rule: If there is no constructor in a class, compiler automatically creates a default constructor.
class Student3{
int id;
String name;
Student3()
{
name="Rishwanth";
id=101;
}
The parameterized constructor is used to provide different values to distinct objects. However,
you can provide the same values also.
In this example, we have created the constructor of Student class that have two parameters. We
can have any number of parameters in the constructor.
Output:
111 Karan
222 Aryan
In Java, a constructor is just like a method but without return type. It can also be overloaded like
Java methods.
Constructor overloading in Java
is a technique of having more than one constructor with different parameter lists. They are
arranged in a way that each constructor performs a different task. They are differentiated by the
compiler by the number of parameters in the list and their types.
Output:
111 Karan 0
222 Aryan 25
Difference between constructor and method in Java
There are many differences between constructors and methods. They are given below.
A constructor must not have a return type. A method must have a return
type.
The Java compiler provides a default constructor The method is not provided by
if you don't have any constructor in a class. the compiler in any case.
The constructor name must be same as the class The method name may or may
name. not be same as the class name.