Constructors in Java
Constructors in Java
Argumented constructor
A constructor that takes parameters is called argumented constructor.
Class Add
{
Int a,b;
Add(int a1,int b1)
{
System.out.println("default constructor");
a=a1;
b=b1;
}
Void sum()
{
Return a+b;
}
}
Class addition
{
Public static void main(String args[])
{
Add d=new Add(10,20);
System.out.println("sum="+d.sum());
}
}
Copy constructor
A copy constructor is a constructor that replicates an existing object. So it is called the copy constructor.
It takes a parameters as a reference to an object of the same class to which the constructor depend.
Syntax:
Class-name(class-name copy constructor name)
A a1=new A();
A b1=new A(a1);
Class copy
{
Int a,b;
Public copy(int a1,int b1)
{
a=a1;
b=b1;
}
public copy(copy c)
{
a=c.a;
b=c.b;
}
Void view()
{System.out.print("a="+a+"b="+b);
}
}
Class copytest
{
Public static void main(String ar[])
{
Copy test=new copy(10,20);
System.out.println("first constructor");
Test.view();
Copy test1=new copy(test);
System.out.println("second constructor");
Test1.view();
}
}
Access specifiers
Access specifiers determine which features of a class may be used by other classes also. In java they are
used to protect both a class variable and its methods when we declare them.
There are three types of access specifiers:
1. public
2. private
3. proteccted
public means ,it is accessible from all other classes. all classes can have public access specifier except
inner class.
Only objects of the same class can access a private variable or method. This is the highest degree
Of protection .they cannot be inherited by subclass and also not accessible in subclass.
Variable, methods and inner class are declared protected; it is accessible only from within its own class
and its subclass. It is not supported in higher version of java.
Static
The modifier static is used to specify that a method is a class method. A class method is a method that is
invoked without being bound to any specific objects of the class. These are associated with
itself not an individual objects. It is used to define a member that is common to all
objects and accessed without using a particular object. That is , the member
belongs to the class as a whole rather than the objects created from the class.
Since the static members and methods are associated with the class, the
variables and methods are called class variables and class methods clearly.
Class statictest
{
Static double multiply(double a,double b)
{
return(a*b);
}
Static double add(double a,double b)
{
return(a+b);
}
Static double division(double a,double b)
{
return(a/b);
}
}
System.out.println("addition="+y);
System.out.println("multiplication="+x);
System.out.println("division="+z);
}
}
In the above program object is not created but the method is called using class name.
// Demonstrate static variables, methods, and blocks.
class UseStatic {
static int a = 3;
static int b;
static
{
System.out.println("Static block initialized.");
b = a * 4;
}
As soon as the UseStatic class is loaded, all of the static statements are run. First, a
is setto 3, then the static block executes (printing a message), and finally, b
is initialized to a *4 or 12. Then main( ) is called, which calls meth( ),
passing 42 to x. The three println( )statements refer to the two static
variables a and b, as well as to the local variable x.
Outside of the class in which they are defined, static methods and variables can be
usedindependently of any object. To do so, you need only specify the
name of their classfollowed by the dot operator.
classname.method( )
Inner classes
Inner classes are classes that are declared within other classes. They are
also known as nested classes and provide additional clarity to programs.
The scope of an inner class is limited to the class that encloses it. The
objects of the inner class can access the members of the outer class. The
outer class can access the members of the inner class through an object of
the inner class.
Syntax:
<modifiers> class <classname>
{
<modifiers> class <innerclassname>
{
}
// other attributes and methods
}
void display(){
Inner in=new Inner();
in.msg();
}
public static void main(String args[]){
TestMemberOuter1 obj=new TestMemberOuter1();
obj.display();
}
}
OuterClassReference.new MemberInnerClassConstructor();
1. Nested classes represent a particular type of relationship that is it can access all the members (data members and methods) of the outer
class, including private.
2. Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
3. Code Optimization: It requires less code to write.
Method Overloading:
The method, which is having different number of arguments and/or different types of
arguments with the same name, is called method overloaded, and the process
is referred as method overloading. Method overloading is used when objects are
required to perform similar tasks but using different input parameters.
Method overloading is one of the ways that Java implements polymorphism.
While overloaded methods may have different return type, the return type
alone is insufficient to distinguish tow versions of a method. When Java
encounters a call to an overloaded method, it simply executes the version of
the method whose parameters match the arguments used in the call.
class Overload
{
public static void main(String args [])
{
OverloadDemo ob = new OverloadDemo();
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);
}}