01 Sep 2022
Objects as parameters
Example:
class Op
{
int a;
int b;
Op(int a,int b)
{
this.a=a;
this.b=b;
}
boolean equalTo(Op o)
{
if(o.a==a &&o.b==b) return true;
else return false;
}
}
class Main
{
public static void main(String args[])
{
Op t1=new Op(10,20);
Op t2=new Op(10,20);
Op t3=new Op(11,21);
System.out.println("t1 == t2: " + t1.equalTo(t2));
System.out.println("t1 == t3: " + t1.equalTo(t3));
}
}
Result:
t1 == t2: true
t1 == t3: false
01 Sep 2022
Overloading methods
- Methods can have the same names within the same class but should have different parameter declaration.
- Method overloading is one of the ways that Java supports polymorphism.
Example
class Op
{
int a;
int b;
int operation(int a)
{
int square=a*a;
return square;
}
int operation(int a,int b)
{
int add=a+b;
return add;
}
}
class Main
{
public static void main(String args[])
{
Op o=new Op();
System.out.println("Sqaure value: "+o.operation(10));
System.out.println("Addition value: "+o.operation(10,20));
}
}
Result:
Sqaure value: 100
Addition value: 30
Overloading Constructors
- If the required variables are not initalized properly then overloading the constructor will handle the improper initialization.
Example:
class Op
{
int a;
int b;
Op()
{
a=-1;
b=-1;
}
Op(int a)
{
this.a=a;
b=a;
}
Op(int a,int b)
{
this.a=a;
this.b=b;
}
int multiply()
{
int mul=a*b;
return mul;
}
}
class Main
{
public static void main(String args[])
{
Op o1=new Op();
Op o2=new Op(10);
Op o3=new Op(10,20);
System.out.println("No values provided: "+o1.multiply());
System.out.println("One values provided: "+o2.multiply());
System.out.println("Both values provided: "+o3.multiply());
}
}
Result:
No values provided: 1
One values provided: 100
Both values provided: 200
01 Sep 2022
this Keyword
- If the local variables in the class and the parameters for the formal parameters for the method are same then local variables hides the instance variables.
- this can be used inside any method to refer to the current object.
Example without this Keyword:
class Rectangle
{
double width;
double height;
Rectangle(double width, double height)
{
width=width;
height=width;
}
double perimeter()
{
double p= 2*(width+height);
return p;
}
}
class Main
{
public static void main(String args[])
{
Rectangle rec1=new Rectangle(10,20);
double vol;
vol=rec1.width * rec1.height;
System.out.println("Rectangle 1 area: "+vol);
System.out.println("Rectangle 1 perimeter method: "+rec1.perimeter());
}
}
Result:
Rectangle 1 area: 0.0
Rectangle 1 perimeter method: 0.0
Example with this keyword
class Rectangle
{
double width;
double height;
Rectangle(double width, double height)
{
this.width=width;
this.height=width;
}
double perimeter()
{
double p= 2*(width+height);
return p;
}
}
class Main
{
public static void main(String args[])
{
Rectangle rec1=new Rectangle(10,20);
double vol;
vol=rec1.width * rec1.height;
System.out.println("Rectangle 1 area: "+vol);
System.out.println("Rectangle 1 perimeter method: "+rec1.perimeter());
}
}
Result:
Rectangle 1 area: 100.0
Rectangle 1 perimeter method: 40.0
Garbage Collection
- When we create an object with new then the dynamic allocation of memory occurs.
- In C++ you have to use keyword delete to manually deallocate the memory.
- But in Java; it deallocated automatically. The technique is called Garbage Collection.
- When no references to an object exist, that object is assumed to be no longer needed, and the memory
occupied by the object can be reclaimed.
Finalize() method
- Sometimes an object will need to perform some action when it is destroyed. For example,
if an object is holding some non-Java resource such as a file handle or character font, then
you might want to make sure these resources are freed before an object is destroyed. To
handle such situations, Java provides a mechanism called finalization.
- finalize( ) is only called just prior to garbage collection.
Syntax:
protected void finalize( )
{
// finalization code here
}
01 Sep 2022
Constructors
Definition:
A constructor initializes an object immediately upon creation.
Important points:
- It is a tedious task to initialize the variables everytime the object is created.
- Java allows objects to be automatically initialized with the help of constructors.
- Constructor has the same name as the class where it resides and syntactically similar to methods.
- Constructor is automatically called when objects are created, before new operator is completed.
- Constructors has no return type not even void.
- Implicit return type of class constructor is class type itself.
Types of constructors: Default(No arguments) and Parameterized.
Default Constructors:
Constructors with no arguments.
Example:
class Rectangle
{
double width;
double height;
Rectangle()
{
width=10;
height=20;
}
double perimeter()
{
double p= 2*(width+height);
return p;
}
}
class Main
{
public static void main(String args[])
{
Rectangle rec1=new Rectangle();
double vol;
vol=rec1.width * rec1.height;
System.out.println("Rectangle 1 area: "+vol);
System.out.println("Rectangle 1 perimeter method: "+rec1.perimeter());
}
}
### Result:
Rectangle 1 area: 200.0
Rectangle 1 perimeter method: 60.0
## Parameterized Constructors
- Default constructors are not very useful as they does not initialze the constructors.
- Solution: use parameterized constructors when we initialize the instance variables while creation of the class.
### Example
class Rectangle
{
double width;
double height;
Rectangle(double w, double h)
{
width=w;
height=h;
}
double perimeter()
{
double p= 2*(width+height);
return p;
}
}
class Main
{
public static void main(String args[])
{
Rectangle rec1=new Rectangle(10,20);
double vol;
vol=rec1.width * rec1.height;
System.out.println("Rectangle 1 area: "+vol);
System.out.println("Rectangle 1 perimeter method: "+rec1.perimeter());
}
}
### Result
Rectangle 1 area: 200.0
Rectangle 1 perimeter method: 60.0
01 Sep 2022
OOPS
Class
Class are the bluprint of the object.
Syntax:
class classname {
type variable1;
type variable2;
type variable3; ...
type function1(parameter-list){
//body of the function1
}
type function2(parameter-list){
//body of the function2
}
Example for class creation
class Rectangle
{
double width;
double height;
}
Object
Object is the instance of the class.
Example for the object creation
Rectangle rec = new Rectangle();
new operator
- Dynamically allocates memory for the object. The memory is allocated at the runtime. Any number of objects can be created. If memory is not sufficient then the new will rise runtime exception.
Important Definitions:
- The data or varialbes declared in the class are called as the instance variables.
- Code is written in function or method.
- Instance variables and methods together are called members of a class.
- Class is a logic construct and the object is physical reality.
Example with 2 objects for a class.
class Rectangle
{
double width;
double height;
}
class Main
{
public static void main(String args[])
{
Rectangle rec1=new Rectangle();
Rectangle rec2=new Rectangle();
double vol;
rec1.width=12;
rec1.height=10;
vol=rec1.width * rec1.height;
System.out.println("Rectangle 1 area: "+vol);
rec2.width=2;
rec2.height=11;
vol=rec2.width * rec2.height;
System.out.println("Rectangle 2 area: "+vol);
}
}
### Result:
Rectangle 1 area: 120.0
Rectangle 2 area: 22.0
Assigning object reference variables
Rectangle rec1=new Rectangle();
rec1=rec2;
- Both rec1 and rec2 will be pointing to the same object. Assignment operator will not allocate new memory.
Methods or functions
Systax:
type name(parameter-list){
//body of method
- type is the type of the data returned by method. If nothing to be returned then the type should be void.
- If the method is returning any value then the function should end with
return value;
Example adding methods to Rectangle
class Rectangle
{
double width;
double height;
double perimeter()
{
double p= 2*(width+height);
return p;
}
}
class Main
{
public static void main(String args[])
{
Rectangle rec1=new Rectangle();
Rectangle rec2=new Rectangle();
double vol;
rec1.width=12;
rec1.height=10;
vol=rec1.width * rec1.height;
System.out.println("Rectangle 1 area: "+vol);
System.out.println("Rectangle 1 perimeter method: "+rec1.perimeter());
rec2.width=2;
rec2.height=11;
vol=rec2.width * rec2.height;
System.out.println("Rectangle 2 area: "+vol);
System.out.println("Rectangle 2 perimeter method: "+rec2.perimeter());
}
}
Result:
Rectangle 1 area: 120.0
Rectangle 1 perimeter method: 44.0
Rectangle 2 area: 22.0
Rectangle 2 perimeter method: 26.0
Method that takes parameters
class Square
{
double width;
double area(double w)
{
width=w;
double p= width*width;
return p;
}
}
class Main
{
public static void main(String args[])
{
Square s1=new Square();
System.out.println("Square are: "+s1.area(10.0));
}
}
### Result
Square are: 100.0