Inheritance, Overloading and Overriding are used to achieve Polymorphism in java
The abiltiy to define more than one function with the same name is called Polymorphism.
In java,c++ there are two type of polymorphism: compile time polymorphism (overloading) and runtime polymorphism (overriding)
Polymorphism is briefly described as "one interface, many implementations."
When you override methods, JVM determines the proper methods to call at the program’s run time, not at the compile time.
Overriding occurs when a class method has the same name and signature as a method in parent class.
Overloading occurs when several methods have same names with
Overloading is determined at the compile time.
Different method signature and different number or type of parameters.
Same method signature but different number of parameters.
Same method signature and same number of parameters but of different type
class BookDetails{
String title;
setBook(String title){ }
}
class ScienceBook extends BookDetails{
setBook(String title){} //overriding
setBook(String title, String publisher,float price){ } //overloading
}
(Inheritance, Overloading and Overriding are used to achieve Polymorphism in java).
Polymorphism manifests itself in Java in the form of multiple methods having the same name.
In some cases, multiple methods have the same name, but different formal argument lists (overloaded methods).
In other cases, multiple methods have the same name, same return type, and same formal argument list (overridden methods).
Method overloading
Method overriding through inheritance
Method overriding through the Java interface
The overriding method cannot have a more restrictive access modifier than the method being overridden
(Ex: You can’t override a method marked public and make it protected).
You cannot override a method marked final
You cannot override a method marked static
method overloading would be an example of static polymorphism
whereas overriding would be an example of dynamic polymorphism.
Dynamic, or late, binding occurs when a method is defined for several classes in a family, but the actual code for the method is not attached, or bound, until execution time.This gives support for overriding...
Static binding occurs when a method is defined with the same name but with different headers and implementations. The actual code for the method is attached, or bound, at compile time. Static binding is used to support overloaded methods in Java.
http://www.coderanch.com/t/379004/java/java/static-polymorphism-dynamic-polymorphism
Compile time polymorphism and the other is run time polymorphism.
Compile time polymorphism is method overloading.
Runtime time polymorphism is done using inheritance and interface.
- static polymorphism vs dynamic polymorphism
method overloading would be an example of static polymorphism
whereas overriding would be an example of dynamic polymorphism.
Dynamic, or late, binding occurs when a method is defined for several classes in a family, but the actual code for the method is not attached, or bound, until execution time.This gives support for overriding...
Static binding occurs when a method is defined with the same name but with different headers and implementations. The actual code for the method is attached, or bound, at compile time. Static binding is used to support overloaded methods in Java.
http://www.coderanch.com/t/379004/java/java/static-polymorphism-dynamic-polymorphism
No comments:
Post a Comment