0% found this document useful (0 votes)
6 views12 pages

1.5 Study Materials Constructors, Methods

The document provides an overview of constructors and methods in Java, explaining their definitions, types, and examples. It details default and parameterized constructors, method overloading, and the use of the 'this' keyword. Additionally, it includes code examples demonstrating how to implement these concepts in Java programming.

Uploaded by

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

1.5 Study Materials Constructors, Methods

The document provides an overview of constructors and methods in Java, explaining their definitions, types, and examples. It details default and parameterized constructors, method overloading, and the use of the 'this' keyword. Additionally, it includes code examples demonstrating how to implement these concepts in Java programming.

Uploaded by

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

UNIT No.

II III

20CSPC301
OBJECT ORIENTED PROGRAMMING
(Common to CSE , IT & CSBS)

UNIT No. 1

INTRODUCTION TO OOP AND JAVA


FUNDAMENTALS
1.5 Constructors and Methods

Version: 1.XX

CONSTRUCTORS
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE , IT & CSBS)

● Constructors are a special member function, it has the same name as the classname.
● Java constructors are invoked when their objects are created.
● Every class has a constructor when we don't explicitly declare a constructor for any java
class the compiler creates a default constructor for that class which does not have any return
type.
● The constructor in Java cannot be abstract, static, final or synchronized and these
modifiers are not allowed for theconstructor.
There are two types of constructors:
1. Default constructor (no-arg constructor)
2. Parameterizedconstructor

Default constructor (no-arg constructor)


A constructor having no parameter is known as default constructor and no-arg constructor.
Example
/* Here, Box uses a constructor to initialize the
dimensions of a box.
*/
class Box
{
double width;
double height;
double depth;
Box() {
System.out.println("Constructing Box");
width = 10;
height = 10;
depth =10;
}
// compute and return volume
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE , IT & CSBS)

double volume()
{
return width * height * depth;
}
}
class BoxDemo6
{
public static void main(String args[])
{
Box mybox1 = new Box();
Box mybox2 = new Box();
doublevol;
vol = mybox1.volume();
System.out.println("Volume is " + vol);
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
Output:
Constructing Box
Constructing Box
Volume is1000.0
Volume is1000.0
Parameterized Constructors
A constructor which has a specific number of parameters is called a parameterized constructor.
Parameterized constructor is used to provide different values to the distinct objects.
Example
/* Here, Box uses a parameterized constructor to initialize the dimensions of a box.
*/
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE , IT & CSBS)

class Box
{
double width;
double height;
double depth;
Box(double w, double h, double d)
{
width = w;
height =h;
depth = d;
}
// compute and return volume
double volume()
{
return width * height * depth;
}
}
class BoxDemo7
{
public static void main(String args[])
{
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box(3, 6, 9);
double vol;
vol = mybox1.volume();
System.out.println("Volume is " + vol);
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE , IT & CSBS)

Output:
Volume is 3000.0
Volume is 162.0

Overloading
Constructors Example:
/* Here, Box defines three constructors to initialize the dimensions of a box various ways.
*/
class Box
{
double width;
double height;
double depth;
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
Box()
{
width = -1;
height = -1;
depth = -1;
}
Box(double len)
{
width = height = depth = len;
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE , IT & CSBS)

}
double volume()
{
return width * height * depth;
}
}
classOverloadCons
{
public static void main(String args[])
{
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
doublevol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
}}
Output:
Volume of mybox1 is 3000.0 Volume of
mybox2 is -1.0
Volume of mycube is 343.0
METHODS
● type specifies the type of data returned by the method. This can be any valid type, including class
types that you create.
● If the method does not return a value, its return type must be void.
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE , IT & CSBS)

● The name of the method is specified byname.


● The parameter-list is a sequence of type and identifier pairs separated by commas. Parameters are
essentially variables that receive the value of the arguments passed to the method when it is called.
If the method has no parameters, then the parameter list will be empty.
● Methods that have a return type other than void return a value to the calling routine using the
following form of the returnstatement.

Syntax:
type name(parameter-list)
{
// body of method
}
Example
// This program includes a method inside the boxclass.
class Box
{
double width;
double height;
double depth;
void volume()
{
System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}
class BoxDemo3
{
public static void main(String args[])
{
Box mybox1 = newBox();
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE , IT & CSBS)

Box mybox2 = newBox();


mybox1.width = 10;
mybox1.height = 20;
mybox1.depth =15;
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth =9;
mybox1.volume();
mybox2.volume();
}}
Output:
Volume is 3000.0
Volume is 162.0

Returning a Value (using method)


Example
class Box
{
double width;
double height;
double depth;
volume double volume()
{
return width * height * depth;
}}
class BoxDemo4 {
public static void main(String args[])
{
Box mybox1 = new Box();
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE , IT & CSBS)

Box mybox2 = new Box(); double vol;


mybox1.width = 10;
mybox1.height = 20;
mybox1.depth =15;
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth =9;
vol = mybox1.volume();
System.out.println("Volume is " + vol);
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
Output:
Volume is 3000
Volume is 162

The this Keyword


this keyword is used to refer to the object that invoked it. this can be used inside any method to refer to the
current object. That is, this is always a reference to the object on which the method was invoked. this() can
be used to invoke the current class constructor.
Syntax:
Box(double w, double h, double d)
{
this.width = w;
this.height = h;
this.depth = d;
}
Example
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE , IT & CSBS)

class Student
{
int id;
String name;
student(int id, String name)
{
this.id = id;
this.name =name;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Student stud1 = new Student(01,"Tarun");
Student stud2 = newStudent(02,"Barun");
stud1.display();
stud2.display();
}
}
Output:
01 Tarun
02 Barun
Overloading Methods
When two or more methods within the same class that have the same name, but their parameter
declarations are different. The methods are said to be overloaded, and the process is referred to as method
overloading. Method overloading is one of the ways that Java supports polymorphism.
There are two ways to overload the method in java
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE , IT & CSBS)

1. By changing number of arguments


2. By changing the datatype

Example
// Demonstrate method overloading.
classOverloadDemo
{
void test()
{
System.out.println("No parameters");
}
void test(int a)
{
System.out.println("a: " + a);
}
void test(int a, int b)
{
System.out.println("a and b: " + a + " " + b);
}
double test(double a)
{
System.out.println("double a: " + a);
return a*a;
}
}
class Overload
{
public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
20CSPC301
OBJECT ORIENTED PROGRAMMING (Common to CSE , IT & CSBS)

double result;
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}
}
Output:
No parameters
a: 10
a and b: 10 20
double a: 123.25
Result of ob.test(123.25): 15190.5625

You might also like