20CS43P : Object Oriented Programming and Design with Java
Tutorial Activity: 9
1. Compare and Contrast. Static and Dynamic binding and identify
usage of each
Binding is a mechanism creating link between method call and method
actual implementation. As per the polymorphism concept in Java , object can have
many different forms. Object forms can be resolved at compile time and run time.
If linking between method call and method implementation is resolved at compile
time then we call it static binding or If it is resolved at run time then it dynamic
binding. Dynamic binding uses object to resolve binding but static binding use
type of the class and fields.
Sr. Key Static Binding Dynamic Binding
No.
1 Basic It is resolved at compile time It is resolved at run time
2 Resolve static binding use type of the Dynamic binding uses
mechanism class and fields object to resolve binding
3 Example Overloading is an example of Method overriding is the
static binding example of Dynamic
binding
4. Type of private, final and static Virtual methods use
Methods methods and variables uses dynamic binding
static binding
Example of Static and Dynamic Binding
public class FastFood {
public void create() {
System.out.println("Creating in FastFood class");
}
}
public class Pizza extends FastFood {
public void create() {
System.out.println("Creating in Pizza class");
}
}
Kashinath Bedare, Lecturer in Govt. Women Polytechnic, Shiralakoppa. Page 1
20CS43P : Object Oriented Programming and Design with Java
public class Main {
public static void main(String[] args) {
FastFood fastFood= new FastFood();
fastFood.create();
//Dynamic binding
FastFood pza= new Pizza();
pza.create();
}
}
2. Compare and Contrast. Abstract class and Interface, identify
usage of each.
Abstract class and interface both are used to achieve abstraction where we
can declare the abstract methods. Abstract class and interface both can't be
instantiated.
But there are many differences between abstract class and interface that are
given below.
Abstract class Interface
1) Abstract class can have abstract and non- Interface can have only
abstract methods. abstract methods. Since Java 8, it can
have default and static methods also.
2) Abstract class doesn't support multiple Interface supports multiple
inheritance. inheritance.
3) Abstract class can have final, non-final, Interface has only static and final
static and non-static variables. variables.
4) Abstract class can provide the Interface can't provide the
implementation of interface. implementation of abstract class.
5) The abstract keyword is used to declare The interface keyword is used to
abstract class. declare interface.
Kashinath Bedare, Lecturer in Govt. Women Polytechnic, Shiralakoppa. Page 2
20CS43P : Object Oriented Programming and Design with Java
6) An abstract class can extend another Java An interface can extend another Java
class and implement multiple Java interfaces. interface only.
7) An abstract class can be extended using An interface can be implemented
keyword "extends". using keyword "implements".
8) A Java abstract class can have class Members of a Java interface are public
members like private, protected, etc. by default.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface
achieves fully abstraction (100%).
Kashinath Bedare, Lecturer in Govt. Women Polytechnic, Shiralakoppa. Page 3