Powered by Blogger.
Showing posts with label invoking overriding methods program. Show all posts
Showing posts with label invoking overriding methods program. Show all posts

About Java - Handle Exceptions and Overriding Methods in Java

Method Overriding and Exception:-

The overriding method can declare only exceptions that are either the same class or a subclass of the exception. For example, if the superclass method throws an IOException, then the overriding method of superclass method can throw an IOException, a FileNotFoundException, which is the subclass of IOException, but not Exception, which is the superclass of IOException.

Method Overriding and Exception Example:-

 public class A{
   public void methodA()throws IOException{}
 }

 class B extends A{
   public void methodA() throws EOFException{
     // Legal, because EOFException is the sub class of IOException;
   }
 }

 class C extends A{
   public void methodA() throws Exception{
     // Illegal, because Exception is the super class of IOException;
   }
 }

f a method throws an checked exception , then the overriding method cannot throws new or boarder checked exceptions. if a method throws FileNotFoundExcepti

on , its overriding method cannot throw IOException or Exception , but the overriding method need not throw any exception or can throw unchecked exceptions like RuntimeException.

The overriding method should not reduce the visibility of overridden method. if method is declared as protected , the overriding method must have protected or public access modifier but it cannot be default or private.

The return type of overridden and overriding method must be the same or must be of covariant type

if A is the return type of overridden method. the B is the return type of overriding method then "B is-a A" must be true else it results in a complier error.

class A{

}

interface Inter{

}

class B extends A implements inter {

}

class Base {

public Inter getObject() {

}

public A getResult() {

}

public Inter getX() {

}

public Object toString() {

}

}


class Child extends Base {

public B getObject() {}//ok B is-a inter

public B getResult() {}//ok B is-a A

public A getX() {}//complier error as A is-a Inter is false ie A is not a Inter

public String toString() {}//ok String is-a Object
}


Now let me say why we have to use Same or sub type of Exception in sub class test method declaration.

super1.test() is going to call subclass method in runtime because we are using polymorphic assignment in creating super1 instance.

if sub class method is allowed to throw any super type of exceptions declared in Super class method. that is if we are allowed to throw Exception instead IOException( or its subtype). it will not fit into catch statement since catch is using exception declared in super class method that is IOException.

That is the reason we have to use same exception or its subtype.

Complete Code is Given Below:

public class Overriding {
   public static void main(String[] args) {
     Super super1 = new Sub();
     try {
       super1.test();
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
}
class Super{
   public void test() throws IOException{
     System.out.println(”Super.test()”);
   }
}
class Sub extends Super{
   public void test(){
    System.out.println(”Sub.test()”);
   }
}

About Java - Invoking Overriding Methods in Java

Invoking Overriding Method:-

A sub-class method can invoke a super-class method using the super keyword. In the following code, line 14 invokes the parent class method showDetails().

Invoking Overriding Methods Program:-

 class Employee{
    public String name = ”Mahmud”;
    public float salary = 50000;

    public void showDetails(){
      System.out.println(name+” ”+salary);
   }
 }

 public class Manager extends Employee{
    public String department = ”Engineering”;

    public void showDe tails(){
    super.showDetails();
    System.out.println(” ”+department);
 }

 public static void main(String[]args){
     Manager m = new Manager();
     m.showDetails();
   }
 }