0% found this document useful (0 votes)
3 views

CSC 211 Lesson 4 Part3 Constructors

The document provides an overview of constructors in Java, detailing their types, including default, parameterized, and copy constructors, as well as constructor overloading. It highlights the differences between constructors and methods, and explains the use of the 'this' keyword for referencing the current object and invoking constructors. Key rules for creating constructors and examples of their usage are also included.

Uploaded by

justusnyamaiamin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CSC 211 Lesson 4 Part3 Constructors

The document provides an overview of constructors in Java, detailing their types, including default, parameterized, and copy constructors, as well as constructor overloading. It highlights the differences between constructors and methods, and explains the use of the 'this' keyword for referencing the current object and invoking constructors. Key rules for creating constructors and examples of their usage are also included.

Uploaded by

justusnyamaiamin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Lecture 4: Constructors

Mr. Tarus
Outline

 Introduction

 Types of Constructors.

 Constructor Overloading in Java.

 Difference between constructor and method in Java.

 Copy constructor.

 this keyword
Constructors. ConstructDemo.java
 A special type of method which is used to initialize the object.
 Every time an object is created using the new() keyword, at least one constructor
is called.
 It is called constructor because it constructs the values at the time of object
creation.
 Java compiler creates a default constructor for every class created.
 Rules for creating Java constructor.
Class name and constructor name must be the same.
Do not have return types.
A Java constructor cannot be abstract, static, final, and synchronized
Constructors can use access modifiers. (private, public, protected, or default)
Types of Constructors

1. No-arg constructor / Default constructor

2. Parameterized constructor.

3. Copy Constructor
Java Default Constructor. Main12.java, ConstructDemo.java
 Constructors which does not have any parameters in the argument list.
 Syntax: <class_name>(){}
//Java Program to create and call a default constructor
class Bike1{
//creating a default constructor
Bike1() {
System.out.println("Bike is created by constructor");
}
public static void main(String args[]){
Bike1 b=new Bike1(); //calling a default constructor
}
}
Java Parameterized Constructor

 Accepts parameters in the argument list.

 ParameterizedCon.java

 student4.java
Constructor Overloading in Java. OverloadedCons.java
 Technique of having more than one constructor with different parameter lists.

 Each constructor performs a different task.

 The compiler differentiates them based on the number of parameters and their
return types.
// overload constructors void display(){
class OverloadedCons{ System.out.println(id+"
int id; "+name+" "+age);
String name; }
int age;
//creating two arg constructor public static void main(String
args[]){
OverloadedCons(int i,String n){
OverloadedCons s1 = new
id = i; OverloadedCons(111,"Karan");
name = n; } OverloadedCons s2 = new
//creating three arg constructor OverloadedCons(222,"James",25);
OverloadedCons(int i,String n,int a){ s1.display();
id = i; s2.display();
name = n; }
age=a; } }
Difference between constructor and method in Java
Java Constructor Java Method
 Used to initialize the state of an  Used to expose the behavior of an
object. object.

 No return type.  Must have return type


 Invoked implicitly / automatically.  Invoked explicitly by using object.

 Java compiler provides a default  Not provided by the compiler in any


constructor. case.

 Name must be same as the class  Name may or may not be same as the
name. class name.
Copy Constructor. CopyConstructor.java

 Copy constructor is passed with another object which copies the data available
from the passed object to the newly created object.
 In Java, there is no such inbuilt copy constructor available like in other
programming languages such as C++.
 In Java, we can create our own copy constructor by passing the object of the
same class to the other instance(object) of the class.
this keyword
• reference variable that refers to the current object.
Usage of Java this keyword
 Used to refer current class instance variables.

 Used to invoke current class method (implicitly).

 this() can be used to invoke current class constructor.

 this can be passed as an argument in the method call.

 this can be passed as an argument in the constructor call.

 this can be used to return the current class instance from the method.

 NoThisStudentDemo.java, ThisStudentDemo.java
this: to invoke current class method. Athis.java
 You may invoke the method of the current class by using the this keyword. If
you don't use the this keyword, compiler automatically adds this keyword
while invoking the method. Let's see the example
this() : to invoke current class constructor. TestThis5.java
 The this() constructor call can be used to invoke the current class constructor. It
is used to reuse the constructor. In other words, it is used for constructor
chaining.
Calling parameterized constructor from default constructor:

• TestThisE.java
Real usage of this() constructor call

 The this() constructor call should be used to reuse the constructor from the
constructor. It maintains the chain between the constructors i.e. it is used for
constructor chaining. Let's see the example given below that displays the actual
use of this keyword.
 TestThisDemo.java

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy