| Constructor | Method |
| Constructor
must have the same name as the class name. |
Method
name can be same or can not be same as the class name. |
| Constructor is used to initialize the variables. | Method
is used to show the behaviour of the object and we can write business logic. |
| Constructor does not return any type. | Method
must return any type otherwise It will be consider as constructor. |
| Constructor is
called when we create the object. Ex. A a = new A(); |
We need to called the
method explicitly when we require. Ex. A a = new A(); a.showMessage(); |
| If we don’t write constructor in Java class, then Java compiler consider the default constructor. |
In case of method,
Java compiler don’t act like constructor. |
Monday, 6 August 2018
Difference bet Constructor and Method in Java
Tuesday, 31 July 2018
Constructor
The Constructor is similar to method except constructor must have the same name as class name and it must not return any type. By default, every class have constructor whether we have declared or not. It is called when we create object using new keyword. for ex. A a = new A(). Constructor are used to initialize the objects. There are two types of constructor.
i. Default Constructor
ii. Parameterized Constructor.
Example of Default Constructor :
class XYZ{
XYZ(){
System.out.println("XYZ is created");
}
public static void main(String args[]){
XYZ b=new XYZ();
}
}
Example of Parameterized Constructor :
class XYZ{
int id;
String name;
XYZ(int i,String n){
id = i;
name = n;
}
void show(){
System.out.println(id+" "+name);
}
public static void main(String args[]){
XYZ x1 = new XYZ(1,"Mangesh");
XYZ x2 = new XYZ(2,"Harish");
x1.show();
x2.show();
}
}
Thursday, 12 May 2016
What is thread in java.
Every executable program is called as thread. and there are two ways to create a thread :
1. By extending Thread class.
2. By implementing Runnable method.
Example of By extending Thread class :
class ThreadDemo extends Thread{
public void run(){
System.out.println("Thread class extended");
}
public static void main(String[] args ){
ThreadDemo TD=new ThreadDemo ();
TD.start();
}
}
Example of By implementing Runnable method :
class ThreadDemo implements Runnable{
public void run(){
System.out.println("Runnable class Implement");
}
public static void main(String[] args ){
ThreadDemo TD=new ThreadDemo ();
TD.start();
}
}
Which is best thread.
If we extends Thread class then we can not extend another class. And if we implement Runnable interface and in future if we want to extend any class then we can extend if we implement Runnable interface.
1. By extending Thread class.
2. By implementing Runnable method.
Example of By extending Thread class :
class ThreadDemo extends Thread{
public void run(){
System.out.println("Thread class extended");
}
public static void main(String[] args ){
ThreadDemo TD=new ThreadDemo ();
TD.start();
}
}
Example of By implementing Runnable method :
class ThreadDemo implements Runnable{
public void run(){
System.out.println("Runnable class Implement");
}
public static void main(String[] args ){
ThreadDemo TD=new ThreadDemo ();
TD.start();
}
}
Which is best thread.
If we extends Thread class then we can not extend another class. And if we implement Runnable interface and in future if we want to extend any class then we can extend if we implement Runnable interface.
Subscribe to:
Posts (Atom)