SlideShare ist ein Scribd-Unternehmen logo
1 von 9
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_(); }
}

Weitere ähnliche Inhalte

Was ist angesagt?

Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 

Was ist angesagt? (20)

Structure in C
Structure in CStructure in C
Structure in C
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Applets
AppletsApplets
Applets
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Java threads
Java threadsJava threads
Java threads
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Methods in java
Methods in javaMethods in java
Methods in java
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 

Andere mochten auch

Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
myrajendra
 
Exception handling
Exception handlingException handling
Exception handling
Iblesoft
 
Access Protection
Access ProtectionAccess Protection
Access Protection
myrajendra
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
Vadym Lotar
 

Andere mochten auch (20)

Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
 
Exception handling
Exception handlingException handling
Exception handling
 
Java exception
Java exception Java exception
Java exception
 
Access Protection
Access ProtectionAccess Protection
Access Protection
 
Effective Java - Chapter 4: Classes and Interfaces
Effective Java - Chapter 4: Classes and InterfacesEffective Java - Chapter 4: Classes and Interfaces
Effective Java - Chapter 4: Classes and Interfaces
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
 
Exception handling in asp.net
Exception handling in asp.netException handling in asp.net
Exception handling in asp.net
 
Exception Handling Java
Exception Handling JavaException Handling Java
Exception Handling Java
 
Toolbarexample
ToolbarexampleToolbarexample
Toolbarexample
 
02basics
02basics02basics
02basics
 
Non ieee dot net projects list
Non  ieee dot net projects list Non  ieee dot net projects list
Non ieee dot net projects list
 
Yaazli International Spring Training
Yaazli International Spring Training Yaazli International Spring Training
Yaazli International Spring Training
 
Java quick reference v2
Java quick reference v2Java quick reference v2
Java quick reference v2
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Yaazli International Web Project Workshop
Yaazli International Web Project WorkshopYaazli International Web Project Workshop
Yaazli International Web Project Workshop
 
For Loops and Variables in Java
For Loops and Variables in JavaFor Loops and Variables in Java
For Loops and Variables in Java
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
 
Yaazli International AngularJS 5 Training
Yaazli International AngularJS 5 TrainingYaazli International AngularJS 5 Training
Yaazli International AngularJS 5 Training
 
Yaazli International Hibernate Training
Yaazli International Hibernate TrainingYaazli International Hibernate Training
Yaazli International Hibernate Training
 

Ähnlich wie Dynamic method dispatch

Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
backdoor
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
Sunil Kumar Gunasekaran
 
polymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdfpolymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdf
BapanKar2
 

Ähnlich wie Dynamic method dispatch (20)

Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Best Core Java Training In Bangalore
Best Core Java Training In BangaloreBest Core Java Training In Bangalore
Best Core Java Training In Bangalore
 
Inheritance
InheritanceInheritance
Inheritance
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
Unit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docxUnit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docx
 
Inheritance
InheritanceInheritance
Inheritance
 
Keyword of java
Keyword of javaKeyword of java
Keyword of java
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
Binding,interface,abstarct class
Binding,interface,abstarct classBinding,interface,abstarct class
Binding,interface,abstarct class
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
polymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdfpolymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdf
 
Java basic
Java basicJava basic
Java basic
 
Java basic tutorial
Java basic tutorialJava basic tutorial
Java basic tutorial
 
Java tutoria part 2
Java tutoria part 2Java tutoria part 2
Java tutoria part 2
 

Mehr von yugandhar vadlamudi

Mehr von yugandhar vadlamudi (12)

Singleton pattern
Singleton patternSingleton pattern
Singleton pattern
 
Object Relational model for SQLIite in android
Object Relational model for SQLIite  in android Object Relational model for SQLIite  in android
Object Relational model for SQLIite in android
 
JButton in Java Swing example
JButton in Java Swing example JButton in Java Swing example
JButton in Java Swing example
 
Collections framework in java
Collections framework in javaCollections framework in java
Collections framework in java
 
Packaes & interfaces
Packaes & interfacesPackaes & interfaces
Packaes & interfaces
 
JMenu Creation in Java Swing
JMenu Creation in Java SwingJMenu Creation in Java Swing
JMenu Creation in Java Swing
 
Adding a action listener to button
Adding a action listener to buttonAdding a action listener to button
Adding a action listener to button
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Inheritance
InheritanceInheritance
Inheritance
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
Closer look at classes
Closer look at classesCloser look at classes
Closer look at classes
 
java Applet Introduction
java Applet Introductionjava Applet Introduction
java Applet Introduction
 

Kürzlich hochgeladen

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Kürzlich hochgeladen (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.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_(); } }