lecture 3
lecture 3
• A constructor which does not have any parameter is called default constructor.
Class A
{ no any parameter
A()
{
}
}
class A
{
Int a; string b; boolean c;
A()
{
a=10; b=“rahul”; c=“true”;
}
void disp()
{
System.out.print(a b c)
}
}
Class B
public static void main(String args[])
{
A b=new A();
b.disp();
}
}
Rule: If there is no constructor in a class,
compiler automatically creates a default
constructor.
Example of parameterized constructor
class A {
int x,y;
A( int a, int b)
{
x =a; y=b;
}
void display()
{
System.out.println(x+" "+y);
}
}
class B
public static void main(String args[]){
A r =new A(10,20)
r.display();
}
}
Java Copy Constructor
• There is no copy constructor in java. But, we can copy the values of one object to another like copy constructor in C++.
• There are many ways to copy the values of one object into another in java. They are:
• By constructor
• By assigning the values of one object into another
Copy constructor:- whenever we pass object reference to the constructor then it is called copy constructor
//....................................................Example of copy constructor
class A {
int a, string b;
A()
{
a =10; b=“ansh”;
S.O.P(a+” ” +b);
}
A(A ref)
{
a=ref.a;
b=ref.b;
S.O.P(a+” ” +b);
}
}
class B
{
public static void main(String args[])
{
A r =new A();
A r2 =new A();
}
}
Private constructor:- it is possible to write a constructor as a private
but we cant access private constructor outside the class
//....................................................Example of Private constructor
class A
{
int a, double b, string c;
Private A()
{
a =10; b=30.56; c=“ansh”;
S.O.P(a+” ” +b ” ” +c);
}
}
public static void main(String args[])
{
A r =new A();
}
Constructor Overloading in Java
• Constructor overloading is a technique in Java
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.
//..........constructor overloading
class A
{
int a, double b, string c;
A()
{
a =10; b=30.56; c=“ansh”;
}
A(int x)
{
a = x;
}
A(double y, string z)
{
b = y; c = z;
}
}
class B
class A
{
static
{
System.Out.Println(”raunak”);
}
public static void main(String args[])
Final Keyword In Java
The final keyword in java is used to restrict the
user. The java final keyword can be used in
many context. Final can be:
• variable
• method
• class
• The final keyword can be applied with the
variables, a final variable that value cant be
changed.
• It can be initialized in the constructor only.
• The blank final variable can be static also
which will be initialized in the static block only.
If you make any variable as final, you cannot change
the value of final variable(It will be constant).
class Bike9{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
}//end of class
Output:Compile Time Error
Java final method
If you make any method as final, you cannot override it.
class Bike{
final void run()
{
System.out.println("running");}
}
class Honda extends Bike{
void run()
{
System.out.println("running safely with 100kmph");
}
public static void main(String args[])
{
Honda honda= new Honda();
honda.run();
}
}
Output:Compile Time Error
Java final class
If you make any class as final, you cannot extend it.
Bike10()
{
speedlimit=70;
System.out.println(speedlimit);
}