This work is licensed under a Creative Commons

  • Slides: 9
Download presentation
This work is licensed under a Creative Commons Attribution-Share. Alike 4. 0 International License.

This work is licensed under a Creative Commons Attribution-Share. Alike 4. 0 International License. This presentation is released under Creative Commons. A 6 ribute, 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

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

Example class Bike{ void run(){System. out. println("running"); } } class Splender extends Bike{ void run(){System. out. println("running safely with 60 km"); } public static void main(String args[]){ Bike b = new Splender(); //upcasting b. run(); } } Output: running safely with 60 km.

Final Keyword • The final keyword in java is used to restrict the user.

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 Bike 9{ final int speedlimit=90; //final variable void run(){ speedlimit=400;

Example: Final Variable class Bike 9{ final int speedlimit=90; //final variable void run(){ speedlimit=400; } public static void main(String args[]){ Bike 9 obj=new Bike 9(); 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

Example: Final Variable class Bike{ final void run(){System. out. println("running"); } } class Honda extends Bike{ void run(){System. out. println("running safely with 100 kmph"); } 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 Honda 1 extends Bike{ void run(){System. out.

Example: Final class final class Bike{} class Honda 1 extends Bike{ void run(){System. out. println("running safely with 100 kmph"); } public static void main(String args[]){ Honda 1 honda= new Honda 1(); 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 • 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 print. Status(); //no body and abstract • (Source: https: //www. javatpoint. com)

Example of abstract class that has abstract method abstract class Bike{ abstract void run();

Example of abstract class that has abstract method abstract class Bike{ abstract void run(); } class Honda 4 extends Bike{ void run(){System. out. println("running safely. . "); } public static void main(String args[]){ Bike obj = new Honda 4(); obj. run(); } } Output: running safely. .