Muneeb Nasir
Muneeb Nasir

Reputation: 2504

Java execution flow?

what will be the flow of execution in case of override? What i believe is , when we call a constructor/object of any class, during execution first it call parent constructor and than child. but what will happen in case of over ridding? lets suppose:

class A {
    public  A(){
        printStatus();
    }

    public void printStatus(){
        System.out.println("In Class A");
    }
}

class B  extends A{

    public  B(){
        printStatus();
    }

    @Override
    public void printStatus(){
        System.out.println("In Class b");
    }

}

public class Test2 {
    public static void main(String[] args){
        B b = new B();
    }
} 

Out put of this code is:

In Class b
In Class b

what i don't understand is, why it's printing "In Class be" only, it should be "In class A and, In Class b",
when i remove override method from class b. it give me desired output.

Upvotes: 2

Views: 984

Answers (7)

Nagaraja
Nagaraja

Reputation: 581

 Try with like this :

class A {
    public  A(){
        printStatus();
    }

    public void printStatus(){
        System.out.println("In Class A");
    }
}

class B  extends A{

    public  B(){
        super.printStatus();
        printStatus();
    }

    @Override
    public void printStatus(){
        System.out.println("In Class b");
    }

}

public class Test2 {
    public static void main(String[] args){
        B b = new B();
    }
} 

For better understanding the concepts of Overloading and Overriding just go through this links:

http://en.wikibooks.org/wiki/Java_Programming/Overloading_Methods_and_Constructors

Upvotes: 0

vels4j
vels4j

Reputation: 11298

Should use super keyword for calling super class method.

class A {
    public  A(){
        printStatus();
    }

    public void printStatus(){
        System.out.println("In Class A");
    }
}

class B  extends A{

    public  B(){
        super.printStatus();
    }

    @Override
    public void printStatus(){
        System.out.println("In Class b");
    }

}

Constructor public B(){ super.printStatus(); } calls Class A print method and constructor public A(){ printStatus(); } calls Class B print method since you've overridden.

But its wrong with overridable method calls in constructors.

Upvotes: 0

Hendra Syailendra
Hendra Syailendra

Reputation: 71

When you override a method, it will override the one that you expect from class A.

Upvotes: 0

Amit Kumar user3037405
Amit Kumar user3037405

Reputation: 390

super is always called whether you write super(); or not.

In the example printStatus() method of Class A will never be called. Since you are creating an instance of class B and there will be method overriding. You can use the following to call the Class A printStatus() method.

public B()
{
    super.printStatus();
}

Upvotes: 1

Tim B
Tim B

Reputation: 41168

When you over-ride a method you over-ride it completely. The existence of the original implementation is completely invisible to other classes (except via reflection but that's a big topic of its own and not really relevant). Only your own class can access the original method and that is by calling super.methodName().

Note that your class can call super.methodName() anywhere, not just in the overriding function, although the most usual use for it is in the overriding function if you want the super implementation to run as well as your own.

Constructors are a slightly special case as there are rules about how and why constructors are called in order to make sure that your super-class is fully initialized when you try and use it in the inheriting class.

Upvotes: 2

Saša Šijak
Saša Šijak

Reputation: 9291

Calling like this printStatus() will call the method from the same class. If you call with super.printStatus() it will envoke method from the super class (class which you have extended).

Upvotes: 2

Damask
Damask

Reputation: 1254

All java methods are virtual. It means the method is called with using actual type of this. So inside of constructor A() {} this is the instance of B, so that is why you've got its method call.

Upvotes: 3

Related Questions