Sie sind auf Seite 1von 19

Constructor

In Java, constructor is a block of codes similar to method. It


is called when an instance of object is created and memory is
allocated for the object.
It is a special type of method which is used to initialize the
object.
When is a constructor called

Every time an object is created using new() keyword,


at least one constructor is called. It is called a default
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.
There are basically two rules defined for the constructor.

Constructor name must be same as its class name


Constructor must have no explicit return type
Types of java constructors

There are two types of constructors in java:


Default constructor (no-arg constructor)
Parameterized constructor
Java Default Constructor
 
A constructor is called "Default Constructor" when it
doesn't have any parameter.
General Syntax of Default Constructor is
 
class demo
{
demo()
{
}
}
In this example, we are creating the no-arg constructor in the
Demo class. It will be invoked at the time of object creation.

class Demo{
int value1;
int value2;
Demo(){
value1 = 10;
value2 = 20;
System.out.println("Inside Constructor");
}
public void display(){
System.out.println("Value1 === "+value1);
System.out.println("Value2 === "+value2);
} Output
Inside Constructor
public static void main(String args[])
Value1 === 10
{
Value2 === 20
Demo d1 = new Demo();
d1.display();
}
}
Java parameterized constructor

A constructor which has a specific number of


parameters is called parameterized constructor.
Parameterized constructor is used to provide different
values to the distinct objects.
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.
class Student4
{  
    int id;  
    String name;  
         Student4(int i,String n)
{  
    id = i;  
    name = n;  
    }   Output:
    void display()  
{ 34 Java
System.out.println(id+" "+name); 43 Python
}  
       public static void main(String args[])
{  
    Student4 s1 = new Student4(34,"Java");  
    Student4 s2 = new Student4(43,"Python");  
    s1.display();  
    s2.display();  
Constructor Overloading in Java

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.
class Demo
{
int value1; public void display()
int value2;
{
Demo()
{ System.out.println("Value1 ===
value1 = 10; "+value1);
value2 = 20; System.out.println("Value2
System.out.println("Inside 1st === "+value2);
Constructor"); }
} public static void main(String
Demo(int a) args[]){
{ Demo d1 = new Demo();
value1 = a;
Demo d2 = new Demo(30);
System.out.println("Inside 2nd
Constructor"); Demo d3 = new Demo(30,40);
} d1.display();
Demo(int a,int b) d2.display();
{ d3.display();
value1 = a; }
value2 = b; }
System.out.println("Inside 3rd
Constructor");
}
Difference between constructor and method in java
Java Copy Constructor

There is no copy constructor in java. But, we can copy


the values of one object to another like copy
constructor in C++.
public class Demo
{
int value1,a;
int value2,b;
Demo()
{
value1 = 10;
value2 = 20;
System.out.println("Inside 1st Constructor");
}
Output:
Demo(Demo s1)
Inside 1st Constructor
{ Inside 2nd Constructor
a = s1.value1; Value1 === 0
b = s1.value2; Value2 === 0
System.out.println("Inside 2nd Constructor"); Value1 === 10
} Value2 === 20
public void display()
{
System.out.println("Value1 === "+a);
System.out.println("Value2 === "+b);
}
public static void main(String args[]){
Demo d1 = new Demo();
Demo d3 = new Demo(d1);
Method overloading

If two or more method in a class have same name but


different parameters, it is known as method overloading.
Overloading always occur in the same class (unlike
method overriding).
Method overloading is one of the ways through which
java supports polymorphism. Method overloading can be
done by changing number of arguments or by changing
the data type of arguments. If two or more method have
same name and same parameter list but differs in
return type are not said to be overloaded method
Method Overloading can be done by 2 ways

Changing the data type of the Arguments


Changing number of arguments
Changing the data type of the
Arguments
public class Area
{
void find(int l, int b)
{
System.out.println("Area is"+(l*b)) ;
}
void find(int l, int b,int h)
{
System.out.println("Area is"+(l*b*h));
}
public static void main (String[] args)
{
Area ar = new Area();
ar.find(8,5); //find(int l, int b) is method is called.
ar.find(4,6,2); //find(int l, int b,int h) is called.
}

Output: Area is40 Area is48  
Changing number of arguments
  class Area
{
void find(long l,long b)
{
System.out.println("Area is"+(l*b)) ;
}
void find(int l, int b,int h)
{
System.out.println("Area is"+(l*b*h));
}
public static void main (String[] args)
{
Area ar = new Area();
ar.find(8,5); //automatic type conversion from find(int,int) to find(long,long) .
ar.find(2,4,6); //find(int l, int b,int h) is called.
}
}
 Output:Area is40 Area is48
Nesting of Methods

A method can be called by using only its name by


another method of same class. This is known as
Nesting of methods. Following example shows the
concept of nesting of methods.
class nesting
{
public static void main(String ar[])
{
test ob=new test(3,4);
ob.add();
}
}
class test
{
int m,n;
test(int x,int y) Output
{ Subtraction of numbers is-1
m=x; Addition is 7
n=y;
}
void sub()
{
System.out.println("Subtraction of numbers is"+(m-n));
}
void add()
{
sub();
System.out.println("Addition is "+(m+n));
}
}

Das könnte Ihnen auch gefallen