4.java Polymorphism
4.java Polymorphism
Method Overloading:
Method Overloading means different methods to have same name, but different
signatures. Overloading is related to compile time (or static) polymorphism.
class Display
{
public void display()
{
System.out.println ("Inside First display method");
}
Output:
Example:
class A{
A(){
System.out.println("No argument constructor");
}
A(int x){
System.out.println("One argument constructor");
}
A(int x, int y){
System.out.println("Two argument constructor");
}
}
public class Test{
public static void main(String[] as){
A a1= new A();
A a2= new A(1);
A a3= new A(2,4);
}
}
Output:
No argument constructor
One argument constructor
Two argument constructor
If a method in parent class is overridden in child class, and then if we call that
method then JVM will firstly checks that method is existed in reference variable type
and It doesn’t check about the right version which is currently pointing to. This task
will be postponed to the programs runtime. At runtime, JVM checks which object is
currently referring the object and calls right version of that object. This is called
Runtime Polymorphism.
Method Overriding:
If subclass (child class) has the same method as declared in the parent class, it
is known as method overriding.
Or
If subclass provides the specific implementation of the method that has been
provided by one of its parent class is called method overriding.
Usage of Method Overriding:
Method overriding is used to provide specific implementation of a
method that is already provided by its super class.
Example:
class Parent {
public void display()
{
System.out.println ("display() method of Parent class");
}
}
class Child extends Parent
{
public void display()
{
System.out.println ("display() method of child class");
}
}
public class Test{
public static void main(String args[])
{
//Assign Parent reference to Parent class
Parent p = new Parent();
p.display();
//Assign Child reference to Child class
Child c=new Child();
c.display();
}
}
Output:
display() method of Parent class
display() method of child class
}
}
Output:
display() method of Parent class
display() method of Child class
In the above example display() 2 times called in main method and one time it
called Parent class version and next time it called Child version.
The compiler checks, the reference variable is of type Parent class and checks
display() is exist in Parent class or not. It doesn’t check about the right version which
is currently pointing to. This task will be postponed to the programs runtime. At
runtime, JVM checks which object is currently referring the object and calls right
version of that object. This is called Runtime Polymorphism. The choosing of current
version of method is happened at run time rather than at compile time. So, it is called
“Dynamic Method Dispatch” Or “Dynamic Binding” Or “Late Binding”.
Example2:
class Parent
{
public void display()
{
System.out.println ("display() method of Parent class");
}
}
class Child extends Parent
{
public void display()
{
System.out.println ("display() method of Child class");
}
public void show(){
System.out.println ("show() method is called");
}
}
public class Test{
public static void main(String args[])
{
//Declaring Reference variable
Parent p;
//Assign Parent reference to Parent class Object
p=new Parent();
p.display();
//Assign Parent reference to Child class Object
p= new Child();
p.display();
//The method show() is undefined for the type Parent.
//If called using Dynamic binding then it show error
//p.show(); //Invalid
Child c=new Child();
c.display();
c.show();
}
}
Output:
display() method of Parent class
display() method of Child class
display() method of child class
show() method is called
We cannot call the Child class methods (which are not defined in Parent class)
with Parent class reference because at compile time JVM checks whether the called
method is present in reference (on which it is called) type class or not.
final Keyword:
The final keyword in java is used to restrict the user. The java final keyword
can be used in many contexts. Final can be:
variable
method
class
A) final Variable:
If you make any variable as final, you cannot change the value of final variable
(It will be constant). It is good practice to represent final variables in all uppercase,
using underscore to separate words.
The final keyword can be applied with the variables, a final variable that have
no value it is called blank final variable or uninitialized final variable. It can be
initialized in the constructor only. The blank final variable can be static also which
will be initialized in the static block only.
Example:
final int MAX=23;//final variable
B) final Method:
When a method is declared with final keyword, it is called a final method. A
final method cannot be overridden. The Object class does this—a number of its
methods are final. We must declare methods with final keyword for which we
required to follow the same implementation throughout all the derived classes.
Example:
final void method(){
//method definition
}
C) final Class:
When a class is declared with final keyword, it is called a final class. A final
class cannot be extended (inherited).
Example:
final class A{
//methods and fields
}
this Keyword:
Keyword 'this' is a reference variable that refers to the current object.
Example:
class A{
A(){System.out.println("hello a");}
A(int x){
this();
System.out.println(x); }
A(int x, int y){
this(x);
System.out.println(y);}}
4) this can be passed as an argument in the method call:
The this keyword can also be passed as an argument in the method. It is mainly
used in the event handling.
5) this can be passed as argument in the constructor call:
We can pass this keyword in the constructor also. It is useful if we have to use
one object in multiple classes.
6) this can be used to return the current class instance from the method:
We can return this keyword as an statement from the method. In such case,
return type of the method must be the class type (non-primitive).
Syntax:
return_type method_name(){
return this;
}