Upload
quontra-solutions
View
112
Download
3
Embed Size (px)
Citation preview
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 : 44 (0)20 3734 1498 / 99, Mail Us: [email protected]
Types of Constructors
• Default constructor (no-arg constructor)• Parameterized constructor
Types of constructors
Default constructor Parameterized constructor
Call : 44 (0)20 3734 1498 / 99, Mail Us: [email protected]
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 : 44 (0)20 3734 1498 / 99, Mail Us: [email protected]
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 : 44 (0)20 3734 1498 / 99, Mail Us: [email protected]
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 : 44 (0)20 3734 1498 / 99, Mail Us: [email protected]
Constructor Overloading
• Constructors are methods that can be overloaded, just like any othermethod in a class.
• Constructors having the same name with different parameter list iscalled constructor overloading.
Call : 44 (0)20 3734 1498 / 99, Mail Us: [email protected]
Topics Covered during Training
• Classes, Objects, Keywords, Methods• Exception Handling• String Handling• Multithearding• AWT• Swing• Applet• Collections• Hibernate• Struts etc………………
Call : 44 (0)20 3734 1498 / 99, Mail Us: [email protected]
Quontra Solutions Offers:
Data WarehouseOBIEE, Cognos, Informatica
Microsoft CoursesC# .NET, ASP .NET, VB .NET
& etc….
Programming CoursesCore Java, Adv Java,
J2EE, & etc….
Testing ToolsManual, QTP, Selenium,
Load Runner
Salesforce CRMSalesforce CRM, ADMIN,
Developer
Networking CoursesLinux, Unix, Sun Solaris,
CCNA
Mobile AppsAndroid, IOS, Cloud
Computing
Data BaseSQL PL/SQL, SQL server DBA, ORACLE 12C DBA
Other Courses…Business Analyst, Hadoop,
QlikviewCall : 44 (0)20 3734 1498 / 99, Mail Us: [email protected]
Contact Us
Call: 44 (0)20 3734 1498 / 99
Mail:[email protected]
Visit:www.quontrasolutions.co.uk
Call : 44 (0)20 3734 1498 / 99, Mail Us: [email protected]
Follow us
Facebook: https://www.facebook.com/quontrasolutionsuk
Twitter : https://twitter.com/QuontraSolution
Google+: https://plus.google.com/+QuontrasolutionsCoUk
Call : 44 (0)20 3734 1498 / 99, Mail Us: [email protected]