Java - Interview Questions and Answers for 'Overriding' | Search Java Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com
Share

Search Java Interview Questions


 2098 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Overriding' - 24 question(s) found - Order By Newest

 Q1. What are points to consider in terms of access modifier when we are overriding any method?Core Java
Admin
info@buggybread.com
Ans. 1. Overriding method can not be more restrictive than the overridden method.

reason : in case of polymorphism , at object creation jvm look for actual runtime object. jvm does not look for reference type and while calling methods it look for overridden method.

If by means subclass were allowed to change the access modifier on the overriding method, then suddenly at runtime when the JVM invokes the true objects version of the method rather than the reference types version then it will be problematic

2. In case of subclass and superclass define in different package, we can override only those method which have public or protected access.

3. We can not override any private method because private methods can not be inherited and if method can not be inherited then method can not be overridden.

  Sample Code for Overriding

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   overriding   access specifier   inheritence   oops   polymorphism   runtime polymorphism     Genpact

Try 2 Question(s) Test


Very Frequently asked to fresh graduates and less experienced. Favorite question in Walk in drives.
  Q2. Difference between Overloading and Overriding ?Core Java
Anonymous
Ans. Overloading - Similar Signature but different definition , like function overloading.

Overriding - Overriding the Definition of base class in the derived class.

  Sample Code for overloading

  Sample Code for overriding

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   oops   overloading   overriding   oops concepts   basic interview question     PC Technology  Cognizant (CTS)  Donlen   hybris Software  RedMane  Allerin Technologies  Gemalto  Allerin Technologies  HCL Technologies  Tata Consultancy (TCS)  Accenture India  Infosys Technologies  Polaris  United Healthcare India  IBM India  Xchanging  American Express  Antra  Flipp  Computer Sciences Corporation (CSC)  Garmin  NetSuite  Donlen  Hudsons Bay  Techlogix  SevOne  Synactive  Oodles Technologies  FDM Group  Marlabs  RedMane Technology  Faichi Solutions  TD  Tech Mahindra  Shutterfly  Luxoft  PC Technology  CCC Information Services  State Farm  Raja Software  Mastek  EPAM  Morgan Stanley  SummitWorks  Sony  Citibank  Emtec  LowerMyBills  Consilience  Xavor  AllState  FasTrak  AmTrust Financial  Walt Disney  Ultramain Systems  International Turnkey Systems  Knoldus Software  CA Technologies  Amdocs  FDM Group  Hybris  Fidelity  United Health Group (UHG)  Bluewhale Solutions  Pegasystems  Revature   Innoplexus  Vanguard  AptSource  Perficient  Grapecity  Subex  QuintilesIMS  NimbleUser  Expeditors Associate  Sapient  HR Cloud  Summitworks  Jabong.com  Kony Labs  Price Waterhouse Coopers (PWC)      basic        frequent

Try 1 Question(s) Test


 Q3. How compiler handles the exceptions in overriding ?Core Java
Admin
info@buggybread.com
Ans. 1)The overriding methods can throw any runtime Exception , here in the case of runtime exception overriding method (subclass method) should not worry about exception being thrown by superclass method.

2)If superclass method does not throw any exception then while overriding, the subclass method can not throw any new checked exception but it can throw any runtime exception

3) Different exceptions in java follow some hierarchy tree(inheritance). In this case , if superclass method throws any checked exception , then while overriding the method in subclass we can not throw any new checked exception or any checked exception which are higher in hierarchy than the exception thrown in superclass method

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   overriding   exceptions   inheritence   inheritance   oops


 Q4. What is a Final Method ?Core Java
Anonymous
Ans. A Method that cannot be overriden in the sub class.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   oops   java keywords   final   final method   overriding   basic interview question      basic        frequent

Try 1 Question(s) Test


 Q5. Can we reduce the visibility of the inherited or overridden method ?Core Java
Anonymous
Ans. No.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   oops   access specifier   overriding   overridding   inheritence   yes-no


 Q6. what is covariant return type? Core Java
Admin
info@buggybread.com
Ans. co-variant return type states that return type of overriding method can be subtype of the return type declared in method of superclass. it has been introduced since jdk 1.5

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   oops   inheritence   overriding   covariant return type   jdk5   advanced     Hewlett Packard (HP)  The Systems Group  Evergent      expert


 Q7. Overridden methods must have the same ...

a. name
b. name and argument list
c. name, argument list, and return type
d. name, argument list, return type and belong to the same class
Anonymous
Ans. name and argument list

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   overriding

Try 2 Question(s) Test


 Q8. Can we reduce the visibility of the overridden method ?Core Java
Anonymous
Ans. No

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     overriding   access specifier   oops   java   yes-no


 Q9. Does java allow overriding static methods ?Core Java
Anonymous
Ans. No. Static methods belong to the class and not the objects. They belong to the class and hence doesn't fit properly for the polymorphic behavior.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   oops   static   static method   overriding      intermediate

Try 3 Question(s) Test



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q10. What restrictions are placed on method overriding?
Anonymous
Ans. Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides. The overriding method may not throw any exceptions that may not be thrown by the overridden method.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   oops   overriding      basic        frequent

Try 2 Question(s) Test


 Q11. How is static and dynamic polymorphism achieved in Java ?Core Java

Ans. Static polymorphism is the polymorphic resolution identified at compile time and is achieved through function overloading whereas dynamic polymorphism is the polymorphic resolution identified at runtime and is achieved through method overriding.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     static polymorphism   dynamic polymorphism   polymorphism   overloading   overriding     Cognizant (CTS)      basic        frequent


 Q12. Any real life example of Overloading and Overridding ?2016-06-13 10:44:01

Ans. All members of a family share a common last name and thats how outsiders recognize them, but to recognize individual team members , we need an overloaded name and hence we use First Name along with Last Name to uniquely identify.

Some members of a family have a Nick Name as the real name is very long or hard to call. So the Nick name overrides the Original Name in certain circumstances.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     overloading  overriding


 Q13. Can we override abstract methods ?Core Java2017-03-01 09:45:08

Ans. Abstract methods are the methods that need to be overridden in the derived class ( either as implementing method or again abstract method ) so it's not only allowed but its required to override abstract method in the derived class.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     overriding  abstract methods  overriding abstract methods


 Q14. Can we override an abstract method with abstract method in the derived class ?Core Java2017-03-01 09:46:35

Ans. Yes, but in that case the derived class itself should be abstract. We cannot have an object of a class without definition for the method which has been declared abstract in the parent.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     overriding  overriding abstract methods  abstract methods


 Q15. Which of the two - compile time and run time polymorphism - requires signature of the method to be different ?Core Java2017-05-02 08:31:29

Ans. runtime polymorphism or method overriding doesn't require method name and signature to be different whereas compile time polymorphism or method overloading requires method name to be same but the signature to be different.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     polymorphism  overloading  overriding      basic


 Q16. How does java identifies which method to be called in method overriding or runtime polymorphism, when both methods share the same name and signature ? Core Java2017-05-02 08:33:52

Ans. Java identifies the method to be called at runtime by the object that is being referenced.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     polymorphism  overloading  overriding      basic


 Q17. What is "dynamic method dispatch" in java ?Core Java2017-05-07 16:27:26

Ans. dynamic method dispatch is a process in which a call to an
overridden method is resolved at runtime rather than at compile-time. This is used to achieve runtime polymorphism in java.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     overriding  runtime polymorphism


 Q18. Can we override the overloaded methods too ?Core Java2017-05-07 16:31:47

Ans. Yes. Both concepts are independent of each other. Overriding depends on the methods with same name and signature whereas Overloading on same name but with different signatures. All definitions of an overloaded method can be individually overridden in the derived class.

For example -

Class A has overloaded methods "public void myMethod(String str)" and "public void myMethod(int x)"

We can have derived class ClassB extends ClassA and hence can override both methods myMethod(String str) and myMethod(int x) in ClassB

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     overloading  overriding


 Q19. Is Runtime Polymorphism possible without Inheritance ?Core Java2017-08-06 19:12:26

Ans. Yes, Runtime Polymprohism requires either Class inheritance or Interface implementation.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     runtime polymorphism  method overriding  inheritance


 Q20. Which of the following can be overridden ?Core Java
a. final instance methods
b. final static methods
c. non final instance methods
d. non final static methods

Ans.c. non final instance methods

 Q21. What will be the ouput upon executing main method ?

public class Vehicle {
   public void move(){
      System.out.println("Vehicle is moving");
   }
}

public class Car {
   public void move(){
      super().move();
      System.out.println("Car is moving");
   }

   public static void main(String[] args){
      new Car().move();
   }
}
Core Java
a. Vehicle is moving
b. Car is moving
c. Vehicle is moving Car is moving
d. Compilation Error

Ans.d. Compilation Error

 Q22. What will be the output upon exceuting main method ?

public class Vehicle {
   private void move(){
      System.out.println("Vehicle is moving");
   }
}

public class Car extends Vehicle{
   public void move(){
      System.out.println("Car is moving");
   }

   public static void main(String[] args){
      Vehicle vehicle = new Car();
      vehicle.move();
   }
}
Core Java
a. Vehicle is moving
b. Car is moving
c. Vehicle is moving Car is moving
d. Compilation error

Ans.d. Compilation error

 Q23. What will be the output upon executing the main method ?

public class Vehicle {
   public void move(){
      System.out.println("Vehicle is moving");
   }
}

public class Car extends Vehicle{
   public void move(){
      System.out.println("Car is moving");
   }

   public static void main(String[] args){
      Vehicle vehicle = new Vehicle();
      ((Car)vehicle).move();
   }
}
Core Java
a. Vehicle is moving
b. Car is moving
c. Cimpilation Error
d. Runtime Exception

Ans.d. Runtime Exception

 Q24. Overriding is not possible without ..Core Java
a. Inheritance
b. Composition
c. Inner Classes
d. Abstract Classes

Ans.a. Inheritance



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: