Polymorphism in Java
Meaning of Polymorphism :
The word polymorphism means many-form.
In OOPs, polymorphism deals with behavior(method or function) part of the object.
If an object is able to perform single functionality in multiple ways, this is called polymorphic behavior of that object.
Java polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.
On the basis of concept of BINDING, the polymorphism is categorized into two category:
Compile Time Polymorphism
Runtime Polymorphism
1. Compile Time Polymorphism :
If BINDING association is known at compile time then this is known as compile time polymorphism.
This is also known as static binding or early binding.
Compile time polymorphism can be achieved in Java programming with the implementation of the following programming concept
Method Overloading
Constructor Overloading
2. Runtime Polymorphism :
If BINDING association is known at compile time that decides the association at runtime then this is known as runtime polymorphism.
This is also known as dynamic binding or late binding.
Compile time polymorphism can be achieved in Java programming with the implementation of the programming concept
Method Overriding
1. Compile Time Polymorphism :
Method Overloading
Java allows to create more than one method with the same name in the same class. This mechanism is called method overloading.
In method overloading the methods name are same but the argument list of each method must be different.
To overload method there are three ways:
- Define a new method with the same name with different types of argument.
- Define a new method with the same name with different number of argument.
- Define a new method with the same name with different types and different number of argument.
NOTE: Return type of method, doesn't play any role in method overloading.
Example: Method overloading :
class Demo{ public void show( int a ) { //definition } //Number of arguments are different public void show( int a, int b ) { //definition } //Types of arguments are different public void show( String a, String b ) { //definition } public void show( int a, String b ) { //definition } //Order of arguments are different public void show( String a, int b ) { //definition } public static void main(String []arg) { Demo d = new Demo (); d.show(10); d.show(10, 20); d.show("Java", "Prowess"); d.show(10, "Java Prowess"); d.show("Java Prowess", 10); } }
Example:
Constructor overloading :class User{ private String name, role; User(String name) { this.name = name; } User( String name, String role) { this.name = name; this.role = role; } public static void main(String []arg) { User user1 = new User("Ayan"); User user2 = new User("Atif","DBA"); } }
1. RunTime Polymorphism :
Method Overriding
-
When a method in a sub-class has the same name and type signature as a method in its super-class, then the method in the sub-class is said to override the method of the super class.
-
Both signature and return type must be the same as super class.
-
The throws clause of an overriding method can have fewer types listed than the method in the super class, or more specific types or both.
Example :
//Parent.java public class Parent { public void show() { System.out.println("Hello Parent"); } } ------------------------------- //Child.java public class Child extends Parent { //overriding public void show() { System.out.println("Hello Child"); } }
-
A sub-class can change the access specifier of the method of the super-class , but only if it provides more access.
-
A method declared public in super class, will be public in sub class.
-
A method declared protected in super class can be re-declared protected or public but not private.
-
A method declared default in super class can be re-declared default, protected or public but not private.
-
Fields cannot be overridden, they can only be hidden.
-
To access the hidden fields use the super keyword.
- Note :
-
When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass.
-
Only non-static method can be override.
-
Final method can't be override.
Example :
//Parent.java public class Parent { public void show() { System.out.println("show from Parent"); } void display() { System.out.println("display from Parent"); } } ------------------- //Child.java public class Child extends Parent { public void show() { System.out.println("show from Child"); } //more acces public void display() { System.out.println("display from Child"); } } ----------------------- //MainClass.java public class MainClass { public static void main(String []arg) { Parent p = new parent(); p.show(); p.display(); Child ch = new Child(); ch.show(); ch.diaplay(); } }OUTPUT :
show from Parent display from Parent show from Child display from Child
Upcasting :
An upcast is a cast from a derived type to one of its base classes. This cast is safe and does not require an explicit cast notation.
Upcasting is using the Super class's reference to refer to a Sub class's object. Or we can say that, the act of converting a Sub class's reference into its Super class's reference is called Upcasting.
-
Syntax :
Super-class ref = Child-class Object; e.g. Parent p = new Child();
- Note :
- Through this reference you can access only those methods, which are inherited or override by subclass, child's method can't be access.
Downcasting :
-
The process of converting super class's refernce that pointing to sub-class Object, to sub-class reference is called downcasting.
downcasting is to be done explicit.
-
Syntax ;
Chil-class ref = (Child-class) Parent-ref. e.g. Child ch = (Child) p;
Example :
Program to understand the concept
//Parent.java public class Parent { public void show() { System.out.println("show from Parent"); } void display() { System.out.println("display from Parent"); } } ------------------- //Child.java public class Child extends Parent { //more override public void display() { System.out.println("display from Child"); } public void xyz() { System.out.println("Child's Method"); } } ----------------------- //MainClass.java public class MainClass { public static void main(String []arg) { //Upcasting Parent p = new Child(); p.show(); //call inherited method p.diaplay();//call override method p.xyz(); //ERROR //Downcasting Child ch = (Child) P; ch.show(); //call inherited method ch.diaplay();//call override method ch.xyz(); //valid } }
Next topic is this-and-super
Training For College Campus
We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies
like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.
Please mail your requirement at info@prowessapps.in
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at info@prowessapps.in