0% found this document useful (0 votes)
37 views7 pages

Constructors in Java

The document discusses various Java concepts including constructors, access specifiers, static methods, inner classes, method overloading, and polymorphism. It provides examples and definitions for each concept. Constructors initialize objects upon creation and have the same name as the class. There are three types of constructors: default, parameterized, and copy constructors. Access specifiers determine class access and include public, private, and protected. Static methods belong to the class rather than objects and can only access static data. Inner classes are declared within other classes and can access members of the outer class. Method overloading involves methods with the same name but different parameters, allowing polymorphism.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
37 views7 pages

Constructors in Java

The document discusses various Java concepts including constructors, access specifiers, static methods, inner classes, method overloading, and polymorphism. It provides examples and definitions for each concept. Constructors initialize objects upon creation and have the same name as the class. There are three types of constructors: default, parameterized, and copy constructors. Access specifiers determine class access and include public, private, and protected. Static methods belong to the class rather than objects and can only access static data. Inner classes are declared within other classes and can access members of the outer class. Method overloading involves methods with the same name but different parameters, allowing polymorphism.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

constructor

A constructor is a method that initializes an object immediately upon creation. It has


two min properties.
a) It has same name as the class in which it resides.
b) It should not have any return type.

There are three types of constructors in java


1. default constructor.
2. Argumented constructor
3. Copy constructor
The constructor has no parameter is called default constructor. When we do not explictily define a
constructor for a class , then java creates a default constructor for the class.The default
constructor automatically initialize all instance variables.
Class Add
{
Int a,b;
Add()
{
System.out.println("default constructor");
a=10;
b=20;
}
Void sum()
{
System.out.println("sum="+(a+b));
}
}
Class addition
{
Public static void main(String args[])
{
Add d=new Add();
d.sum();
}
}

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.

Eg public static void main(String a[])


Which means thsis method as one that belongs to the entire class and not a part of any object of the
class. So interpreter uses this method before any objects is created.
If we declare variable as static, it make only one value of the variable exists for all instance of the class.
Eg. Static int x;
These variablea are called class variable.this variable is used even the class is never actually instantied.
Methods declared as static have several restrictions:
■ They can only call other static methods.
■ They must only access static data.
■ They cannot refer to this or super in any way. (The keyword super relates to
inheritance.)

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);
}
}

Public class mytest


{
Public static void main(String a[])
{
Double x=statictest.multiply(2,5);
Double y=statictest.add(12,5);
Double z=statictest.division(25,5);

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 void meth(int x)


{
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}

static
{
System.out.println("Static block initialized.");
b = a * 4;
}

public static void main(String args[])


{
meth(42);
}
}

Here is the output of the program:


Static block initialized.
x = 42
a=3
b = 12

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
}

// Demonstrate an inner class.


class TestMemberOuter1{
private int data=30;
class Inner{
void msg(){System.out.println("data is "+data);}
}

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.

// Demonstrating the method overloading.


class OverloadDemo
{
void test()
{
System.out.println( "No parameters");
}

// Overload test for one interger parameter.


void test (int a)
{
System.out.println( "a: " +a);
}

// Overload test for two integer parameters.


void test (int a ,int b)
{
System.out.println( "a and b: " + a + " " + b);
}
// overload test for a double parameter
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();
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);
}}

You might also like