03 Java Polymorphism
03 Java Polymorphism
PROGRAMMING
SUBJECT DESCRIPTION: JAVA ENTERPRISE EDITION PROGRAMMING
SUBJECT CODE: CS220
In this example, we are creating static methods so that we don't need to create instance for
calling methods.
class Adder{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class TestOverloading1{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
2) METHOD OVERLOADING: CHANGING
DATA TYPE OF ARGUMENTS
In this example, we have created two methods that differs in data type.
class Adder{
static int add(int a, int b){return a+b;}
static double add(double a, double b)
{return a+b;}
}
class TestOverloading2{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
}}
METHOD OVERLOADING AND TYPE
PROMOTION
One type is promoted to another
implicitly if no matching datatype is
found.
The covariant return type specifies that the return type may
vary in the same direction as the subclass.
1) Covariant return type assists to stay away from the confusing type casts in the class
hierarchy and makes the code more usable, readable, and maintainable.
2) In the method overriding, the covariant return type provides the liberty to have more to
the point return types.
Bike7()
{System.out.println("speed is "+speed);}
{speed=100;}
1. method
2. constructor
3. block
WHAT IS INVOKED FIRST, INSTANCE
INITIALIZER BLOCK OR CONSTRUCTOR?
class Bike8{
int speed; In the example, it
seems that instance
Bike8(){System.out.println("constructor is invoked");} initializer block is
firstly invoked but NO.
{System.out.println("instance initializer block invoked"); Instance initializer
} block is invoked at
the time of object
public static void main(String args[]){ creation. The java
Bike8 b1=new Bike8(); compiler copies the
Bike8 b2=new Bike8(); instance initializer
} block in the
} constructor after the
first statement
super(). So firstly,
constructor is
Note: The java compiler copies the code of instance
invoked. initializer block in
RULES FOR INSTANCE INITIALIZER
BLOCK :
There are mainly three rules for the instance
initializer block. They are as follows:
The final keyword in java is used to restrict the user. The java
final keyword can be used in many context.
Final can be:
• variable
• method
• class
JAVA FINAL VARIABLE
• 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.
• The blank final variable can be static also which will be initialized in
the static block only.
Example of final variable
There is a final
class Bike9{ variable
final int speedlimit=90;//final variable speedlimit, we are
void run(){ going to change
speedlimit=400; the value of this
} variable, but It
public static void main(String args[]){ can't be changed
Bike9 obj=new Bike9(); because final
obj.run(); variable once
} assigned a value
}//end of class can never be
changed.
If you make any variable as final, you cannot
change the value of final variable(It will be
constant).
STATIC BLANK FINAL VARIABLE
class A{
static final int data;//static blank final variable
static{ data=50;}
public static void main(String args[]){
System.out.println(A.data);
}
}
JAVA FINAL METHOD
class Student{
int id;
String name;
final String PAN_CARD_NUMBER;
...
}
FAQ
Bike10(){
speedlimit=70;
System.out.println(speedlimit);
}
Let's see the real use of instanceof keyword by the example given below.