Our trainer’s having vast experience in real time environment. If anyone has a dream for their career in software programming, then go for java because it is a popular route to establish and fulfill your dreams.
We offer the best quality and affordable training, so you get trained from where you are, from our experienced instructors, remotely using Webex / Gotomeeting.
Introduction to ArtificiaI Intelligence in Higher Education
Java constructors
1. Call : (404) 900-9988, (404) 990-3007, Mail Us: info@quontrasolutions.com
Quontra Solutions
by
Java Constructors
2. Constructor
• Constructor in java is a special type of method that is used to
initialize the object
• Java Constructor is invoked at the time of object creation
• They do not contain any return type
• Constructor name is same as class name
Call : (404) 900-9988, (404) 990-3007, Mail Us: info@quontrasolutions.com
3. Types of Constructors
• Default constructor (no-arg constructor)
• Parameterized constructor
Types of constructors
Default constructor Parameterized constructor
Call : (404) 900-9988, (404) 990-3007, Mail Us: info@quontrasolutions.com
4. Default Constructors
Example for default constructor:
class Cons{
cons(){
System.out.println("constructor is created");
}
public static void main(String args[]){
Cons c=new Cons();
}
}
Call : (404) 900-9988, (404) 990-3007, Mail Us: info@quontrasolutions.com
5. Parameterized Constructors
Example for parameterized constructor
class Student4{
int id;
String name;
Student4(int i,String n){
id = i;
name = n; }
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
s1.display();
s2.display(); }
}
Call : (404) 900-9988, (404) 990-3007, Mail Us: info@quontrasolutions.com
6. Difference between constructor and method
Constructor Method
Constructor is used to
initialize the state of an
object.
Method is used to expose
behaviour of an object.
Constructor must not have
return type
Method must have return
type.
Constructor is invoked
implicitly.
Method is invoked explicitly.
Constructor name must be
same as the class name.
Method name should not
be same as class name
Call : (404) 900-9988, (404) 990-3007, Mail Us: info@quontrasolutions.com
7. Constructor Overloading
• Constructors are methods that can be overloaded, just like any other
method in a class.
• Constructors having the same name with different parameter list is
called constructor overloading.
Call : (404) 900-9988, (404) 990-3007, Mail Us: info@quontrasolutions.com