Poonam posted an Question
January 23, 2020 • 02:38 am 15 points
  • UGC NET
  • Computer Science & Applications

Consider the following code Px = new Q(); Qy = new Q0: Pz = new Q); x. f (1):((P) y). f (1); z.f(1); The output is (A) 121 (B) (D) 2 1 1 (C) 212 2 22

1 Answer(s) Answer Now
  • 1 Likes
  • 3 Comments
  • 0 Shares
  • comment-profile-img>
    Bajrang best-answer

    ans is D 222 this is polymorphism  Superclass object = new subclass(); object created for subclass referenced by a superclass  when anything invoked using this object (THIS IS IMPORTANT)  All the methods of superclass can be invoked which are not in subclass  AND when a method is invoked which is in both (overridden) the SUBCLASS Method will be called!!  Additional info-->  superclass to subclass requires EXPLICIT CASTING  subclass to superclass doesn't require explicit casting (although no problem if used) Inheritance will make sure every function in parent class P is also a part of Q. But since Q has overridden the function f, Q's definition of function f will be used whenever an object of type Q is used to call f. Dynamic binding determines the type of the object at run-time and not compile time. Refer  http://www.javatpoint.com/static-binding-and-dynamic-binding Looking at the pseudo code, in all the cases x.f(1), ((P) y), z.f(1), the objects are of type Q, hence function f from child class will be used. And since the definition of function f is present in parent class as well there wont be any exceptions.

whatsapp-btn

Do You Want Better RANK in Your Exam?

Start Your Preparations with Eduncle’s FREE Study Material

  • Updated Syllabus, Paper Pattern & Full Exam Details
  • Sample Theory of Most Important Topic
  • Model Test Paper with Detailed Solutions
  • Last 5 Years Question Papers & Answers