Given the following class diagram in which of the following

Given the following class diagram, in which of the following cases would dynamic binding be needed. Assume that the compiler knows that no additional subclasses can be created of any class, nor can any extra methods be added. a) Operation methodName2 is invoked on a variable of type A b) Operation methodName1 is invoked on a variable of type A c) Operation methodName1 is invoked on a variable of type B d) Operation methodName1 is invoked on a variable of type C e) More than one of the above In the diagram of question 1, which of the following should result in a compiler error a) Operation methodNanie2 is invoked on a variable of type A b) Operation methodName2 is invoked on a variable of type C c) Operation methodName1 is invoked on a variable of type B d) Operation methodName1 is invoked on a variable of type C e) More than one of the above

Solution

Please follow the data and description :

a) Dynamic Binding :

This type of binding the object to the data is determined at the run-time, then it is known as dynamic binding. It uses Object to resolve the binding. In this kind only the overridden methods are bonded at runtime.

Example :

CODE :

public class Animal {

    void eat() {
        System.out.println(\"animal is eating...\");
    }
}

class Cat extends Animal {
  
    void drink() {
        System.out.println(\"Cat is Drinking\");
    }
}
class Dog extends Animal {

    @Override
    void eat() {
        System.out.println(\"dog is eating...\");
    }
  
    public static void main(String args[]) {
        Animal a = new Dog();
        Animal d = new Dog();
        Animal c = new Cat();
        c.eat();
        d.eat();
        a.eat();
    }
}

OUTPUT :

animal is eating...
dog is eating...
dog is eating...

So from the given perspective of the method calling the call can be occured in any of the kind as suggested so the method1 can be called using the variable types of any of the classes may be the parent or even the child class objects. So this matches with 3 cases in the given options.

So the answer is OPTION E (More than one of the above).

2)

For the sample example above, if the method 2 is called using the variable type of A or the Animal it gives a acompile error as the method required is not present in the respective class so throws a compile-time error stating that the method required is not present in the class.

So the answer is OPTION A (Operation methodName2 is invoked on a variable of type A).

Hope this is helpful.

 Given the following class diagram, in which of the following cases would dynamic binding be needed. Assume that the compiler knows that no additional subclasse
 Given the following class diagram, in which of the following cases would dynamic binding be needed. Assume that the compiler knows that no additional subclasse

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site