- Defining multiple methods with same name
- Static polymorphism and dynamic polymorphism
Static polymorphism:
- Its a process of defining multiple methods with the same name and with different parameter types or list or order is called static polymorphism
- Compile time itself we can say which method is going to get executed based on parameters types.
- So its also called compile time polymorphism.
- This will be achieved by method overloading.
- Method overloading means defining multiple methods with same name and different parameters.
- If two methods have same method name , But different parameter types those methods are considered as overloaded methods.
- Will see example program on method overloading.
Example program on method overloading:
- package instanceofjava;
- public class OverloadingDemo {
- void add(){
- System.out.println("No-arg method");
- }
- void add(int i){
- System.out.println("int-arg method");
- }
- void add(String str){
- System.out.println("String-arg method");
- }
- void add(float d){
- System.out.println("float-arg method");
- }
- void add(int a, int b){
- System.out.println("2 int-arg method");
- }
- public static void main(String[] args){
- OverloadingDemo obj= new OverloadingDemo();
- obj.add();
- obj.add(10);
- obj.add(1.2f);
- obj.add("java");
- obj.add(1, 2);
- }
- }
Output
- No-arg method
- int-arg method
- float-arg method
- String-arg method
- 2 int-arg method
Method overloading with reference types:
- We can also overload method with reference types, check below program.
- package instanceofjava;
- public class Test {
- void show(){
- System.out.println("test class show() method");
- }
- }
- package instanceofjava;
- public class Example {
- void method(String s){
- System.out.println("String-arg");
- }
- void method(Test obj){
- System.out.println("method(Test obj)-arg");
- obj.show();
- }
- public static void main(String[] args){
- Example obj= new Example();
- obj.method("polymorphism definition in oops");
- obj.method(new Test());
- }
- }
Output:
- String-arg
- method(Test obj)-arg
- test class show() method
Dynamic polymorphism:
- Redefining the super class method in sub class is called "method overriding" .
- Method overriding is a language feature that allows a sub class to provide a specific implementation of a method that is already provided by one of its super class.
- The implementation in the sub class overrides the implementation in the super class.
- This is also called dynamic polymorphism / runtime polymorphism.
- package instanceofjava;
- public class A{
- void show(String s){
- System.out.println("super class method");
- }
- }
- package instanceofjava;
- public class B extends A{
- void show(String s){
- System.out.println("sub class method");
- }
- public static void main(String[] args){
- B obj =new B();
- obj.show();
- }
- }
Output:
- sub class method
We can not override static methods:
- Actually static variables and static methods are class level. they are not instance level.
- So if you try to override a static method will throw compile error
- package instanceofjava;
- public class Super {
- int a,b;
- static void add(){
- System.out.println("super class add() method");
- }
- }
- package instanceofjava;
- public class Sub extends Super {
- void add(){ // ERROR:This instance method cannot override the static method
- System.out.println("this is sub class add() method");
- }
- public static void main(String [] args){
- Sub obj= new Sub();
- }
- }
- Static methods are class level so we need to access by using class name.
- package instanceofjava;
- public class Sub extends Super {
- static void add(){// works fine
- System.out.println("this is sub class add() method");
- }
- public static void main(String [] args){
- Super.add();
- Sub.add();
- }
- }
Output:
- super class add() method
- sub class add() method
Method Overriding - Exceptions:
- When we overriding a method from super class to sub class. we need to follow some rules
- Whenever sub class method throws checked exception then super class method must be declared throws with same exception or super class of that checked exception.
- package instanceofjava;
- public class Super {
- int a,b;
- void add(){
- System.out.println("super class add() method");
- }
- }
- package instanceofjava;
- public class Sub extends Super {
- void add() throws IOException{ // ERROR: - Exception IOException is not compatible with
- //throws clause in Super.add()
- System.out.println("this is sub class add() method");
- }
- public static void main(String [] args){
- Sub obj= new Sub();
- }
- }
- package instanceofjava;
- public class Super {
- int a,b;
- void add()throws IOException{
- System.out.println("super class add() method");
- }
- }
- package instanceofjava;
- public class Sub extends Super {
- void add() throws IOException{// works fine
- System.out.println("this is sub class add() method");
- }
- public static void main(String [] args){
- Sub obj= new Sub();
- }
- }
No comments