Java 5 Polymorphism Overloading
Java 5 Polymorphism Overloading
1. class Adder{
2. static int add(int a,int b){return a+b;}
3. static int add(int a,int b,int c){return a+b+c;}
4. }
5. class TestOverloading1{
6. public static void main(String[] args){
7. System.out.println(Adder.add(11,11));
8. System.out.println(Adder.add(11,11,11));
9. }}
Test it Now
Output:
22
33
1. class Adder{
2. static int add(int a, int b){return a+b;}
3. static double add(double a, double b)
4. {
5. return a+b;
6. }
7. }
8. class TestOverloading2{
9. public static void main(String[] args){
10. System.out.println(Adder.add(11,11));
11. System.out.println(Adder.add(12.3,12.6));
12. }}
Output:
22
24.9
Output:
Mobile phone numbers are not stored as integers, as the integer data type
holds values that have the potential to be used in calculations. There is no
context for using a mobile phone number as part of a calculation, so it is
stored as a STRING value.
1. class Animal{
2. String color="white";
3. }
4. class Dog extends Animal{
5. String color="black";
6. void printColor(){
7. System.out.println(color);//prints color of Dog class
8. System.out.println(super.color);//prints color of Animal class
9. }
10. }
11. class TestSuper1{
12. public static void main(String args[]){
13. Dog d=new Dog();
14. d.printColor();
15. }}
Output:
black
white
1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void eat(){System.out.println("eating bread...");}
6. void bark(){System.out.println("barking...");}
7. void work(){
8. super.eat();
9. bark();
10. }
11. }
12. class TestSuper2{
13. public static void main(String args[]){
14. Dog d=new Dog();
15. d.work();
16. }}
Output
eating...
barking...
1. class Animal{
2. Animal(){System.out.println("animal is created");}
3. }
4. class Dog extends Animal{
5. Dog(){
6. super();
7. System.out.println("dog is created");
8. }
9. }
10. class TestSuper3{
11. public static void main(String args[]){
12. Dog d=new Dog();
13. }}
Output:
animal is created
dog is created
1. class Animal{
2. Animal(){System.out.println("animal is created");}
3. }
4. class Dog extends Animal{
5. Dog(){
6. System.out.println("dog is created");
7. }
8. }
9. class TestSuper4{
10. public static void main(String args[]){
11. Dog d=new Dog();
12. }}
Test it Now
Output:
animal is created
dog is created
1. class Person{
2. int id;
3. String name;
4. Person(int id,String name){
5. this.id=id;
6. this.name=name;
7. }
8. }
9. class Emp extends Person{
10. float salary;
11. Emp(int id,String name,float salary){
12. super(id,name);//reusing parent constructor
13. this.salary=salary;
14. }
15. void display(){System.out.println(id+" "+name+"
"+salary);}
16. }
17. class TestSuper5{
18. public static void main(String[] args){
19. Emp e1=new Emp(1,"ankit",45000f);
20. e1.display();
21. }}
Final Keyword In Java
The final keyword in java is used to restrict the user. The java
final keyword can be used in many context. Final can be:
1. variable
2. method
3. class
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.
class Bike9{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
1. }//end of class
Output:Compile Time Error
1. class Bike{
2. final void run(){System.out.println("running...");}
3. }
4. class Honda2 extends Bike{
5. public static void main(String args[]){
6. new Honda2().run();
7. }
8. }
Output:running...
Polymorphism in Java
Polymorphism in Java is a concept by which we can perform
a single action in different ways. Polymorphism is derived from 2
Greek words: poly and morphs. The word "poly" means many and
"morphs" means forms. So polymorphism means many forms.
Upcasting
If the reference variable of Parent class refers to the object of
Child class, it is known as upcasting. For example:
1. class Bike{
2. void run(){System.out.println("running");}
3. }
4. class Splendor extends Bike{
5. void run(){System.out.println("running safely with 60km");}
Output:
1. class Bank{
2. float getRateOfInterest(){return 0;}
3. }
4. class SBI extends Bank{
5. float getRateOfInterest(){return 8.4f;}
6. }
7. class ICICI extends Bank{
8. float getRateOfInterest(){return 7.3f;}
9. }
10. class AXIS extends Bank{
11. float getRateOfInterest(){return 9.7f;}
12. }
13. class TestPolymorphism{
14. public static void main(String args[]){
15. Bank b;
16. b=new SBI();
17. System.out.println("SBI Rate of Interest: "+b.getRateOfInterest());
18. b=new ICICI();
19. System.out.println("ICICI Rate of Interest: "+b.getRateOfInterest());
20. b=new AXIS();
21. System.out.println("AXIS Rate of Interest: "+b.getRateOfInterest());
22. }
23. }
Output:
SBI Rate of Interest: 8.4
ICICI Rate of Interest: 7.3
AXIS Rate of Interest: 9.7
Output:
eating bread...
eating rat...
eating meat...
Output:
90
1. class Animal{
2. void eat(){System.out.println("eating");}
3. }
4. class Dog extends Animal{
5. void eat(){System.out.println("eating fruits");}
6. }
7. class BabyDog extends Dog{
8. void eat(){System.out.println("drinking milk");}
9. public static void main(String args[]){
10. Animal a1,a2,a3;
11. a1=new Animal();
12. a2=new Dog();
13. a3=new BabyDog();
14. a1.eat();
15. a2.eat();
16. a3.eat();
17. }
18. }
1. Output:
eating
eating fruits
drinking Milk
1. int data=30;
1. class Dog{
2. public static void main(String args[]){
3. Dog d1;//Here d1 is a type of Dog
4. }
5. }
Objects have a type
An object is an instance of particular java class,but it is also an instance of its superclass.
1. class Animal{}
2.
3. class Dog extends Animal{
4. public static void main(String args[]){
5. Dog d1=new Dog();
6. }
7. }
Here d1 is an instance of Dog class, but it is also an instance of Animal.
static binding
When type of the object is determined at compiled time(by the
compiler), it is known as static binding. If there is any private,
final or static method in a class, there is static binding.
Dynamic binding
When type of the object is determined at run-time, it is known as
dynamic binding.
Que) What is the use of instance initializer block while we can directly
assign a value in instance data member? For example:
1. class Bike{
2. int speed=100;
3. }
2. class Bike7{
3. int speed;
4. Bike7(){System.out.println("speed is "+speed);}
5. {speed=100;}
3. Bike8(){System.out.println("constructor is invoked");}
Note: The java compiler copies the code of instance initializer block in
every constructor.
Rules for instance initializer block :
There are mainly three rules for the instance initializer block. They are as
follows:
1. The instance initializer block is created when instance of the class is
created.
2. The instance initializer block is invoked after the parent class
constructor is invoked (i.e. after super() constructor call).
3. The instance initializer block comes in the order in which they appear.
6. class B3 extends A{
7. B3(){
8. super();
9. System.out.println("child class constructor invoked");
10. }
1. class Dog2{
2. public static void main(String args[]){
3. Dog2 d=null;
4. System.out.println(d instanceof Dog2);//false
5. }
6. }
Output:false
1. class Animal { }
2.
3. class Dog3 extends Animal {
4. static void method(Animal a) {
5. if(a instanceof Dog3){
6. Dog3 d=(Dog3)a;//downcasting
7. System.out.println("ok downcasting performed");
8. }
9. }
10.
11. public static void main (String [] args) {
12. Animal a=new Dog3();
13. Dog3.method(a);
14. }
15.
16. }
Output:ok downcasting performed
1. class Animal { }
2. class Dog4 extends Animal {
3. static void method(Animal a) {
4. Dog4 d=(Dog4)a;//downcasting
5. System.out.println("ok downcasting performed");
6. }
7. public static void main (String [] args) {
8. Animal a=new Dog4();
9. Dog4.method(a);
10. }
11. }
Output:ok downcasting performed
1. interface Printable{}
2. class A implements Printable{
3. public void a(){System.out.println("a method");}
4. }
5. class B implements Printable{
6. public void b(){System.out.println("b method");}
7. }
8. class Call{
9. void invoke(Printable p){//upcasting
10. if(p instanceof A){
11. A a=(A)p;//Downcasting
12. a.a();
13. }
14. if(p instanceof B){
15. B b=(B)p;//Downcasting
16. b.b();
17. }
18. }
19. }//end of Call class