Method Overloading:
When a class has two or more methods by same name but different parameters, it is known as method overloading.
In overriding a method has same method name, type, number of parameters etc.
Lets consider the example shown before for finding minimum numbers of integer type. If, lets say we want to find minimum number of double type. Then the concept of Overloading will be introduced to create two or more methods with the same name but different parameters. (taken from link)
The Constructors:
A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type.
Typically, you will use a constructor to give initial values to the instance variables defined by the class, or to perform any other startup procedures required to create a fully formed object.
All classes have constructors, whether you define one or not, because Java automatically provides a default constructor that initializes all member variables to zero. However, once you define your own constructor, the default constructor is no longer used. (taken from link)
The this keyword
this is a keyword in Java which is used as a reference to the object of the current class, with in an instance method or a constructor. Using this you can refer the members of a class such as constructors, variables and methods.
In general the keyword this is used to :
- Differentiate the instance variables from local variables if they have same names, within a constructor or a method. (taken from link)
Call one type of constructor( parametrized constructor or default ) from other in a class. It is known as explicit constructor invocation.
class Student{ int age Student(){ this(20); // invoking the default constructor } Student(int age){ this.age=age; } }
public class This_Example { //Instance variable num int num=10; This_Example(){ System.out.println("This is an example program on keyword this "); } This_Example(int num){ //Invoking the default constructor this(); //Assigning the local variable num to the instance variable num this.num=num; }
Variable Arguments(var-args):
JDK 1.5 enables you to pass a variable number of arguments of the same type to a method. The parameter in the method is declared as follows:
typeName... parameterName
In the method declaration, you specify the type followed by an ellipsis (...) Only one variable-length parameter may be specified in a method, and this parameter must be the last parameter. Any regular parameters must precede it. (taken from link)
The finalize( ) Method:
It is possible to define a method that will be called just before an object's final destruction by the garbage collector. This method is called finalize( ), and it can be used to ensure that an object terminates cleanly.
protected void finalize( ) { // finalization code here }
(taken from link)
No comments:
Post a Comment