JAVA Constructors.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Saravanan.G.
More Object Concepts Chapter 4.  Our class is made up of several students and each student has a name and test grades  How do we assign the variables.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
Introduction to Java Classes and Objects. What is a class A class is description of a structure that contains both data and methods – Describes a set.
ECE122 Feb. 22, Any question on Vehicle sample code?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Classes One class usually represents one type of object –May contain its own member variables –May contain its own methods to operate on the member variables.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
Classes - Intermediate
Object Oriented Programming I ( ) Dr. Adel hamdan Part 03 (Week 4) Dr. Adel Hamdan Date Created: 7/10/2011.
Methods.
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Review – Primitive Data What is primitive data? What are 3 examples of primitive data types? How is a piece of primitive data different from an object?
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
J AVA P ROGRAMMING 2 CH 04: C LASSES, O BJECTS AND M ETHODS (II) 0.
Defining Your Own Classes II
Topic: Classes and Objects
GC211 Data structure Lecture 3 Sara Alhajjam.
3 Introduction to Classes and Objects.
Static data members Constructors and Destructors
Coming up Constructors Overloading With one parameter
Anatomy of a class Part II
This pointer, Dynamic memory allocation, Constructors and Destructor
More Object Oriented Programming
CSC 113 Tutorial QUIZ I.
An Introduction to Java – Part II
Object Oriented Programming in java
Unit-2 Objects and Classes
Assignment 7 User Defined Classes Part 2
S.VIGNESH Assistant Professor/CSE, SECE
S.VIGNESH Assistant Professor, SECE
Classes Lecture 7 from Chapter /1/11.
Assessment – Java Basics: Part 1
class PrintOnetoTen { public static void main(String args[]) {
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Chapter 10: Method Overriding and method Overloading
Constructors, GUI’s(Using Swing) and ActionListner
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Method Overriding and method Overloading
Anatomy of a class Part II
Class: Special Topics Overloading (methods) Copy Constructors
Unit-1 Introduction to Java
Constructors & Destructors
ITE “A” GROUP 2 ENCAPSULATION.
CS 240 – Advanced Programming Concepts
Previous Lecture: Today’s Lecture: Reading (JV):
Presentation transcript:

JAVA Constructors

Definition Constructors are used to initialize the object’s state. Like methods, a constructor also contains collection of statements(i.e. instructions) that are executed at time of Object creation. Each time an object is created using new keyword at least one constructor (it could be default constructor) is invoked to assign initial values to the data members of the same class. Constructor is invoked at the time of object or instance creation.

REGULATIONS A constructor doesn’t have a return type. The name of the constructor must be the same as the name of the class. Unlike methods, constructors are not considered members of a class. A constructor is called automatically when a new instance of an object is created.

Types of java constructors There are two types of constructors in java: Default constructor (no-argument constructor) Parameterized constructor

DEFAULT constructor A constructor is called "Default Constructor" when it doesn't have any parameter. If we don’t define a constructor in a class, then compiler creates default constructor(with no arguments) for the class. And if we write a constructor with arguments or no-argument then compiler does not create default constructor. Default constructor provides the default values to the object like 0, null etc. depending on the type.

DEFAULT CONSTRUCTOR Contd… import java.io.*;   class Student {     int num;     String name;     // this would be invoked while object     // of that class created.     Student()     {         System.out.println("Constructor is called");     } } Class MyClass {     public static void main (String[] args)     {         // this would invoke default constructor.         Student obj= new Student();           // Default constructor provides the default         // values to the object like 0, null         System.out.println(obj.name);         System.out.println(obj.num);     } }   OUTPUT Constructor is called null

DEFAULT CONSTRUCTOR Contd… import java.io.*;   class Student {         String name;     // this would be invoked while object     // of that class created.  } Class MyClass {     public static void main (String[] args)     {         // this would invoke default constructor.         Student obj= new Student();           // Default constructor provides the default         // values to the object like 0, null         System.out.println(obj.name);     } }   Class Student { } Class Student { Student() } OUTPUT Constructor is called null After Compilation (in .class file) Before Compilation

parameterized constructor class Student{ int id; String name; Student(int i, String n){ id = i; name = n; } void display(){ System.out.println(id+" "+name);} public static void main(String args[]){ Student s1 = new Student(100,“Varun"); Student s2 = new Student(101,"Alia"); s1.display(); s2.display(); } } OUTPUT Varun Alia

Constructor Overloading import java.io.*;  class Student {     // constructor with one argument      Student(String name)     {         System.out.println("Constructor with one " +                       "argument - String : " + name);     }       // constructor with two arguments      Student(String name, int age)          System.out.print("Constructor with two arguments : " +                 " String and Integer : " + name + " "+ age);      }      // Constructor with one argument but with different     // type than previous..      Student(long id)         System.out.println("Constructor with one argument : " +                                             "Long : " + id); }   class MyClass {     public static void main(String[] args)     {         // Creating the objects of the class named ‘Student '         // by passing different arguments         // Invoke the constructor with one argument of type 'String'.          Student obj1= new Student(“SACHIN");         // Invoke the constructor with two arguments          Student obj2= new Student(“SOURAV", 36);         // Invoke the constructor with one argument of type 'Long'.          Student obj3= new Student(8965645765);     } } Constructor with one argument - String : SACHIN Constructor with two arguments - String and Integer : SOURAV 36 Constructor with one argument - Long : 8965645765