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

constructorsmethodoverloading

The document provides an overview of constructors, methods, access control, and garbage collection in Java. It explains the purpose of constructors in initializing objects, the structure and types of methods, and the importance of encapsulation and access modifiers. Additionally, it covers method overloading, parameter passing, and recursion with examples to illustrate these concepts.

Uploaded by

saradha.r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

constructorsmethodoverloading

The document provides an overview of constructors, methods, access control, and garbage collection in Java. It explains the purpose of constructors in initializing objects, the structure and types of methods, and the importance of encapsulation and access modifiers. Additionally, it covers method overloading, parameter passing, and recursion with examples to illustrate these concepts.

Uploaded by

saradha.r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Constructor

• A constructor initializes the instance variables of an object.


• It is called immediately after the object is created but before
the new operator completes.
1) it is syntactically similar to a method:
2) it has the same name as the name of its class
3) it is written without return type; the default
return type of a class
• constructor is the same class
• When the class has no constructor, the default constructor
automatically initializes all its instance variables with zero.

L 5.1
Example: Constructor

class Box {
double width;
double height;
double depth;
Box() {
System.out.println("Constructing Box");
width = 10; height = 10; depth = 10;
}
double volume() {
return width * height * depth;
}
}
L 5.2
Parameterized Constructor

class Box {
double width;
double height;
double depth;
Box(double w, double h, double d) {
width = w; height = h; depth = d;
}
double volume()
{ return width * height * depth;
}
}
L 5.3
Methods

• General form of a method definition:


type name(parameter-list) {
… return value;

}
• Components:
1) type - type of values returned by the method. If a method
does not return any value, its return type must be void.
2) name is the name of the method
3) parameter-list is a sequence of type-identifier lists
separated by commas
4) return value indicates what value is returned by the
method.

L 5.4
Example: Method

• Classes declare methods to hide their internal data


structures, as well as for their own internal use: Within a
class, we can refer directly to its member variables:
class Box {
double width, height, depth;
void volume() {
System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}
L 5.5
Parameterized Method

• Parameters increase generality and applicability of a


method:
• 1) method without parameters
int square() { return 10*10; }
• 2) method with parameters
int square(int i) { return i*i; }
• Parameter: a variable receiving value at the time the
method is invoked.
• Argument: a value passed to the method when it is
invoked.

L 5.6
Access Control: Data Hiding and
Encapsulation
• Java provides control over the visibility of variables and
methods.
• Encapsulation, safely sealing data within the capsule of
the class Prevents programmers from relying on details
of class implementation, so you can update without
worry
• Helps in protecting against accidental or wrong usage.
• Keeps code elegant and clean (easier to maintain)

L 6.1
Access Modifiers: Public, Private, Protected

• Public: keyword applied to a class, makes it


available/visible everywhere. Applied to a
method or variable, completely visible.
• Default(No visibility modifier is specified): it
behaves like public in its package and private in
other packages.
• Default Public keyword applied to a class, makes
it available/visible everywhere. Applied to a
method or variable, completely visible.

L 6.2
• Private fields or methods for a class only
visible within that class. Private members are
not visible within subclasses, and are not
inherited.
• Protected members of a class are visible
within the class, subclasses and also within all
classes that are in the same package as that
class.

L 6.3
Visibility
public class Circle {
private double x,y,r;

// Constructor
public Circle (double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
}
//Methods to return circumference and area
public double circumference() { return 2*3.14*r;}
public double area() { return 3.14 * r * r; }
}
L 6.4
Keyword this

• Can be used by any object to refer to itself in


any class method
• Typically used to
– Avoid variable name collisions
– Pass the receiver as an argument
– Chain constructors

L 6.5
Keyword this

• Keyword this allows a method to refer to the object


that invoked it.
• It can be used inside any method to refer to the
current object:
Box(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;
}

L 6.6
Garbage Collection

• Garbage collection is a mechanism to remove objects from


memory when they are no longer needed.
• Garbage collection is carried out by the garbage collector:
• 1) The garbage collector keeps track of how many references
an object has.
• 2) It removes an object from memory when it has no longer
any references.
• 3) Thereafter, the memory occupied by the object can be
allocated again.
• 4) The garbage collector invokes the finalize method.

L 6.7
finalize() Method

• A constructor helps to initialize an object just after it


has been created.
• In contrast, the finalize method is invoked just before
the object is destroyed:
• 1) implemented inside a class as:
protected void finalize() { … }
• 2) implemented when the usual way of removing
objects from memory is insufficient, and some
special actions has to be carried out

L 6.8
Method Overloading

• It is legal for a class to have two or more methods


with the same name.
• However, Java has to be able to uniquely associate
the invocation of a method with its definition
relying on the number and types of arguments.
• Therefore the same-named methods must be
distinguished:
• 1) by the number of arguments, or
• 2) by the types of arguments
• Overloading and inheritance are two ways to
implement polymorphism.

L 7.1
Example: Overloading

class OverloadDemo {
void test() {
System.out.println("No parameters");
}
void test(int a) {
System.out.println("a: " + a);
}
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
double test(double a) {
System.out.println("double a: " + a); return a*a;
}
}
L 7.2
Constructor Overloading
class Box {
double width, height, depth;
Box(double w, double h, double d) {
width = w; height = h; depth = d;
}
Box() {
width = -1; height = -1; depth = -1;
}
Box(double len) {
width = height = depth = len;
}
double volume() { return width * height * depth; }
}
L 7.3
Parameter Passing

• Two types of variables:


1) simple types
2) class types
• Two corresponding ways of how the arguments are
passed to methods:
• 1) by value a method receives a cope of the original
value; parameters of simple types
• 2) by reference a method receives the memory
address of the original value, not the value itself;
parameters of class types

L 7.4
Call by value
class CallByValue {
public static void main(String args[]) {
Test ob = new Test();
int a = 15, b = 20;
System.out.print("a and b before call: “);
System.out.println(a + " " + b);
ob.meth(a, b);
System.out.print("a and b after call: ");
System.out.println(a + " " + b);
}
}
L 7.5
Call by refference
• As the parameter hold the same address as the argument, changes to the
object inside the method do affect the object used by the argument:
class CallByRef {
public static void main(String args[]) {
Test ob = new Test(15, 20);
System.out.print("ob.a and ob.b before call: “);
System.out.println(ob.a + " " + ob.b);
ob.meth(ob);
System.out.print("ob.a and ob.b after call: ");
System.out.println(ob.a + " " + ob.b);
}
}

L 7.6
Recursion

• A recursive method is a method that calls itself:


1) all method parameters and local variables are
allocated on the stack
2) arguments are prepared in the corresponding
parameter positions
3) the method code is executed for the new
arguments
4) upon return, all parameters and variables are
removed from the stack
5) the execution continues immediately after the
invocation point

L 8.1
Example: Recursion

class Factorial {
int fact(int n) {
if (n==1) return 1;
return fact(n-1) * n;
}
}
class Recursion {
public static void main(String args[]) {
Factorial f = new Factorial();
System.out.print("Factorial of 5 is ");
System.out.println(f.fact(5));
} }
L 8.2

You might also like