java constructors with examples - quontra solutions

12
Call : 44 (0)20 3734 1498 / 99, Mail Us: [email protected] Quontra Solutions by Java Constructors

Upload: quontra-solutions

Post on 17-Jul-2015

108 views

Category:

Education


3 download

TRANSCRIPT

Page 1: Java Constructors with examples  - Quontra Solutions

Call : 44 (0)20 3734 1498 / 99, Mail Us: [email protected]

Quontra Solutions

by

Java Constructors

Page 2: Java Constructors with examples  - Quontra Solutions

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]

Page 3: Java Constructors with examples  - Quontra Solutions

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]

Page 4: Java Constructors with examples  - Quontra Solutions

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]

Page 5: Java Constructors with examples  - Quontra Solutions

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]

Page 6: Java Constructors with examples  - Quontra Solutions

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]

Page 7: Java Constructors with examples  - Quontra Solutions

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]

Page 8: Java Constructors with examples  - Quontra Solutions

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]

Page 9: Java Constructors with examples  - Quontra Solutions

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]

Page 10: Java Constructors with examples  - Quontra Solutions

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]

Page 11: Java Constructors with examples  - Quontra Solutions

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]

Page 12: Java Constructors with examples  - Quontra Solutions