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

Constructor in Java

The document discusses constructors in Java classes. It defines constructors as methods that have the same name as the class and do not have a return type. Constructors are used to initialize object attributes and can be overloaded. There are default constructors generated by the compiler if none are defined, and parameterized constructors that take arguments. The this() and super() keywords allow communication between constructors in a class or its parent class.

Uploaded by

Vineet Pathak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Constructor in Java

The document discusses constructors in Java classes. It defines constructors as methods that have the same name as the class and do not have a return type. Constructors are used to initialize object attributes and can be overloaded. There are default constructors generated by the compiler if none are defined, and parameterized constructors that take arguments. The this() and super() keywords allow communication between constructors in a class or its parent class.

Uploaded by

Vineet Pathak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Module – 4

Constructor
class class_name
{
class_name( )
{
body of constructor
}
}

Characterstics:-
 must have the same name as class name
 method does not have any return_type not even void
 used to initialized the instance variables of an object
 types are default constructor and parameterized constructor
 compiler creates a default constructor if we does not define any
constructor
 it can be overloaded
 use this( ) to communicate from one constructor to another
constructor in the same class
 use super( ) to communicate with super class
Default Constructor
import java.util.*;
class Compute
{
Scanner sc = new Scanner(System.in);
int a,b;
Compute( )
{
System.out.println(“Enter the value of a and b: ”);
a=sc.nextInt( );
b=sc.nextInt( );
}
void display( )
{
System.out.println(“You Entered:- ”);
System.out.println(“a = ”+a+“ b = ”+b);
}
}
class MCompute
{
public static void main(String args[])
{
Compute C=new Compute( );
C.display( );
}
}
Parameterised constructor

class class_name
{
variable declaration
class_name(arg1,arg2,…,argN);
method declaration
}
class main_class_name
{
public static void main(String args[])
{
class_name object_name = new class_name(val1,val2,…valN);
}
}

example:-

class Compute
{
int a,b;
Compute( int x, int y)
{
a=x;
b=y;
}
void display( )
{
System.out.println(“Your value:-”);
System.out.println(“a = ”+a+“ b = ”+b);
}
}
class MCompute
{
public static void main(String args[])
{
Compute C=new Compute(11,12 );
C.display( );
}
}

//passing user input value


class MCompute
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter two integer number :- ”);
int m = sc.nextInt( );
int n = sc.nextInt( );
Compute C=new Compute(m , n );
C.display( );
}
}
Constructor Overloading

class Area
{
int A;
Area(int s)
{
A=s*s;
System.out.println(“Side of square = ”+s);
System.out.println(“Area of square = ”+A);
}
Area(int l, int b)
{
A=l*b;
System.out.println(“Length of rectangle = ”+l);
System.out.println(“Breadth of rectangle = ”+b);
System.out.println(“Area of Rectangle = ”+A);
}
}
class MArea
{
public static void main(String args[])
{
Area obj1 = new Area(10);
Area obj2 = new Area(5,6);
}
}
use of this( )
class Val
{
Val( )
{
System.out.println(“This is default constructor”);
}
Val( int v1)
{
this( );
System.out.println(“ single parameterized constructor”);
System.out.println(“v1 = ”+v1);
}
Val( int v1, int v2)
{
this( v1);
System.out.println(“two parameterized constructor”);
System.out.println(“v1 = ”+v1+ “ v2 = ”+v2);
}
}
class MVal
{
public static void main(String args[])
{
Val v = new Val(40,10);
}
}

Modifiers
 Access Specifier
o private
o public
o protected
o default
 no access specifier
o static
o final

Method overloading

class class_name
{
return_type method_name(arg1)
{
-----------
}
return_type method_name(arg1, arg2,..)
{
----------
}
}

example:-

class ABC
{
int A;
void area(int s)
{
A=s*s;
System.out.println(“Side of square = ”+s);
System.out.println(“Area of square = ”+A);
}
void area(int l, int b)
{
A=l*b;
System.out.println(“Length of rectangle = ”+l);
System.out.println(“Breadth of rectangle = ”+b);
System.out.println(“Area of Rectangle = ”+A);
}
}
class MABC
{
public static void main(String args[])
{
ABC obj = new ABC( );
obj.area(10);
obj.area(10,20);
}
}

example2:-
class xyz
{
int g;
void greatest(int a, int b)
{
g=a>b?a:b;
System.out.println(“a = ”+a+“b = ”+b);
System.out.println(“Greatest=”+g);
}
void greatest(int x, int y, int z)
{
g=x>y?x:y;
g=g>z?g:z;
System.out.println(“x = ”+x+“y = ”+y+“z = ”+z);
System.out.println(“Greatest=”+g);
}

}
class Abc
{
public static void main(String args[])
{
xyz ob = new xyz( );
ob.greatest(5,6);
ob.greatest(7,5,6);
}
}

You might also like