Professional Documents
Culture Documents
OOP (O - O P S) : S Bject Riented Rogramming Ystem
OOP (O - O P S) : S Bject Riented Rogramming Ystem
OOP (O - O P S) : S Bject Riented Rogramming Ystem
SYSTEM)
1
CONSTRUCTOR
2
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All
rights reserved.
INTRODUCTION
Constructor is a block of codes similar to the
method. It is called when an instance of
the class is created.
At the time of calling constructor, memory for the
object is allocated in the memory.
It is a special type of method which is used to
initialize the object.
3
INTRODUCTION
Every time an object is created using the new()
keyword, at least one constructor is called.
It calls a default constructor if there is no
constructor available in the class. In such case,
Java compiler provides a default constructor by
default.
4
RULES FOR CREATING JAVA CONSTRUCTOR
5
TYPES OF JAVA CONSTRUCTORS
Parameterized constructor
6
JAVA DEFAULT CONSTRUCTOR
<class_name>(){}
7
EXAMPLE OF DEFAULT CONSTRUCTOR:WE ARE CREATING THE NO-ARG
CONSTRUCTOR IN THE BIKE CLASS. IT WILL BE INVOKED AT THE TIME
OF OBJECT CREATION.
Bike1(){System.out.println("Bike is created");}
//main method
}
8
}
9
JAVA PARAMETERIZED CONSTRUCTOR
10
EXAMPLE OF PARAMETERIZED CONSTRUCTOR:WE HAVE CREATED THE
CONSTRUCTOR OF STUDENT CLASS THAT HAVE TWO PARAMETERS. WE CAN
HAVE ANY NUMBER OF PARAMETERS IN THE CONSTRUCTOR.
15
JAVA COPY CONSTRUCTOR
16
WE ARE GOING TO COPY THE VALUES OF ONE
OBJECT INTO ANOTHER USING JAVA
CONSTRUCTOR.
//Java program to initialize the values from one object to another object.
class Student6{
int id;
String name;
//constructor to initialize integer and string
Student6(int i,String n){
id = i;
name = n;
}
//constructor to initialize another object
Student6(Student6 s){
id = s.id;
name =s.name;
} 17
void display(){System.out.println(id+" "+name);}
class Student7{
int id;
String name;
Student7(int i,String n){
id = i;
name = n;
}
Student7(){}
void display(){System.out.println(id+" "+name);}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All
rights reserved.
Create a class called Invoice that a hardware store might use to
represent an invoice for an item sold at the store. An Invoice should
include four pieces of information as instance variables‐a part
number(type String),a part description(type String),a quantity of the
item being purchased (type int) and a price per item (double).
Your class should have a constructor that initializes the four
instance variables. Provide a set and a get method for each instance
variable. In addition, provide a method named getInvoice Amount
that calculates the invoice amount (i.e., multiplies the quantity by the
price per item), then returns the amount as a double value.
If the quantity is not positive, it should be set to 0. If the price per
item is not positive, it should be set to 0.0. Write a test application
named InvoiceTest that demonstrates class Invoice’s capabilities.
21
22