Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java compiler super() constructor generals [duplicate]

Possible Duplicate:
Use of ‘super’ keyword when accessing non-overridden superclass methods

I'm new to Java and have been reading a lot about it lately to get more knowledge and experience about the language. I have a question about inherited methods and extending classes when the compiler inserts automatic code.

I've been reading that if I create class A with some methods including lets say a method called checkDuePeriod(), and then create a class B which extends class A and its methods.

If I then call the method checkDuePeriod() within class B without using the super.checkDuePeriod() syntax, during compilation will the compiler include the super. before checkDuePeriod() or will the fact that the compiler includes the super() constructor automatically when compiling the class imply the super. call of the methods that class B calls from class A?

I'm a little confused about this. Thanks in advance.

like image 758
luiguiv Avatar asked Oct 17 '12 03:10

luiguiv


3 Answers

The super class's implementation of regular methods is not automatically invoked in sub classes, but a form of the super class's constructor must be called in a sub class's constructor.

In some cases, the call to super() is implied, such as when the super class has a default (no-parameter) constructor. However, if no default constructor exists in the super class, the sub class's constructors must invoke a super class constructor directly or indirectly.

Default constructor example:

public class A {
    public A() {
        // default constructor for A
    }
}

public class B extends A {
    public B() {
        super(); // this call is unnecessary; the compiler will add it implicitly
    }
}

Super class without default constructor:

public class A {
    public A(int i) {
        // only constructor present has a single int parameter
    }
}

public class B extends A {
    public B() {
        // will not compile without direct call to super(int)!
        super(100);
    }
}
like image 168
FThompson Avatar answered Oct 05 '22 11:10

FThompson


If you call checkDuePeriod() in B without super., means you want to invoke the method that belongs to the this instance (represented by this within B) of B. So, it equivalent to saying this.checkDuePeriod(), so it just doesn't make sense for the compiler to add super. in the front.

super. is something that you must explicitly add to tell the compiler that you want to call the A's version of the method (it is required specially in case B has overridden the implementation provided by A for the method).

like image 31
Bhesh Gurung Avatar answered Oct 05 '22 13:10

Bhesh Gurung


Call of super() as a default constructor (constructor with no args) can be direct or non direct but it garants that fields of extendable class are properly initialized.

for example:

public class A {
    StringBuilder sb;
    public A() {
        sb = new StringBuilder();
    }
}

public class B extends A {
    public B() {
        //the default constructor is called automatically
    }
    public void someMethod(){
        //sb was not initialized in B class, 
        //but we can use it, because java garants that it was initialized
        //and has non null value
        sb.toString();
    }
}

But in case of methods:

Methods implement some logic. And if we need to rewrite logic of super class we use

public class B extends A {
    public B() {
    }
    public boolean checkDuePeriod(){
       //new check goes here
    }
}

and if we want just implement some extra check, using the value returned from checkDuePeriod() of superclass we should do something like this

public class B extends A {
    public B() {
    }
    public boolean checkDuePeriod(){
       if(super.checkDuePeriod()){
            //extra check goes here
       }else{
            //do something else if need
       }
       return checkResult;
    }
}
like image 41
cache Avatar answered Oct 05 '22 12:10

cache