Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Flow of execution - overridden method gets executed first than the constructor

Tags:

java

debugging

I have the following code, in the same java file.

import javax.swing.SwingUtilities;
import java.io.File;

public class MainClass2{
   public static void main(String[] args){
       SwingUtilities.invokeLater(new Runnable(){
             public void run() {
                 javax.swing.JFileChooser jfc = new MyFileChooser();
                     File file = jfc.getSelectedFile();
             }

      });
   }
}

class MyFileChooser extends javax.swing.JFileChooser{
    public MyFileChooser(){
        System.out.println("constructor call");
    }
    @Override
    public java.io.File getSelectedFile(){
        System.out.println("call to getSelectedFile");
        return null;
    }
}

When I run it, the output gives me

call to getSelectedFile

constructor call

call to getSelectedFile

Shouldn't the output be

constructor call

call to getSelectedFile

I'm using java 5.

like image 206
Bnrdo Avatar asked Apr 04 '13 07:04

Bnrdo


People also ask

What is the advantage of using override?

Advantages of Method Overriding Method overriding helps in writing a generic code based on the parent class. It provides multiple implementations of the same method and can be used to invoke parent class overridden methods using super keyword. It defines what behavior a class can have.

What does the override method do?

Method overriding, in object-oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. It allows for a specific type of polymorphism (subtyping).

What is overriding constructor?

Constructor Overriding is never possible in Java. This is because, Constructor looks like a method but name should be as class name and no return value. Overriding means what we have declared in Super class, that exactly we have to declare in Sub class it is called Overriding.

Which method should be overridden when?

Rules for Method Overriding For example: If the superclass method is declared public then the overridding method in the sub class cannot be either private or protected. Instance methods can be overridden only if they are inherited by the subclass. A method declared final cannot be overridden.


1 Answers

MyFileChooser's constructor is equivalent to:

public MyFileChooser() {
    super(); // ***
    System.out.println("constructor call");
}

The first call to getSelectedFile() is made by MyFileChooser's base class constructor, which is invoked implicitly at the point marked *** above, before the System.out.println("constructor call").

Here is the stack trace:

MyFileChooser.getSelectedFile() line: 16    
AquaFileChooserUI.installComponents(JFileChooser) line: 1436    
AquaFileChooserUI.installUI(JComponent) line: 122   
MyFileChooser(JComponent).setUI(ComponentUI) line: 670  
MyFileChooser(JFileChooser).updateUI() line: 1798   
MyFileChooser(JFileChooser).setup(FileSystemView) line: 360 
MyFileChooser(JFileChooser).<init>(File, FileSystemView) line: 333  
MyFileChooser(JFileChooser).<init>() line: 286  
MyFileChooser.<init>() line: 11 
like image 169
NPE Avatar answered Oct 06 '22 00:10

NPE