class bank { void m1(int id,int ac)///METHOD OF PARENT CLASS { System.out.println("Employee ID= "+id); System.out.println("Account Number= "+ac); } } class det extends bank { void m1(int id,int sal)///METHOD OF CHILD CLASS { System.out.println("Balance= "+id); System.out.println("salary= "+sal); } } class main { public static void main(String arg[]) { //bank b1=new bank(); ///polymorphism //b1.m1(14548,154558); //det d1=new det(); //d1.m1(1244545,78455); det d1=new det(7894,48458); } } ///try this | 0 |
By: [email protected] On: Wed Apr 05 15:14:47 IST 2017 0 0 0 | 0 |
Are You Satisfied :0Yes0No |
vehicle v1=new Bike2();//run time polymorphism | 0 |
By: [email protected] On: Wed Apr 05 15:16:26 IST 2017 0 0 0 | 0 |
Are You Satisfied :0Yes0No |
Fine, but my question is why can't we override the method with different primitive return type? Like in the Vehicle class return type is void, but the subclass Bike2 return type of method is int. Try this: class Vehicle{ void run(){System.out.println("Vehicle is running");} } class Bike2 extends Vehicle { int run(){System.out.println("Bike is running safely"); return 0; } public static void main(String args[]){ Vehicle obj = new Bike2(); obj.run(); } } --------------------------------------------------------------------------- output: >javac Bike2.java Bike2.java:7: error: run() in Bike2 cannot override run() in Vehicle int run(){System.out.println("Bike is running safely"); ^ return type int is not compatible with String --------------------------------------------------------------------------- I know that method overriding with different primitive return type is not possible. I am looking for reason why not possible.... 1 error | 0 |
By: [email protected] On: Wed Apr 05 16:12:03 IST 2017 0 0 0 | 0 |
Are You Satisfied :0Yes0No |
Before Java 5.0, when you override a method, both parameters and return type must match exactly. In Java 5.0, it introduces a new facility called covariant return type. You can override a method with the same signature but returns a subclass of the object returned. In another words, a method in a subclass can return an object whose type is a subclass of the type returned by the method with the same signature in the superclass. | 0 |
By: [email protected] On: Sun Apr 09 09:56:05 IST 2017 0 0 0 | 0 |
Are You Satisfied :0Yes0No |
we cant change the return type of primitive type while overriding... covariant return type is used for non primitive types | 0 |
By: [email protected] On: Thu May 25 21:22:34 IST 2017 0 0 0 | 0 |
Are You Satisfied :0Yes0No |