1.5 Study Materials Constructors, Methods
1.5 Study Materials Constructors, Methods
II III
20CSPC301
OBJECT ORIENTED PROGRAMMING
(Common to CSE , IT & CSBS)
UNIT No. 1
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
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)
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)
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)
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