Understanding Java Constructors: Types, Rules & Examples

department of master of computer applications n.w
1 / 16
Embed
Share

Learn about Java constructors, including their types such as default, parameterized, overloaded, and copy constructors. Explore the rules for creating Java constructors and see examples of how to create and call them in Java programs.

  • Java Constructors
  • Constructor Types
  • Java Programming
  • Object-Oriented Programming

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Department of Master of Computer Applications St. Joseph s College (Autonomous) Tiruchirappalli - 2 Semester Semester II CONSTRUCTORSIN JAVA Programme Programme MCA Course Code Course Code 17PCA3113 Course Course PROGRAMMING IN JAVA Dr. L. AROCKIAM, Associate Professor Department of Computer Science St. Joseph s College (Autonomous) Tiruchirappalli-620002

  2. CONSTRUCTORS 6/2/2025 Codes are similar to the method Called when an instance of the class is created At the time of calling constructor, memory for the object is allocated in the memory A special type of method which is used to initialize the object Whenever an object is created using the new() keyword, at least one constructor is called 2

  3. There are two types of constructors in Java: No-arg constructor 6/2/2025 Parameterized constructor Note: Constructor constructs the values at the time of object creation Not necessary to write a constructor for a class Java compiler creates a default constructor if your class doesn't have any 3

  4. RULESFORCREATING JAVACONSTRUCTOR 6/2/2025 Constructor name must be the same as its class name A Constructor must have no explicit return type A Java constructor cannot be abstract, static, final, and synchronized 4

  5. TYPESOFCONSTRUCTORS 6/2/2025 Default Constructor Parameterized Constructor Overloaded Constructor Copy Constructor (Copying the values of one object into another) 5

  6. JAVA DEFAULT CONSTRUCTOR A constructor is called "Default Constructor" when it doesn't have any 6/2/2025 parameter. Eg. C1 ob= new C1(); 6

  7. //Java Program to create and call a default constructor EXAMPLE class Bike1 6/2/2025 In this example, we are creating the { //creating a default constructor no-arg constructor in the Bike class Bike1(){System.out.println("Bike is created"); } It will be invoked at the time of object Class Cal { public static void main(String args[]) creation. { //calling a default constructor Bike1 b=new Bike1(); Rule: If there is no constructor in a class, compiler automatically creates a default constructor. } } OutPut Bike is created 7

  8. JAVA PARAMETERIZED CONSTRUCTOR 6/2/2025 A constructor which has a specific number of parameters is called a parameterized constructor. Used to provide different values to distinct objects. In the following example, we have created the constructor of Student class that have two parameters 8

  9. EXAMPLE Class Call { public static void main(String args[]) { //creating objects and passing values Student4 s1 = new Student4(111,"Karan"); Student4 s2 = new Student4(222,"Aryan"); //calling method to display the values of object s1.display(); s2.display(); } } Output: 111 Karan 222 Aryan //Java Program to demonstrate the use of the parameterized const ructor. class Student4 { int id; String name; //creating a parameterized constructor Student4(int i, String n) { id = i; name = n; } //method to display the values void display() { System.out.println(id+" "+name); } } 6/2/2025 9

  10. CONSTRUCTOR OVERLOADINGIN JAVA 6/2/2025 In Java, a constructors can be be overloaded like Java methods. Constructor overloading is a technique of having more than one constructor with different parameter lists. They are arranged in a way that each constructor performs a different task. They are differentiated by the compiler by the number of parameters in the list and their types. 10

  11. //creating three arg constructor Student5(int I, String n, int a) EXAMPLE { id = i; name = n; age=a; } void display() { System.out.println(id+" "+name+" "+age); } } Class Call { public static void main(String args[]){ Student5 s1 = new Student5(111,"Karan"); Student5 s2 = new Student5(222,"Aryan",25); s1.display(); s2.display(); } 6/2/2025 Example of Constructor Overloading //Java program to overload constructors class Student5{ int id; String name; int age; //creating two arg constructor Student5(int I, String n) { id = i; name = n; } 11

  12. JAVA COPY CONSTRUCTOR 6/2/2025 There is no copy constructor in Java. However, we can copy the values from one object to another like copy constructor in C++. There are many ways to copy the values of one object into another in Java. They are: By using a constructor By assigning the values of one object into another By clone() method of Object class 12

  13. EXAMPLE //Java program to initialize the values from one object to another object. class Student6 { int id; String name; Student6(int I, String n) //constructor to initialise integer and string { id = i; name = n; } Student6(Student6 s) //constructor to initialize another object { id = s.id; name =s.name; } 6/2/2025 void display() {System.out.println(id+" "+name);} Class Call { public static void main(String args[]) { Student6 s1 = new Student6(111,"Karan"); Student6 s2 = new Student6(s1); s1.display(); s2.display(); } } Output: 111 Karan 111 Karan 13

  14. COPYINGVALUESWITHOUTCONSTRUCTOR We can copy the values of one object into another by assigning the 6/2/2025 objects values to another object. In this case, there is no need to create the constructor. 14

  15. EXAMPLE Class Call { public static void main(String args[]) { Student7 s1 = new Student7(111,"Karan"); Student7 s2 = new Student7(); s2.id=s1.id; s2.name=s1.name; s1.display(); s2.display(); } } class Student7 { int id; String name; Student7(int I, String n) { id = i; name = n; } 6/2/2025 void display() { System.out.println(id+" "+name); } } Output: 111 Karan 111 Karan 15

  16. 6/2/2025 Thank You 16

More Related Content