SlideShare a Scribd company logo
2
Most read
3
Most read
4
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

PPT
ABSTRACT CLASSES AND INTERFACES.ppt
PPTX
Interface in java ,multiple inheritance in java, interface implementation
PPSX
Java Multi-threading programming
PPTX
Ppt on this and super keyword
PPT
Thread model in java
PDF
Java Inheritance
PPTX
Abstract Class & Abstract Method in Core Java
PPT
Method overriding
ABSTRACT CLASSES AND INTERFACES.ppt
Interface in java ,multiple inheritance in java, interface implementation
Java Multi-threading programming
Ppt on this and super keyword
Thread model in java
Java Inheritance
Abstract Class & Abstract Method in Core Java
Method overriding

What's hot (20)

PPTX
I/O Streams
PPTX
Java Strings
PPTX
Inter Thread Communicationn.pptx
PPTX
JAVA AWT
PPT
Generics in java
PPTX
Java Method, Static Block
PPTX
Control Statements in Java
PDF
Class and Objects in Java
PPTX
Java awt (abstract window toolkit)
PPT
Java Streams
PDF
Java thread life cycle
PPTX
Static Members-Java.pptx
PPTX
File handling
PPTX
Control statements in java
PPTX
Java exception handling
PPTX
07. Virtual Functions
PPTX
Java swing
PPT
Java And Multithreading
PDF
Arrays in Java
I/O Streams
Java Strings
Inter Thread Communicationn.pptx
JAVA AWT
Generics in java
Java Method, Static Block
Control Statements in Java
Class and Objects in Java
Java awt (abstract window toolkit)
Java Streams
Java thread life cycle
Static Members-Java.pptx
File handling
Control statements in java
Java exception handling
07. Virtual Functions
Java swing
Java And Multithreading
Arrays in Java
Ad

Viewers also liked (20)

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
PPTX
Core java online training
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
Core java online training
Ad

Similar to Dynamic method dispatch (20)

PPTX
FINAL_DAY9_METHOD_OVERRIDING_Role and benefits .pptx
PPT
Java inheritance
PPTX
Inheritance.pptx
DOCX
JAVA Notes - All major concepts covered with examples
PPT
L7 inheritance
PPT
L7 inheritance
PPTX
Java tutoria part 2
PPTX
Chap3 inheritance
PPTX
object oriented programming unit two ppt
PPTX
Java tutorial part 2
PPTX
Unit3 part2-inheritance
PPTX
Unit3 inheritance
PDF
Java unit2
PPT
Inheritance and-polymorphism
PDF
JAVA-INHERITANCE unitfgfjjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
DOCX
Core java by amit
PPTX
Ch5 inheritance
PPTX
Chapter 9 java
DOCX
Binding,interface,abstarct class
DOC
116824015 java-j2 ee
FINAL_DAY9_METHOD_OVERRIDING_Role and benefits .pptx
Java inheritance
Inheritance.pptx
JAVA Notes - All major concepts covered with examples
L7 inheritance
L7 inheritance
Java tutoria part 2
Chap3 inheritance
object oriented programming unit two ppt
Java tutorial part 2
Unit3 part2-inheritance
Unit3 inheritance
Java unit2
Inheritance and-polymorphism
JAVA-INHERITANCE unitfgfjjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Core java by amit
Ch5 inheritance
Chapter 9 java
Binding,interface,abstarct class
116824015 java-j2 ee

More from yugandhar vadlamudi (13)

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
PPTX
Class introduction in java
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
Class introduction in java

Recently uploaded (20)

PPTX
Cell Types and Its function , kingdom of life
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Updated Idioms and Phrasal Verbs in English subject
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Cell Structure & Organelles in detailed.
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
Computing-Curriculum for Schools in Ghana
PPTX
master seminar digital applications in india
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Cell Types and Its function , kingdom of life
Final Presentation General Medicine 03-08-2024.pptx
Updated Idioms and Phrasal Verbs in English subject
STATICS OF THE RIGID BODIES Hibbelers.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Cell Structure & Organelles in detailed.
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
RMMM.pdf make it easy to upload and study
Computing-Curriculum for Schools in Ghana
master seminar digital applications in india
Weekly quiz Compilation Jan -July 25.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Classroom Observation Tools for Teachers
Final Presentation General Medicine 03-08-2024.pptx
Chinmaya Tiranga quiz Grand Finale.pdf
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Paper A Mock Exam 9_ Attempt review.pdf.
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE

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_(); } }