This work is licensed under a Creative Commons Attribution-ShareAlike
4.0 International License. This presentation is released under Creative Commons-
A6ribute,on 4.0 License. You are free to use, distribute and modify it ,
including for commercial purposes, provided you acknowledge the source.
Dynamic method Dispatch
• Runtime polymorphism or Dynamic Method Dispatch is a process in
which a call to an overridden method is resolved at runtime rather than
compile-time.
• In this process, an overridden method is called through the reference
variable of a super class.
• (Source: https://www.javatpoint.com)
Example
class Bike{
void run(){System.out.println("running");}
}
class Splender extends Bike{
void run(){System.out.println("running safely with 60km");}
public static void main(String args[]){
Bike b = new Splender();//upcasting
b.run();
}
}
Output:
running safely with 60km.
Final Keyword
• The final keyword in java is used to restrict the user. The java final
keyword can be used in many context. Final can be:
• variable
• method
• Class
What is blank or uninitialized final variable?
• A final variable that is not initialized at the time of declaration is known as
blank final variable.
• It can be initialized only in constructor.
• (Source: https://www.javatpoint.com)
Example: Final Variable
class Bike9{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
}//end of class
Output:
Compile Time Error
Note: You can’t change the value of final variable
Example: Final Variable
class Bike{
final void run(){System.out.println("running");}
}
class Honda extends Bike{
void run(){System.out.println("running safely with 100kmph");}
public static void main(String args[]){
Honda honda= new Honda();
honda.run();
}
}
Output: Compile Time Error
Note: You can’t override a final method
Example: Final class
final class Bike{}
class Honda1 extends Bike{
void run(){System.out.println("running safely with 100kmph");}
public static void main(String args[]){
Honda1 honda= new Honda1();
honda.run();
}
}
Output: Compile Time Error
Note: You can’t extend a final class.
Abstract Class
• A class that is declared with abstract keyword, is known as abstract class in
java. It can have abstract and non-abstract methods (method with body).
Ways to achieve Abstraction
• There are two ways to achieve abstraction in java
• Abstract class (0 to 100%)
• Interface (100%)
Abstract class in Java
• A class that is declared as abstract is known as abstract class. It needs to be
extended and its method implemented. It cannot be instantiated.
Abstract method
• A method that is declared as abstract and does not have implementation is
known as abstract method.Example abstract method
• abstract void printStatus();//no body and abstract
• (Source: https://www.javatpoint.com)
Example of abstract class that has abstract
method
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){System.out.println("running safely..");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}
Output: running safely..