What keyword allows a child class to access its parent classs implementation of a method that the child class is overriding?

Use the keyword super within the overridden method in the child class to use the parent class method. You can only use the keyword within the overridden method though. The example below will help.

public class Parent { public int add(int m, int n){ return m+n; } } public class Child extends Parent{ public int add(int m,int n,int o){ return super.add(super.add(m, n),0); } } public class SimpleInheritanceTest { public static void main(String[] a){ Child child = new Child(); child.add(10, 11); } }

The add method in the Child class calls super.add to reuse the addition logic.

Method Overriding in Java allows a subclass (or child class) to provide a specific implementation of a method which is already defined in its super class (or parent class). When a method in a subclass has the same name, parameters and return type as the method defined in its super class, then the subclass class overrides the method of the superclass. In other words, If a subclass provides the specific implementation of a method that is already declared by one of its parent class, it is known as method overriding. Method overriding in java is used to achieve Run Time Polymorphism. It means, is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time.

Which version of the method needs to be executed is determined by the object that is used to invoke it. If an object of a super class is used to invoke the method, then the method version in the super class is executed whereas if an object of the subclass is used to invoke the method, then the method version in the child class is executed.

Java program for method overriding

class Bird { String birdtype; Bird(String birdtype) { this.birdtype = birdtype; } public void move() { System.out.println(birdtype + " is Flying"); } } class Ostrich extends Bird { Ostrich(String birdtype) { super(birdtype); } public void move() { System.out.println(birdtype + " is Running"); } } public class MethodOverriding { public static void main(String[] args) { // Create object of parent class Bird bird = new Bird("Crow"); // Create object of child class Ostrich ostrich = new Ostrich("Ostrich"); // Invoke move() of parent class bird.move(); // Invoke move() of child class ostrich.move(); } } Output Crow is Flying Ostrich is Running

In the above program, the move() method is present in both Bird superclass and Ostrich subclass. As we know, that Ostriches cannot fly like other birds. Hence, we have overriden the move() method in Ostrich class.

When we call move() using the bird object (object of the super class), the method inside the Bird is invoked whereas when we call move() using ostrich object (object of the sub class) the method inside the Ostrich is invoked.

Rules for Method Overriding in Java

  • The method name, argument list and return type of method in child class should be exactly the same as the original overridden method in the parent class.

  • A method declared final in parent class cannot be overridden.

  • A method declared static in parent class cannot be overridden.

  • Constructors cannot be overridden.

  • If a method cannot be inherited, then it cannot be overridden.

  • If a class is extending an abstract class or implementing an interface then it has to override all the abstract methods unless the class itself is a abstract class.

As we know that, when we override a method in child class, then call to the method using child class object calls the overridden method of child class. A common question that arrises in our mind is

Can we access the overridden method of the parent class from child class ?

Yes, we can. We can call parent class overridden method from child class using super keyword. Here is a java program to access overridden method of parent class from child class.

class Driver { // overridden method public void getInfo() { System.out.println("I am a Driver."); } } class TruckDriver extends Driver { // overriding getInfo method public void getInfo() { // calling base class overridden method. super.getInfo(); System.out.println("I am Driving a Truck."); } } public class MethodOverridingSuper { public static void main(String[] args) { TruckDriver truckDriver = new TruckDriver(); // Calling overridden method truckDriver.getInfo(); } } Output I am a Driver. I am Driving a Truck.

In the above program, the sub class TruckDriver overrides the getInfo() method of superclass Driver. Inside getInfo() method of TruckDriver class, we used super.getInfo() to call getInfo() method of the super class Driver.

Method overriding is a feature that allows a subclass, or a child class, to specifically implement a method already given in one of its super-classes, or parent classes, in any object-oriented programming language.

Thus, the process of redefining a parent class’s method in a subclass is known as method overriding. It is also called run time polymorphism or dynamic binding because the compiler doesn’t really know the type of object passed on compilation.

When a method in a subclass has the same name, parameters or signature, and return type (or sub-type) as a method in its super-class, then the method in the subclass (the child class) overrides the method in the super-class (the parent class).

We can implement method overriding in any object-oriented programming language, but only when the classes involved have an ‘IS-A’ relationship of inheritance between them.

What keyword allows a child class to access its parent classs implementation of a method that the child class is overriding?

In the illustration above, the Rectangle and Circle classes are overriding the Shape of the class’s getArea() method. The purpose of overriding is achieved so that a sub class can provide its own implementation to a method that a superclass already provides.

In simple Java programs, you may work with just one class and one file. However, as your programs become more complex you will work with multiple classes, each of which requires its own file. Only one of these files in the Java package requires a main() method, and this is the file that will be run in the package.

For example, say we have two files in our Java package for two different classes:

  • Shape, the parent class.
  • Square, the child class.

If the Java file containing our Shape class is the only one with a main() method, this is the file that will be run for our Java package.

class Shape {

public static void main(String[] args) {

Square sq = new Square();

}

}

class Square extends Shape {

}