-1

Is it possible to override a method of the super class of the extended class?

Here is the example:

public class ApplicationModuleImpl {
    protected void prepareSession(oracle.jbo.Session p1) { 
        // Preparing the session
    }
}

public class BaseModuleImpl extends ApplicationModuleImpl {

}

Class BaseModuleImpl doesn't override any methods of ApplicationModuleImpl, then I have:

public class MyClass extends BaseModuleImpl {
    protected void prepareSession(Session session) {
        super.prepareSession(session);
    }
}

Does this really override the method?

EDIT:

I am using ADF with faces in jdeveloper and I am extending as you can see the applicationModule class and my class extends the extended class. and the method prepareSession must be called if it is overrided, it it is not being called at all.
Why it isn't being called then, at its loading time, since it's extending ADF ApplicationModuleImpl ??

pnuts
  • 54,806
  • 9
  • 74
  • 122
GingerHead
  • 7,882
  • 14
  • 54
  • 91
  • 3
    Annotate your method with `@Override` and then compile. If the compiler doesn't complain about the usage of the annotation then yes it properly overrides the (super super) method. [When do you use Java's @Override annotation and why?](http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why) – Bhesh Gurung Jun 13 '14 at 16:24

3 Answers3

2

Yes this is overriding the method since MyClass is inheriting its properties from BaseModuleImpl which in turn inherits from ApplicationModuleImpl. But remember that super calls the most immediate parent. So if you had overriden prepareSession in BaseModuleImpl, then prepareSession of MyClass would call BaseModuleImpl's prepareSession.

Vivin
  • 1,237
  • 1
  • 9
  • 26
  • Can you rephrase your question? I did not get what you are asking. – Vivin Jun 14 '14 at 21:58
  • So you mean *prepareSession* of ApplicationModuleImpl is not being called? – Vivin Jun 16 '14 at 15:32
  • I don't understand. The only case when the protected method is not accessible is when, it is being called by a class not in the same package unless the class calling is a subclass which MyClass is. Are you sure you are not missing anything? – Vivin Jun 17 '14 at 17:51
1

Yes you can since MyClass essentially also extends ApplicationModuleImpl. The ApplicationModuleImpl class is also considered a parent class for MyClass.

M A
  • 65,721
  • 13
  • 123
  • 159
1

Yes,you can override the method of super class into subclass.

Method overriding is itself means: The specific implementation of the method that has been provided by one of its parent class, it is known as Method Overriding.

to know more about overriding refer Java - Overriding

And also refer Why is super.super.method(); not allowed in Java? this question may help you.

Community
  • 1
  • 1
rachana
  • 3,158
  • 6
  • 27
  • 45