The Artima Developer Community
Sponsored Link

Java Buzz Forum
Top 10 Interview Programs and questions on method overriding in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Top 10 Interview Programs and questions on method overriding in java Posted: Feb 17, 2016 7:01 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Top 10 Interview Programs and questions on method overriding in java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
1.What is method overriding in java?
  • Defining multiple methods with same name and same signature in super class and sub class known as method overriding. 
  • Method overriding is type of polymorphism in java which is one of the main object oriented feature.
  • Redefined the super class method in sub class is known as method overriding.
  • method overriding allows sub class to provide specific implementation that is already defined in super class.
  • Sub class functionality replaces the super class method functionality (implementation).
2. Can we override private methods in java?



3. Can we override static methods of super class in sub class?
  • NO.Its not possible to override static methods because static means class level so static methods not involve in inheritance.

 4. Can we change the return type of overridden method in sub class?
  • No. Return type must be same in super class and sub class.


  1. package MethodOverridingExamplePrograms;
  2. public class Super{
  3.  
  4. void add(){
  5.  System.out.println("Super class add method");
  6. }

  7. }

  1. package MethodOverridingInterviewPrograms;
  2. public class Sub extends Super{
  3.   
  4. int add(){    //Compiler Error: The return type is incompatible with Super.add()
  5.  
  6. System.out.println("Sub class add method");
  7. return 0; 

  8. }

  9. }

5.Can we change accessibility modifier in sub class overridden method?
  • Yes we can change accessibility modifier in sub class overridden method but should increase the accessibility if we decrease compiler will throw an error message.


Super class method  Subclass method
protected protected, public
public      public
default     default , public
6.What happens if we try to decrease accessibility from super class to sub class?
  • Compile time error will come.
  1. package MethodOverridingExamplePrograms;
  2. public class Super{
  3.  
  4. public void add(){
  5.  System.out.println("Super class add method");
  6. }

  7. }

  1. package MethodOverridingInterviewPrograms;
  2. public class Sub extends Super{
  3.   
  4. void add(){ //Compiler Error: Cannot reduce the visibility of the inherited method
  5. from Super
  6.  
  7. System.out.println("Sub class add method");

  8. }

  9. }


method overriding example program interview question


7.Can we override a super class method without throws clause to with throws clause in the sub class?

  • Yes if super class method throws unchecked exceptions.
  • No if super class method throws checked exceptions.

 8.What are the rules we need to follow in overriding if super class method throws exception ?

  •  If sub class throws unchecked exception super class should throw same or super class exception of this.
  • If super class method  throws checked or unchecked exceptions its not mandatory to put throws in sub class overridden method.
  • If super class method throws exceptions in sub class if you want to mention throws  then use  same class  or its  sub class exception.

 9.What happens if we not follow these rules if super class method throws some exception.

  •  Compile time error will come.

  1. package MethodOverridingExamplePrograms;
  2. public class Super{
  3.  
  4. public void add(){
  5.  System.out.println("Super class add method");
  6. }

  7. }

  1. package MethodOverridingInterviewPrograms;
  2. public class Sub extends Super{
  3.   
  4. void add() throws Exception{ //Compiler Error: Exception Exception is not compatible with
  5. throws clause in Super.add()
  6. System.out.println("Sub class add method");

  7. }

  8. }

  1. package MethodOverridingExamplePrograms;
  2. public class Super{
  3.  
  4. public void add(){
  5.  System.out.println("Super class add method");
  6. }

  7. }

  1. package MethodOverridingInterviewPrograms;
  2. public class Sub extends Super{
  3.   
  4. void add() throws NullPointerException{ // this method throws unchecked exception so no
  5. isuues
  6. System.out.println("Sub class add method");

  7. }

  8. }

10.Can we change an exception of a method with throws clause from unchecked to checked while overriding it?
  •  No. As mentioned above already
  • If super class method throws exceptions in sub class if you want to mention throws  then use  same class  or its  sub class exception.
  • So we can not change from unchecked to checked

Read: Top 10 Interview Programs and questions on method overriding in java

Topic: Kids : HouseHold Items v8 Previous Topic   Next Topic Topic: Object-Oriented approach to Code Generation

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use