Difference bettween constructor and method
Constructor | Method |
1. Name of constructor must be same with the name of class | 1. For method there is no such type requirement.Methods name can be any kind depends on person choice. |
2.In constructor chaining there is no explicitly constructor. Constructor is invoked implicitly | 2. Method is invoked explicitly |
3. Constructor does not have any return type | 3. In method there can be return type and return something |
4. Constructor can provides a default constructor if you don't have any constructor | 4. java compiler can't provide method |
Java OOPs Concept - Consstructors
Constructor
Java allows objects to initialize themselves when they are created. This automatic initialization is performed through the use of a constructor. A constructor is a spacial type of method which is initializes an object immediately upon creation.
Simple rules for creating constructor:
constructor looks like strange because of
1. There are no return type class . It happens because the implicit type of a class , contractor is the class type itself.
2. Constructor name must be the same name as its own class name.
Constructor Types:
There are two types of Constructor
1. Default constructor
2. Parameterized constructor
Default Constructor
A constructor that have no parameter is known as default constructor. it is auto create class if you don't create this type of class.
package com.tariqul.StudentDetails;
public class StudentRegistration{
private String Name;
Private String Address;
. . . . . . .. . . .. . .. . . . . . ..
StudentRegistration(){
}
}
if you don't create any constructor then compiler create default constructor automatically. Depending on your attributes type, defaults constructor provides null or 0 or any other defaults value.
package com.tariqul.StudentDetails;
public class StudentRegistration{
private String Name;
Private String Address;
. . . . . . .. . . .. . .. . . . . . ..
}
when you do this, It provides you some default value. as like
package com.tariqul.StudentDetails;
public class StudentRegistration{
private String Name;
Private String Address;
. . . . . . .. . . .. . .. . . . . . ..
StudentRegistration(){
Name = " ";
Address = " ";
}
}
Parameterized constructor
We can pass values by using parameter .
package com.tariqul.StudentDetails;
public class StudentRegistration{
private String Name;
Private String Address;
. . . . . . .. . . .. . .. . . . . . ..
StudentRegistration(String name, String address ){
Name = name;
Address = address;
}
}
you can get the value by calling this constructor.
StudentRegistration st_registration= new StudentRegistration("Tariqul", "A");
when the object is created , it handed over "Tariqul" and "A" to the constructor .
Java allows objects to initialize themselves when they are created. This automatic initialization is performed through the use of a constructor. A constructor is a spacial type of method which is initializes an object immediately upon creation.
Simple rules for creating constructor:
constructor looks like strange because of
1. There are no return type class . It happens because the implicit type of a class , contractor is the class type itself.
2. Constructor name must be the same name as its own class name.
Constructor Types:
There are two types of Constructor
1. Default constructor
2. Parameterized constructor
Default Constructor
A constructor that have no parameter is known as default constructor. it is auto create class if you don't create this type of class.
package com.tariqul.StudentDetails;
public class StudentRegistration{
private String Name;
Private String Address;
. . . . . . .. . . .. . .. . . . . . ..
StudentRegistration(){
}
}
if you don't create any constructor then compiler create default constructor automatically. Depending on your attributes type, defaults constructor provides null or 0 or any other defaults value.
package com.tariqul.StudentDetails;
public class StudentRegistration{
private String Name;
Private String Address;
. . . . . . .. . . .. . .. . . . . . ..
}
when you do this, It provides you some default value. as like
package com.tariqul.StudentDetails;
public class StudentRegistration{
private String Name;
Private String Address;
. . . . . . .. . . .. . .. . . . . . ..
StudentRegistration(){
Name = " ";
Address = " ";
}
}
Parameterized constructor
We can pass values by using parameter .
package com.tariqul.StudentDetails;
public class StudentRegistration{
private String Name;
Private String Address;
. . . . . . .. . . .. . .. . . . . . ..
StudentRegistration(String name, String address ){
Name = name;
Address = address;
}
}
you can get the value by calling this constructor.
StudentRegistration st_registration= new StudentRegistration("Tariqul", "A");
when the object is created , it handed over "Tariqul" and "A" to the constructor .
Difference between constructor and method
Difference Between Array And ArrayList
There are some major difference between array and array list though both array and array list are use to store elements
In one word , Array is a limited size and can save similar data typed elements and
Array List is a dynamically representation of Array, Array List are Stores object only and resizable.
you can't change the size of array, but array list can re-size itself when need.
Difference :
Array is the collection of values of same data type
1. the variables in an array is called array elements : Instead of declaring individual variables
2. Every array is the reference type of data type.
Array list is a class which store only objects and it is resizable
ArrayList AR= new ArrayList();
The methods of array list classes are
1) Add
2) Remove
3) Clear
4) Insert
5) TrimToSize
6) Reverse
7) Sort
In one word , Array is a limited size and can save similar data typed elements and
Array List is a dynamically representation of Array, Array List are Stores object only and resizable.
you can't change the size of array, but array list can re-size itself when need.
Difference :
Array is the collection of values of same data type
1. the variables in an array is called array elements : Instead of declaring individual variables
2. Every array is the reference type of data type.
Array list is a class which store only objects and it is resizable
ArrayList AR= new ArrayList();
The methods of array list classes are
1) Add
2) Remove
3) Clear
4) Insert
5) TrimToSize
6) Reverse
7) Sort
Android Tutorial
Android application development is most popular and become more attractive development tools day by day for the programmer.