SlideShare a Scribd company logo
2
Most read
3
Most read
6
Most read
Dynamic Method Dispatch
Abstract Class
Using Final with inheritance
Dynamic method dispatch
• Dynamic method dispatch is one type of mechanism by which a call to an
overridden method is resolved at run time
• When an overridden method is called through the object of superclass
then Java determines which version of that method to execute, based
upon the type of the object being referred to at the time the call occurs,
hence determination is made at run time.
• Go through the example below
• /* * Dynamic Method Dispach */
class Class_A
{ void demo()
{ System.out.println("the class A method called"); }
}
class Class_B extends Class_A
{ void demo()
{ System.out.println("the class B method called"); }
}
public class inheritance
{ public static void main(String[] y)
{ Class_A a=new Class_A();
a.demo();
/*
* The below one is called upcasting
*/
Class_A b=new Class_B();
b.demo();
a=b;
a.demo();
}
}
Difference between Static binding and Dynamic
binding in java ?
• Static binding in Java occurs during compile time
• dynamic binding occurs during runtime.
• Overloaded methods are bonded using static binding
• overridden methods are bonded using dynamic binding at runtime.
Abstract classes
• If a class contain any abstract method then the class is declared as
abstract class.
• An abstract class is never instantiated.
• Although it does not provide 100% abstraction because it can also have
concrete method.
Syntax
• Abstract class <class_name>{}
Abstract method
• Method that are declared without any body within an abstract class is
known as abstract method.
• The method body will be defined by its subclass
Syntax
Abstract return_type method_name(arguments list);
/* * abstract class and method */
abstract class Demo_A
{ abstract void Demo();//abstract method }
class Demo_B extends Demo_A
{ void Demo()
{ System.out.println("hello"); }
}
public class Abstract_Demo
{ public static void main(String[] y)
{ Demo_B b=new Demo_B();
b.Demo();
}
}
Abstract class with Concrete methods
/* * abstract class and Concrete method */
abstract class Demo_A
{ abstract void Demo(); //abstract method
void demo_a() //concrete method
{ System.out.println("the method of abstract class concrete method"); }
}
class Demo_B extends Demo_A
{ void Demo()
{ System.out.println("hello"); }
}
public class Abstract_Demo
{ public static void main(String[] y)
{ Demo_B b=new Demo_B();
b.Demo();
b.demo_a();
//Demo_A a=new Demo_A();
//a.demo_a();
/* * try to remove the above comments to under the concept */
/* * we can create refference variable for abstract classes */
Demo_A a=new Demo_B();
a.Demo();
}
}
Final method
• A final method cannot be overridden
• Which means even though a sub class can call the final method of parent class without
any issues but it cannot override it
class Super
{ final void demo()
{ System.out.println("hello"); }
}
class Sub_ extends Super
{ /*void demo()
{
System.out.println("hello world");
}*/
void demo_1()
{ System.out.println("hello world"); }
}
class Demo
{ public static void main(String[] y)
{ Sub_ s=new Sub_();
s.demo();
}
}
Final class
• We cannot extend a final class prevent inheritance
final class Super
{ void demo()
{ System.out.println("hello"); }
}
class Sub_ extends Super
{ void demo_1()
{ System.out.println("hello world"); }
}
class Demo
{ public static void main(String[] y)
{ Sub_ s=new Sub_(); }
}

More Related Content

PPTX
Computer system bus
PPTX
Inheritance in java
PPTX
java interface and packages
PPTX
Asymptotic notations
PPTX
Need for Software Engineering
PPT
Css Ppt
Computer system bus
Inheritance in java
java interface and packages
Asymptotic notations
Need for Software Engineering
Css Ppt

What's hot (20)

PPTX
Methods in java
PPT
PPTX
Inheritance in java
PDF
Class and Objects in Java
PPTX
Inner classes in java
ODP
OOP java
PPTX
Polymorphism presentation in java
PPTX
Java exception handling
PPTX
INHERITANCE IN JAVA.pptx
PPTX
Applets in java
PPTX
Interface in java
PPT
9. Input Output in java
PPTX
I/O Streams
PPTX
Inheritance in c++
PPTX
6. static keyword
PPTX
Packages in java
PPTX
This keyword in java
PPTX
Type casting in java
PDF
Methods in Java
Methods in java
Inheritance in java
Class and Objects in Java
Inner classes in java
OOP java
Polymorphism presentation in java
Java exception handling
INHERITANCE IN JAVA.pptx
Applets in java
Interface in java
9. Input Output in java
I/O Streams
Inheritance in c++
6. static keyword
Packages in java
This keyword in java
Type casting in java
Methods in Java
Ad

Viewers also liked (20)

PPTX
Ppt on this and super keyword
PPT
Runnable interface.34
PPT
Exception handling
PPT
Java exception
PPT
Access Protection
PPTX
Effective Java - Chapter 4: Classes and Interfaces
PPTX
Exceptions in Java
PPTX
Exception handling in asp.net
PPT
Exception Handling Java
ODP
Toolbarexample
PPT
02basics
DOC
Non ieee dot net projects list
PDF
Yaazli International Spring Training
PDF
Java quick reference v2
DOCX
Java Exception handling
PDF
Yaazli International Web Project Workshop
PPTX
For Loops and Variables in Java
PPTX
Exception handling in java
PDF
Yaazli International AngularJS 5 Training
PDF
Yaazli International Hibernate Training
Ppt on this and super keyword
Runnable interface.34
Exception handling
Java exception
Access Protection
Effective Java - Chapter 4: Classes and Interfaces
Exceptions in Java
Exception handling in asp.net
Exception Handling Java
Toolbarexample
02basics
Non ieee dot net projects list
Yaazli International Spring Training
Java quick reference v2
Java Exception handling
Yaazli International Web Project Workshop
For Loops and Variables in Java
Exception handling in java
Yaazli International AngularJS 5 Training
Yaazli International Hibernate Training
Ad

Similar to Dynamic method dispatch (20)

PPT
Java inheritance
PPTX
Inheritance
PPTX
FINAL_DAY9_METHOD_OVERRIDING_Role and benefits .pptx
PPT
Best Core Java Training In Bangalore
PPT
Inheritance
PPTX
Abstract Class & Abstract Method in Core Java
PPT
Object Oriented Programming with Java
PPTX
Object Oriented Programming Inheritance with case study
PPTX
Inheritance.pptx
PPTX
Class introduction in java
DOCX
JAVA Notes - All major concepts covered with examples
PPTX
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
DOCX
Unit-3 Practice Programs-5.docx
PPTX
Inheritance
DOCX
Keyword of java
PPS
Inheritance chepter 7
DOCX
Binding,interface,abstarct class
PPTX
Unit3 inheritance
PDF
polymorphismpresentation-160825122725.pdf
PPTX
Inheritance & interface ppt Inheritance
Java inheritance
Inheritance
FINAL_DAY9_METHOD_OVERRIDING_Role and benefits .pptx
Best Core Java Training In Bangalore
Inheritance
Abstract Class & Abstract Method in Core Java
Object Oriented Programming with Java
Object Oriented Programming Inheritance with case study
Inheritance.pptx
Class introduction in java
JAVA Notes - All major concepts covered with examples
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
Unit-3 Practice Programs-5.docx
Inheritance
Keyword of java
Inheritance chepter 7
Binding,interface,abstarct class
Unit3 inheritance
polymorphismpresentation-160825122725.pdf
Inheritance & interface ppt Inheritance

More from yugandhar vadlamudi (12)

ODP
Singleton pattern
PPT
Object Relational model for SQLIite in android
DOCX
JButton in Java Swing example
PPTX
Collections framework in java
PPTX
Packaes & interfaces
DOCX
JMenu Creation in Java Swing
DOCX
Adding a action listener to button
PPTX
Operators in java
PPTX
Inheritance
PPTX
Control flow statements in java
PPTX
Closer look at classes
PPTX
java Applet Introduction
Singleton pattern
Object Relational model for SQLIite in android
JButton in Java Swing example
Collections framework in java
Packaes & interfaces
JMenu Creation in Java Swing
Adding a action listener to button
Operators in java
Inheritance
Control flow statements in java
Closer look at classes
java Applet Introduction

Recently uploaded (20)

PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PDF
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
PDF
Module 3: Health Systems Tutorial Slides S2 2025
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
English Language Teaching from Post-.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Introduction and Scope of Bichemistry.pptx
PPTX
Onica Farming 24rsclub profitable farm business
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
How to Manage Starshipit in Odoo 18 - Odoo Slides
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
Module 3: Health Systems Tutorial Slides S2 2025
O5-L3 Freight Transport Ops (International) V1.pdf
Open Quiz Monsoon Mind Game Final Set.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
English Language Teaching from Post-.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Introduction and Scope of Bichemistry.pptx
Onica Farming 24rsclub profitable farm business
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
human mycosis Human fungal infections are called human mycosis..pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Anesthesia in Laparoscopic Surgery in India
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
The Final Stretch: How to Release a Game and Not Die in the Process.
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx

Dynamic method dispatch

  • 1. Dynamic Method Dispatch Abstract Class Using Final with inheritance
  • 2. Dynamic method dispatch • Dynamic method dispatch is one type of mechanism by which a call to an overridden method is resolved at run time • When an overridden method is called through the object of superclass then Java determines which version of that method to execute, based upon the type of the object being referred to at the time the call occurs, hence determination is made at run time. • Go through the example below
  • 3. • /* * Dynamic Method Dispach */ class Class_A { void demo() { System.out.println("the class A method called"); } } class Class_B extends Class_A { void demo() { System.out.println("the class B method called"); } } public class inheritance { public static void main(String[] y) { Class_A a=new Class_A(); a.demo(); /* * The below one is called upcasting */ Class_A b=new Class_B(); b.demo(); a=b; a.demo(); } }
  • 4. Difference between Static binding and Dynamic binding in java ? • Static binding in Java occurs during compile time • dynamic binding occurs during runtime. • Overloaded methods are bonded using static binding • overridden methods are bonded using dynamic binding at runtime.
  • 5. Abstract classes • If a class contain any abstract method then the class is declared as abstract class. • An abstract class is never instantiated. • Although it does not provide 100% abstraction because it can also have concrete method. Syntax • Abstract class <class_name>{}
  • 6. Abstract method • Method that are declared without any body within an abstract class is known as abstract method. • The method body will be defined by its subclass Syntax Abstract return_type method_name(arguments list); /* * abstract class and method */ abstract class Demo_A { abstract void Demo();//abstract method } class Demo_B extends Demo_A { void Demo() { System.out.println("hello"); } } public class Abstract_Demo { public static void main(String[] y) { Demo_B b=new Demo_B(); b.Demo(); } }
  • 7. Abstract class with Concrete methods /* * abstract class and Concrete method */ abstract class Demo_A { abstract void Demo(); //abstract method void demo_a() //concrete method { System.out.println("the method of abstract class concrete method"); } } class Demo_B extends Demo_A { void Demo() { System.out.println("hello"); } } public class Abstract_Demo { public static void main(String[] y) { Demo_B b=new Demo_B(); b.Demo(); b.demo_a(); //Demo_A a=new Demo_A(); //a.demo_a(); /* * try to remove the above comments to under the concept */ /* * we can create refference variable for abstract classes */ Demo_A a=new Demo_B(); a.Demo(); } }
  • 8. Final method • A final method cannot be overridden • Which means even though a sub class can call the final method of parent class without any issues but it cannot override it class Super { final void demo() { System.out.println("hello"); } } class Sub_ extends Super { /*void demo() { System.out.println("hello world"); }*/ void demo_1() { System.out.println("hello world"); } } class Demo { public static void main(String[] y) { Sub_ s=new Sub_(); s.demo(); } }
  • 9. Final class • We cannot extend a final class prevent inheritance final class Super { void demo() { System.out.println("hello"); } } class Sub_ extends Super { void demo_1() { System.out.println("hello world"); } } class Demo { public static void main(String[] y) { Sub_ s=new Sub_(); } }