Class - Constructor This
Class - Constructor This
class Rectangle
1
{
double length ;
double breadth;
length=l;
breadth=b;
System.out.println(*Area is “+area);
A class with datamembers and methods has no life and memory is not allocated
to data members with class template or class structure declaration. So, we have
to use objects to allocate a memory to data members of class.
Creating an object:
The new operator creates an object of the specified class and returns a reference
to the object.
Syntax:
2
Declare the object and allocate memory by using new operator
Syntax: objectname.member;
class Rectangle
double length ;
double breadth;
length=l;
breadth=b;
System.out.println(*Area is “+area);
3
}
class Rectangledemo
{
public static void main(String args[])
{
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
r1.read(11,5);
r2.read(3,15);
r1.findarea( );
r2.findarea( );
}
}
Constructors:
Constructor is a special type of method that is used to initialize the object.
Constructors are invoked automatically when objects are created.
Rules for creating constructor:
In order to create a constructor we have to observe following rules
Constructor name must be similar to class name.
It should be declared as public.
They do not have any return types even void.
Constructor can be overloaded.
Constructor cannot be inherited.
There are two different types of constructors available in java. they are
1).Default constructor
2).Parameterized constructor
Default constructor: A constructor that can accepts no parameters is called Default
constructor . (OR)
A constructor without any parameters is called Default constructor.
If no constructor is defined for a class then java system automatically generates a special
constructor called default constructor.
Syntax:
4
class classname
{
classname ( )
{
Statements ;
}
----------
----------
}
Example:
class triangle
double base;
double height;
triangle( )
base=10.54;
height=20.68;
void findarea( )
System.out.println("Area is "+area);
class triangledefault
5
t1.findarea();
t2.findarea();
disadvantage:
In order to initialize an object with various values we have to use the Parameterized
constructor.
Syntax:
class classname
classname (parameters )
Statements ;
----------
---------
Example:
6
class triangle
double base ;
double height;
base = b;
height = h;
void findarea( )
class triangleparameter
t1.findarea();
t2.findarea();
}
7
Constructor Overloading in Java:
Defining two or more constructors with same name in a class but with different
signature by changing either number of parameters or type of parameters is
known as constructor Overloading. OR
Constructor overloading is a technique in which a class can have any number of
constructors that differ in parameter lists. The compiler differentiates these
constructors by taking into account the number of parameters in the list and their
type.
class triangle
{
double base ;
double height;
triangle( )
{
base=10.5;
height=20.8;
}
class triangleconover
{
public static void main(String args[])
{
8
triangle t1=new triangle();
t1.findarea();
triangle t2=new triangle(32.37,20.67);
t2.findarea();
}
}
In java, Methood Overloading is not possible by changing the return type of the
method.
In this example, we have created two overloaded methods, first sum method performs
addition of two numbers and second sum method performs addition of three numbers.
class Calculation
{
void sum(int a,int b)
{
System.out.println(a+b);
}
void sum(int a,int b,int c)
{
System.out.println(a+b+c);
}
}
}
9
2)Example of Method Overloading by changing data type of argument
In this example, we have created two overloaded methods that differs in data type. The first
sum method receives two integer arguments and second sum method receives two double
arguments.
class Calculation2
{
void sum(int a,int b)
{
System.out.println(a+b);
}
void sum(double a,double b)
{
System.out.println(a+b);
}
}
}
There can be a lot of usage of java this keyword. In java, this is a reference variable that refers
to the current object.
The this keyword can be used to refer current class instance variable.
Whenever data members (instance variables) and local parameters with same name
then java compiler gets an ambiguity and parameters are not assigned to data
members.
Whenever data members (instance variables) and local parameters with same name
then data members are referred by using this keyword or this pointer .
In the above example, parameters (formal arguments) and instance variables are with same
name that is why we are using this keyword to distinguish between local variables and instance
variables.
class Student
{
int id;
String name;
Student12(int i,String n)
{
id = i;
name = n;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Student e1 = new Student12(101,"ANIL");
Student e2 = new Student12(102,"RAJU");
e1.display();
e2.display();
}
}
Output 101 ANIL
102 RAJU
12
this() can be used to invoked current class constructor.
The this() constructor call can be used to invoke the current class constructor (constructor
chaining).
This approach is better if you have many constructors in the class and want to reuse that
constructor.
class Student13
{
int id;
String name;
Student13()
{
System.out.println("default constructor is invoked");
}
13
102 RAJU
The this keyword can be used to invoke current class method (implicitly).
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
class S
{
void m()
{
System.out.println("method is invoked");
}
void n()
{
this.m(); //no need
}
void p()
{
n(); //complier will add this to invoke n() method as this.n()
}
public static void main(String args[])
{
S s1 = new S();
s1.p();
}
}
14