Dynamic method dispatch

9
Dynamic Method Dispatc h Abstract Class Using Final with inher itance

Transcript of Dynamic method dispatch

Dynamic Method DispatchAbstract 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 subclassSyntax

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